You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue with PdfPig while trying to add text to pages imported from an existing PDF document. Here's the code I'm using:
usingvarrawDocument=PdfDocument.Open(path);usingvardocument=PdfDocument.Open(Augment(rawDocument));privatebyte[]Augment(PdfDocumentdocument){PdfDocumentBuilderbuilder=new();varfont=builder.AddStandard14Font(Standard14Font.Helvetica);for(inti=1;i<=document.NumberOfPages;i++){varpage=document.GetPage(i);varimages=page.GetImages();varpageBuilder=builder.AddPage(document,i);foreach(varimageinimages){varpoint=newPdfPoint(image.Bounds.BottomLeft.X,(image.Bounds.TopLeft.Y+image.Bounds.BottomLeft.Y)/2);// var imageIndex = AddImage(image); pageBuilder.AddText($"<<image-.png>>",8,point,font);}}byte[]fileBytes=builder.Build();returnfileBytes;}
Context:
I'm using builder.AddPage(document, i) to import pages from an existing PDF into a new PdfDocumentBuilder.
My goal is to add text annotations near images on the pages.
The AddImage method is not relevant to the issue (it's commented out).
Problem:
When I run this code, I get the following PdfDocumentFormatException errors when attempting to open the augmented PDF:
UglyToad.PdfPig.Core.PdfDocumentFormatException: 'Could not find the object number 50 0 with type StreamToken instead, it was found with type ObjectToken.'
The exception occurs in the BasePageFactory class, specifically in the following method:
publicTPageCreate(intnumber,DictionaryTokendictionary,PageTreeMemberspageTreeMembers,NamedDestinationsnamedDestinations){// ... varcontentStream=DirectObjectFinder.Get<StreamToken>(obj,PdfScanner);// ... }publicstaticT?Get<T>(IndirectReferencereference,IPdfTokenScannerscanner)whereT:class,IToken{vartemp=scanner.Get(reference);if(tempisnull||temp.DataisNullToken){returnnull;}if(temp.DataisTlocatedResult){returnlocatedResult;}if(temp.DataisIndirectReferenceTokennestedReference){returnGet<T>(nestedReference,scanner);}if(temp.DataisArrayTokenarray&&array.Data.Count==1){vararrayElement=array.Data[0];if(arrayElementisIndirectReferenceTokenarrayReference){returnGet<T>(arrayReference,scanner);}if(arrayElementisTarrayToken){returnarrayToken;}}thrownewPdfDocumentFormatException($"Could not find the object number {reference} with type {typeof(T).Name} instead, it was found with type {temp.GetType().Name}.");}
Observations:
If I remove the pageBuilder.AddText line, the code executes without errors, and the augmented PDF opens correctly.
This suggests that adding text to the imported pages is causing the issue.
This only happens on rare pdfs. Most of the time it is ok. I can give you an example pdf where this fails but would prefer to do it directly with you and not post it here.
Question:
Is it possible to use PdfPig's PdfDocumentBuilder to add text to pages imported from an existing PDF without encountering the PdfDocumentFormatException error? If so, how can I modify my code to achieve this?
The text was updated successfully, but these errors were encountered:
I'm encountering an issue with PdfPig while trying to add text to pages imported from an existing PDF document. Here's the code I'm using:
Context:
builder.AddPage(document, i)
to import pages from an existing PDF into a newPdfDocumentBuilder
.AddImage
method is not relevant to the issue (it's commented out).Problem:
When I run this code, I get the following
PdfDocumentFormatException
errors when attempting to open the augmented PDF:The exception occurs in the
BasePageFactory
class, specifically in the following method:Observations:
pageBuilder.AddText
line, the code executes without errors, and the augmented PDF opens correctly.Question:
Is it possible to use PdfPig's
PdfDocumentBuilder
to add text to pages imported from an existing PDF without encountering thePdfDocumentFormatException
error? If so, how can I modify my code to achieve this?The text was updated successfully, but these errors were encountered: