ByteScout Premium Suite - C# - Slice PDF by horizontal lines inside - ByteScout

ByteScout Premium Suite – C# – Slice PDF by horizontal lines inside

  • Home
  • /
  • Articles
  • /
  • ByteScout Premium Suite – C# – Slice PDF by horizontal lines inside

slice PDF by horizontal lines inside in C# using ByteScout Premium Suite

slice PDF by horizontal lines inside in C#

We regularly create and update our sample code library so you may quickly learn slice PDF by horizontal lines inside and the step-by-step process in C#. ByteScout Premium Suite was made to help with slice PDF by horizontal lines inside in C#. ByteScout Premium Suite is the set that includes 12 SDK products from ByteScout including tools and components for PDF, barcodes, spreadsheets, screen video recording.

This rich and prolific sample source code in C# for ByteScout Premium Suite contains various functions and options you should do calling the API to implement slice PDF by horizontal lines inside. To use slice PDF by horizontal lines inside in your C# project or application just copy & paste the code and then run your app! Want to see how it works with your data then code testing will allow the function to be tested and work properly.

Visit our website to get a free trial version of ByteScout Premium Suite. Free trial contains many of source code samples to help you with your C# project.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using System; using System.Drawing; using System.IO; using Bytescout.PDF; using Bytescout.PDFExtractor; using Bytescout.PDFRenderer; using Image = System.Drawing.Image; using Path = System.IO.Path; namespace SlicePdfByLinesExample { /// <summary> /// The example demonstrates slicing of PDF document pages by horizontal lines. /// The example uses three products: /// - PDF Extractor SDK (https://bytescout.com/products/developer/pdfextractorsdk/index.html) - /// to detect lines; /// - PDF Renderer SDK (https://bytescout.com/products/developer/pdfrenderersdk/index.html) - /// to render PDF documents to raster images; /// - PDF SDK (https://bytescout.com/products/developer/pdfsdk/index.html) - /// to create new PDF documentsd from sliced images. /// </summary> class Program { static string InputFile = @".\sample.pdf"; static string OutputFolder = @".\output"; static void Main(string[] args) { if (!Directory.Exists(OutputFolder)) Directory.CreateDirectory(OutputFolder); // Create LineDetector instance and load document LineDetector lineDetector = new LineDetector("demo", "demo"); lineDetector.LoadDocumentFromFile(InputFile); // Create RasterRenderer instance and load document RasterRenderer rasterRenderer = new RasterRenderer("demo", "demo"); rasterRenderer.LoadDocumentFromFile(InputFile); try { int pageCount = lineDetector.GetPageCount(); for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { Console.WriteLine("Processing page #{0}", pageIndex); // Find horizontal lines on the page FoundLinesCollection horzLines = lineDetector.FindLines(pageIndex, LineOrientationsToFind.OnlyHorizontal); // Slice page by separating lines and create new PDF documents Slice(pageIndex, horzLines, rasterRenderer); } } catch (Exception exception) { Console.WriteLine(exception); } finally { // Cleanup rasterRenderer.Dispose(); lineDetector.Dispose(); } Console.WriteLine(); Console.WriteLine("Press any key..."); Console.ReadKey(); } static void Slice(int pageIndex, FoundLinesCollection lines, RasterRenderer rasterRenderer) { const float pdfRenderingResolution = 300; float lastLineY = float.NaN; int sliceNum = 0; RectangleF pageRect = rasterRenderer.GetPageRectangle(pageIndex); foreach (FoundLine line in lines) { if (float.IsNaN(lastLineY)) { lastLineY = line.From.Y; } else { // Compute slice rectangle RectangleF sliceRect = new RectangleF(0, lastLineY, pageRect.Width, line.From.Y - lastLineY); // Set extraction rectangle for RasterRenderer rasterRenderer.SetExtractionArea(sliceRect); // Render the page region to Image object Image slice = rasterRenderer.GetImage(pageIndex, pdfRenderingResolution); // Create PDF document Document document = new Document { RegistrationName = "demo", RegistrationKey = "demo" }; // Create page of A4 size Page page = new Page(PaperFormat.A4); document.Pages.Add(page); // Create PDF Image object from System.Drawing.Image Bytescout.PDF.Image pdfImage = new Bytescout.PDF.Image(slice); // Draw image on the page keeping the aspect ratio RectangleF r = new RectangleF(0, 20, page.Width, page.Width / slice.Width * slice.Height); page.Canvas.DrawImage(pdfImage, r.Left, r.Top, r.Width, r.Height); // Save PDF document string fileName = string.Format(@"{0}\{1}-page{2}-slice{3}.pdf", OutputFolder, Path.GetFileName(InputFile), pageIndex, ++sliceNum); document.Save(fileName); Console.WriteLine("Saved slice '{0}'", fileName); // Cleanup document.Dispose(); slice.Dispose(); lastLineY = line.From.Y; } } } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Premium Suite Home Page

Explore ByteScout Premium Suite Documentation

Explore Samples

Sign Up for ByteScout Premium Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout Premium Suite Home Page

Explore ByteScout Premium Suite Documentation

Explore Samples

Sign Up for ByteScout Premium Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next