How to handle "No StructParents key" PDF syntax issue when using iText to copy pages?

32 Views Asked by At

I'm encountering an issue while using iText 7 to extract pages from a PDF document. When attempting to copy pages using the CopyPagesTo method, I'm receiving a System.NotSupportedException with the message Specified method is not supported. The exception trace indicates that the problem lies within the PdfDestination.MakeDestination method.

Upon further investigation using Adobe Preflight, I discovered a PDF syntax issue: No StructParents key in an object referenced by Pg key. It seems that this inconsistency in the PDF structure is causing the exception in iText.

Error in Adobe Preflight

This is the code

private void ExtractPages(PdfDocument pdfDoc, string filesOutputDir, string dest)
{
    int pageCount = pdfDoc.GetNumberOfPages();
    for (int pageNum = 1; pageNum <= pageCount; pageNum++)
    {
        var pageFileName = $"{dest}.{pageNum}.pdf";

        var outputFileName = System.IO.Path.Combine(filesOutputDir, pageFileName);
        var writerProperties = new WriterProperties();

        using (var writer = new PdfWriter(outputFileName))
        {
            using (var outputDoc = new PdfDocument(writer))
            {
                var page = pdfDoc.CopyPagesTo(pageNum, pageNum, outputDoc);
                outputDoc.Close();
            }
        }
    }
}

And this is the exception:

System.NotSupportedException: Specified method is not supported.
Result StackTrace:  
at iText.Kernel.Pdf.Navigation.PdfDestination.MakeDestination(PdfObject pdfObject)
   at iText.Kernel.Pdf.DestinationResolverCopyFilter.ProcessLinkAnnotion(PdfObject newParent, PdfObject value, PdfDictionary dict)
   at iText.Kernel.Pdf.DestinationResolverCopyFilter.ShouldProcess(PdfObject newParent, PdfName name, PdfObject value)
   at iText.Kernel.Pdf.PdfPage.CopyAnnotations(PdfDocument toDocument, PdfPage page, ICopyFilter copyFilter)
   at iText.Kernel.Pdf.PdfPage.CopyTo(PdfPage page, PdfDocument toDocument, IPdfPageExtraCopier copier)
   at iText.Kernel.Pdf.PdfPage.CopyTo(PdfDocument toDocument, IPdfPageExtraCopier copier, Boolean addPageToDocument, Int32 pageInsertIndex)
   at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument, Int32 insertBeforePage, IPdfPageExtraCopier copier)
   at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(Int32 pageFrom, Int32 pageTo, PdfDocument toDocument, Int32 insertBeforePage, IPdfPageExtraCopier copier)
   at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(Int32 pageFrom, Int32 pageTo, PdfDocument toDocument, IPdfPageExtraCopier copier)
   at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(Int32 pageFrom, Int32 pageTo, PdfDocument toDocument)
  1. Is there a way to handle or ignore the "No StructParents key" issue in the PDF structure when using iText to copy pages?
  2. Alternatively, are there any workarounds or modifications that can be made to the code to bypass or mitigate this issue?
  3. I can not fix the PDF file beforehand because it is an external input to my system. Could the presence of this syntax issue be addressed programmatically within iText?
0

There are 0 best solutions below