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