Splitting PDF pages programatically using ABCPdf 7

As a programmer, you are sometimes faced with the issue of taking one existing PDF and extracting a specific page from it to show to the user. We have been using ABCPDF7 for this which you can download from their website.

Website Link: http://www.websupergoo.com/abcpdf-1.htm

First, we import the dlls into the project reference:

Reference Include=”ABCpdf, Version=7.0.3.5, Culture=neutral, PublicKeyToken=a7a0b3f5184f2169, processorArchitecture=MSIL”>
<SpecificVersion>False</SpecificVersion>
<HintPath>…………..Program FilesWebSupergooABCpdf .NET 7.0ABCpdf.DLL</HintPath>
</Reference>

and the namespace at the top of the file. For this example I will be using VB.NET

Imports WebSupergoo.ABCpdf7
Imports WebSupergoo.ABCpdf7.Objects
Imports WebSupergoo.ABCpdf7.Atoms
Imports WebSupergoo.ABCpdf7.Operations

[…]
Declaring the source PDF and getting the number of pages:
Dim theSrc As Doc = New Doc()
theSrc.Read(Server.MapPath(“test.pdf”))
Dim theCount As Integer = theSrc.PageCount

Pages start their count from 1 which is different from the 0-based arrays in .NET. Now we go through the pages of the document and save each page as a separate PDF file

Dim i As Integer
For i = 1 To theCount
Dim theDst As Doc = New Doc()
theDst.MediaBox.String = theSrc.MediaBox.String

theDst.Rect.String = theDst.MediaBox.String
theDst.Rect.Inset(0, 0)
theDst.Rect.Position(0, 0)
Dim theX As Double, theY As Double
theX = theDst.MediaBox.Width / 2
theY = theDst.MediaBox.Height / 2

theDst.Page = theDst.AddPage()
theDst.FrameRect()
theDst.Save(Server.MapPath(“result” & CStr(i) & “.pdf”))
Next

The files will be saved in the local folder and will be result1, result2, etc..

If you only want one specific page, PageNumberWanted is the page number starting from 1 and less than the number of pages available (can be retrieved via the count method above). The code is:

Dim theDst As Doc = New Doc()

theDst.MediaBox.String = theSrc.MediaBox.String
theDst.Rect.String = theDst.MediaBox.String
theDst.Rect.Inset(0, 0)
theDst.Rect.Position(0, 0)

theDst.Page = theDst.AddPage()
theDst.AddImageDoc(theSrc, PageNumberWanted, Nothing)
theDst.FrameRect()

strPath = Server.MapPath(“ReportPageNumberWanted.pdf”)
theDst.Save(strPath)

Advertisements