Search code examples
c#pdf.net-coreitextitext7

How to split PDF document into small ones


I need to split a document into several small documents. For example, if document has 7 pages I need to generate 7 pdfs.

In iTextSharp I was using the following code, works pretty well. However, in iText 7 its not possible to do it in the same way.

###iTextSharp old code

var reader = new PdfReader(src);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
    var document = new Document();
    var copy = new PdfCopy(document, new FileStream(result + i + ".pdf", FileMode.Create));
    document.Open();
    copy.AddPage(copy.GetImportedPage(reader, i));
    document.Close();
}

###iText 7, but not working

First problem
I have found that there is PdfSplitter, which could split my pdf into small pdfs. However, even my testing pdf has 7 pages and even GetNumberOfPages() returns number 7, number of split documents is just one.

In this linked documenation is somehow shown how to split document. However, I have no idea how to make similar method to the one mentioned - getNextPdfWriter

Second problem
Even I have one file, its empty. I am wondering how to set proper writer to create correct pdf. Respectively, how to set reader in order to read the content of split document.

string result = outputPath + @"/page00";
using (pdfDocument = new PdfDocument(new PdfReader(pdfPath)))
{

    var splitter = new PdfSplitter(pdfDocument);
    var splittedDocs = splitter.SplitByPageCount(pdfDocument.GetNumberOfPages());

    for (int i = 0; i < pdfDocument.GetNumberOfPages(); i++)
    {
        //how to set reader to read the content of split docs. Or how to set writer for split doc.
        var pdfDoc = new PdfDocument(new PdfWriter(new FileStream(result + i + ".pdf", FileMode.Create)));
        pdfDoc.Close();
        splittedDocs[i].Close();
    }
}

##Question
How to properly split document into small ones in .NET core with iText 7


Solution

  • Well, it was quite easy. According to linked documentation I did the following:

    Create custom splitter overriding functionality of PdfSplitter.

    class CustomSplitter : PdfSplitter
    {
          private int _order;
          private readonly string _destinationFolder;
        
          public CustomSplitter(PdfDocument pdfDocument, string destinationFolder) : base(pdfDocument)
          {
              _destinationFolder = destinationFolder;
              _order = 0;
          }
        
          protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
          {
               return new PdfWriter(_destinationFolder + "splitDocument1_" +  _order++ + ".pdf");
          }
    }
    

    Then just use it for splitting a PDF document. Dont forget to close splitted document. And I would like to point out one thing. SplitByPageCount - it takes number according to wich should be splitted. SplitByPageCount(1) split a PDF document by one page. I trully misunderstood this method.

    using (var pdfDoc = new PdfDocument(new PdfReader("doc.pdf")))
    {
         var outputDir = @"C:\";
         var splitter = new CustomSplitter(pdfDoc, outputDir);
         var splittedDocs = splitter.SplitByPageCount(1);
    
         foreach (var splittedDoc in splittedDocs)
         {
             splittedDoc.Close();
         }
    }
    

    The result is a few pdfs.