سؤال

I have code that takes a bunch of PDFs and combines them into one file...

PdfWriter writer = PdfWriter.getInstance(document, baos)
....
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, null, af.fileName, af.documentData.data)
fs.addCollectionItem(item)
writer.addFileAttachment(fs)

This works great and I see all of the pages as I would expect. Now I am looking to split it up and copy to multiple sub files based on size. However, when I consume the generated byte[] iText only seems to see the first page...

 PdfCopy copy = null
 PdfReader reader = new PdfReader(before) // Add the byte array here
 int pages = reader.getNumberOfPages() //It is only 1 I would expect more pages.

It seems to only be recognizing the cover page which was added before the other documents. Is there a way I can get the count of ALL pages? If I take before and just send it as an attachment it shows up with all 1xx pages.

هل كانت مفيدة؟

المحلول

You are combining the files by attaching them to a new PDF which only has a cover page, and making this file a Portfolio as Adobe calls it:

PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer, null, af.fileName, af.documentData.data);
fs.addCollectionItem(item);
writer.addFileAttachment(fs);

Thus, your code is not one of the typical merge functions (using PdfCopy or PdfWriter with getImportedPage; correct ones use PdfCopy, fishy ones use PdfWriter) which generate a single PDF containing all source pages as genuine document pages. Instead your code creates a single page document with other PDFs as attachments and some extra information making PDF viewers display the attached PDFs inline.

If you want to access the pages of these attached PDFs, you have to extract them again and open them in separate PdfReader instances.

You can find more information on extracting PDF attachments in this answer.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top