locked
Splitting a pdf report into individual files RRS feed

  • Question

  • I have a pdf report that is one consolidated/merged report that has data for each of our facilities (one page per facility). I need to be able to break this report into individual files for each facility as an automated process. I have used itextsharp.dll in the past using a VB windows form in VS2012 to merge files. Is there a way to use the same library to create individual reports from one large report? I would also need to be able to name the individual files using a naming scheme that would indicate the facility if that is possible (basically telling it to break the individual files by facility name which appear in the header on each page).  

    Thanks.

    • Moved by Sheng Jiang 蒋晟 Thursday, March 26, 2015 11:21 PM third party product, use stackoverflow and add itextsharp tag instead.
    Thursday, March 26, 2015 12:45 PM

Answers

  • For those who may be in a similar situation, here is the code I used to successfully split out the pdf to individual reports (you will need to have the itextsharp.dll file and add it as a reference to your project):

    Imports iTextSharp.text.pdf
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf.parser

    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
    Dim totpages As Integer
    Dim ExportRptPath As String
    Dim counter As Integer

    reader = New iTextSharp.text.pdf.PdfReader("C:\rptname.pdf")
    totpages = reader.NumberOfPages


    For counter = 1 To totpages
     
        doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
        ExportRptPath = "C:\test\newrpt_counter.pdf"
        pdfCpy = New iTextSharp.text.pdf.PdfCopy(doc, New IO.FileStream(ExportRptPath, IO.FileMode.Create))
        doc.Open()
        page = pdfCpy.GetImportedPage(reader, counter)
        pdfCpy.AddPage(page)
        doc.Close()
    Next

    • Marked as answer by J-Bal Monday, June 15, 2015 1:39 PM
    Monday, June 15, 2015 1:39 PM

All replies

  • Hello,

    You should ask on iTextSharp on StackOverflow

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Wednesday, April 1, 2015 8:29 PM
  • For those who may be in a similar situation, here is the code I used to successfully split out the pdf to individual reports (you will need to have the itextsharp.dll file and add it as a reference to your project):

    Imports iTextSharp.text.pdf
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf.parser

    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
    Dim totpages As Integer
    Dim ExportRptPath As String
    Dim counter As Integer

    reader = New iTextSharp.text.pdf.PdfReader("C:\rptname.pdf")
    totpages = reader.NumberOfPages


    For counter = 1 To totpages
     
        doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
        ExportRptPath = "C:\test\newrpt_counter.pdf"
        pdfCpy = New iTextSharp.text.pdf.PdfCopy(doc, New IO.FileStream(ExportRptPath, IO.FileMode.Create))
        doc.Open()
        page = pdfCpy.GetImportedPage(reader, counter)
        pdfCpy.AddPage(page)
        doc.Close()
    Next

    • Marked as answer by J-Bal Monday, June 15, 2015 1:39 PM
    Monday, June 15, 2015 1:39 PM