Public Shared Sub ExtractPdf(ByVal sourcePdf As String, ByVal pageNumbersToExtract As Integer(), ByVal outPdf As String)
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim doc As iTextSharp.text.Document = Nothing
Dim pdfCpy As iTextSharp.text.pdf.PdfCopy = Nothing
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Try
iTextSharp.text.Document.Compress = True
reader = New iTextSharp.text.pdf.PdfReader(sourcePdf)
doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
'doc.Compress = iTextSharp.text.Document.Compress = True
pdfCpy = New iTextSharp.text.pdf.PdfCopy(doc, New IO.FileStream(outPdf, IO.FileMode.Create))
doc.Open()
For Each pageNum As Integer In pageNumbersToExtract
page = pdfCpy.GetImportedPage(reader, pageNum)
pdfCpy.AddPage(page)
Next
Form1.setPageSize(reader.NumberOfPages)
pdfCpy.CompressionLevel = 100
pdfCpy.SetFullCompression()
doc.Close()
reader.Close()
Catch ex As Exception
Throw ex
End Try
End Sub
|