PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Oct 06, 2024 8:23 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Oct 04, 2024 10:04 am 
Offline

Joined: Fri Oct 04, 2024 9:52 am
Posts: 2
Hi,

I use PdfSharp to automate the processing of PDF documents. For some of them, "PdfReader.Open" fails with the following exception:

Code:
InvalidOperationException "Object already in table."


I cannot share the document, but I debugged the issue and found the following one-liner as fix:

Code:
diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfCrossReferenceTable.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfCrossReferenceTable.cs
index 7c18106..89c5e58 100644
--- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfCrossReferenceTable.cs
+++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.Advanced/PdfCrossReferenceTable.cs
@@ -60,6 +60,7 @@ public void Add(PdfReference iref)
 #endif
             }
             ObjectTable.Add(iref.ObjectID, iref);
+            MaxObjectNumber = Math.Max(MaxObjectNumber, iref.ObjectID.ObjectNumber);
         }

         /// <summary>


The background is that for some PDF files,
Code:
Add(PdfReference)
is called and adds objects to the ObjectTable, but MaxObjectNumber is not updated. When
Code:
Add(PdfObject)
is called for another object later on, and it does not have an ID, MaxObjectNumber is used, causing a collision and the exception.

I did a minimal fix that works for me, but the design is fragile. It is easy to call Add() without updating MaxObjectNumber. An abstraction layer could take care of this, but someone familiar with the code should better do this. Maybe the quick fix can be merged in the meantime?

I can also provide the fix as a git commit, but I did not find how to contribute this way on the home page of the project.


Last edited by ocroquette on Fri Oct 04, 2024 11:56 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 04, 2024 11:09 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 990
Location: CCAA
ocroquette wrote:
I can also provide the fix as a git commit, but I did not find how to contribute this way on the home page of the project.
You can add a PR here:
https://github.com/empira/PDFsharp

Not really needed for this one-line change.
Thanks for your feedback.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 04, 2024 11:55 am 
Offline

Joined: Fri Oct 04, 2024 9:52 am
Posts: 2
Thanks for the quick feedback. Oh right, there are PR's there. I had checked the commit log and it gave me the impression that it was a read-only repository.
Can the current devepment branch of PDFSharp be seen somewhere?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group