:
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 VB.NET. ByteScout PDF Suite was made to help with slice PDF by horizontal lines inside in VB.NET. ByteScout PDF Suite is the bundle that provides six different SDK libraries to work with PDF from generating rich PDF reports to extracting data from PDF documents and converting them to HTML. This bundle includes PDF (Generator) SDK, PDF Renderer SDK, PDF Extractor SDK, PDF to HTML SDK, PDF Viewer SDK and PDF Generator SDK for Javascript.
Save time on writing and testing code by using the code below and use it in your application. If you want to know how it works, then this VB.NET sample code should be copied and pasted into your application’s code editor. Then just compile and run it. These VB.NET sample examples can be used in one or many applications.
Trial version can be downloaded from our website for free. It contains this and other source code samples for VB.NET.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
Imports System.Drawing
Imports System.IO
Imports Bytescout.PDFExtractor
Imports Bytescout.PDFRenderer
Imports Bytescout.PDF
''' <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>
Module Module1
Dim InputFile = ".\sample.pdf"
Dim OutputFolder = ".\output"
Sub Main()
if Not Directory.Exists(OutputFolder)
Directory.CreateDirectory(OutputFolder)
End If
' Create LineDetector instance and load document
Dim lineDetector = new LineDetector("demo", "demo")
lineDetector.LoadDocumentFromFile(InputFile)
' Create RasterRenderer instance and load document
Dim rasterRenderer = new RasterRenderer("demo", "demo")
rasterRenderer.LoadDocumentFromFile(InputFile)
Try
Dim pageCount = lineDetector.GetPageCount()
For pageIndex As Integer = 0 To pageCount - 1
Console.WriteLine("Processing page #{0}", pageIndex)
' Find horizontal lines on the page
Dim horzLines = lineDetector.FindLines(pageIndex, LineOrientationsToFind.OnlyHorizontal)
' Slice page by separating lines and create new PDF documents
Slice(pageIndex, horzLines, rasterRenderer)
Next
Catch exception As Exception
Console.WriteLine(exception)
Finally
' Cleanup
rasterRenderer.Dispose()
lineDetector.Dispose()
End Try
Console.WriteLine()
Console.WriteLine("Press any key...")
Console.ReadKey()
End Sub
Private Sub Slice(pageIndex As Integer, lines As FoundLinesCollection, rasterRenderer As RasterRenderer)
Const pdfRenderingResolution As Single = 300
Dim lastLineY As Single = Single.NaN
Dim sliceNum as Integer = 0
Dim pageRect = rasterRenderer.GetPageRectangle(pageIndex)
For Each line As FoundLine In lines
If Single.IsNaN(lastLineY)
lastLineY = line.From.Y
Else
' Compute slice rectangle
Dim 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
Dim slice = rasterRenderer.GetImage(pageIndex, pdfRenderingResolution)
' Create PDF document
Dim document = new Document With {
.RegistrationName = "demo",
.RegistrationKey = "demo"
}
' Create page of A4 size
Dim page = new Page(PaperFormat.A4)
document.Pages.Add(page)
' Create PDF Image object from System.Drawing.Image
Dim pdfImage = new Bytescout.PDF.Image(slice)
' Draw image on the page keeping the aspect ratio
Dim 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
sliceNum = sliceNum + 1
Dim fileName = string.Format("{0}\{1}-page{2}-slice{3}.pdf", OutputFolder, IO.Path.GetFileName(InputFile), pageIndex, sliceNum)
document.Save(fileName)
Console.WriteLine("Saved slice '{0}'", fileName)
' Cleanup
document.Dispose()
slice.Dispose()
lastLineY = line.From.Y
End If
Next
End Sub
End Module
60 Day Free Trial or Visit ByteScout PDF Suite Home Page
Explore ByteScout PDF Suite Documentation
Explore Samples
Sign Up for ByteScout PDF Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout PDF Suite Home Page
Explore ByteScout PDF Suite Documentation
Explore Samples
Sign Up for ByteScout PDF Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: