PdfPig: Adobe Reader cannot open some pdf created with PdfPig

Hi,

I’m using pdfPig to cut out one big Pdf file into several pdf. The code below is an example of how I am using your library to create one new pdf with only the first page of my source file :

            var stream = File.OpenRead(pdfPath);
            var sourceDocument = PdfDocument.Open(stream);
            var documentBuilder = new PdfDocumentBuilder();
            documentBuilder.AddPage(sourceDocument, 1);
            var newDoc = documentBuilder.Build();
            // then create fileStream from newDoc byte[]

The source file can be open without any error in Adobe Reader or Chrome. But, when I try to open the newly created with Adobe Reader, I encountered this error (It works fine with chrome btw) :

image

It works fine with many source Pdf but sometimes I’m facing this issue. It depends on the source file 😞 . Unfortunatly, due to the sensitive content of these pdf I cannot attached it.

Have you ever facing such an issue ? Let me know if you need more information.

Thanks in advance,

Florent

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 32 (14 by maintainers)

Most upvoted comments

@plaisted I have made some test and your PR fixes the bug with my pdf. Thanks ! And I hope it will be merged soon 😃

Can you try opening the resulting bad pdfs in PdfPig with strict mode turned on

PdfPig is unable to open the created (broken) PDF. Both a “normal” PdfDocument.Open() and one with ParsingOptions.LenientParsingOff throw the same exception:

UglyToad.PdfPig.Core.PdfDocumentFormatException
  HResult=0x80131500
  Message=Could not find dictionary associated with reference in pages kids array: 14 0.
  Source=UglyToad.PdfPig
  StackTrace:
   at UglyToad.PdfPig.Parser.CatalogFactory.ProcessPagesNode(IndirectReference reference, DictionaryToken nodeDictionary, IndirectReference parentReference, Boolean isRoot, IPdfTokenScanner pdfTokenScanner, Boolean isLenientParsing, Int32& pageNumber)
   at UglyToad.PdfPig.Parser.CatalogFactory.Create(IndirectReference rootReference, DictionaryToken dictionary, IPdfTokenScanner scanner, Boolean isLenientParsing)
   at UglyToad.PdfPig.Parser.PdfDocumentFactory.OpenDocument(IInputBytes inputBytes, ISeekableTokenScanner scanner, ILog log, Boolean isLenientParsing, IReadOnlyList`1 passwords, Boolean clipPaths)
   at UglyToad.PdfPig.Parser.PdfDocumentFactory.Open(IInputBytes inputBytes, ParsingOptions options)
   at UglyToad.PdfPig.Parser.PdfDocumentFactory.Open(String filename, ParsingOptions options)
   at UglyToad.PdfPig.PdfDocument.Open(String filePath, ParsingOptions options)