PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Save() throw an exception An item with the same key has ...
https://forum.pdfsharp.net/viewtopic.php?f=3&t=1946
Page 1 of 1

Author:  prometheus_zh [ Wed Mar 21, 2012 1:56 pm ]
Post subject:  Save() throw an exception An item with the same key has ...

Hello,

I'm very happy using PdfSharp, but just only today we had a problem using it with a large file (32mo)
While calling Save() the exception "An item with the same key has already been added" is thrown.

The PDF is openned with :
_pdfDocument = PdfSharpPdf.IO.PdfReader.Open( IO.File.OpenRead(filePath), PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)

The exception is happening in PdfReferenceTable.cs:257 of the last release "1.32.2608.0"

Image

Can I solve the probleme without having anything messed up in the PDFs with a IF before testing if the "this.objectTable" ContainsKey(iref.ObjectID) ? If it contains then nothing is added

What do you think about this solution ?

Thank you for helping

Attachments:
capture error PdfSharp.png
capture error PdfSharp.png [ 23.32 KiB | Viewed 7132 times ]

Author:  prometheus_zh [ Wed Mar 21, 2012 3:15 pm ]
Post subject:  Re: Save() throw an exception An item with the same key has ...

Hello again,

The code that produce this error is the following:
Code:
        Using graf = Drawing.XGraphics.FromPdfPage(page)

            Dim pen As Drawing.XPen = _penConfig
           
            For Each stick In sequence

                graf.DrawLine(pen, positionX, positionY, positionX, positionY + PipeHeight)
               
                positionX += PipeWidth + PipeSpace
            Next
        End Using


We use a XGraphics object to draw bars, when commenting this part they are no error in the Saving of the Pdf.

When I keep this part, the exception is thrown and when comparing the 2 objects (one already existing in the dictionary and the one causing the exception. "iref" is from the loop iteration of "irefs", "o" is the PdfReference already existing in the directionary) we can see borth have the same ObjectID but different uid. ref attached image.

How is this possible as we dispose the XGraphics reference ?

Thanks for helping :-)

Attachments:
File comment: Code edited for testing the exception
capture error PdfSharp3.png
capture error PdfSharp3.png [ 20.67 KiB | Viewed 7131 times ]
capture error PdfSharp2.png
capture error PdfSharp2.png [ 46.01 KiB | Viewed 7131 times ]

Author:  Thomas Hoevel [ Mon Mar 26, 2012 7:36 am ]
Post subject:  Re: Save() throw an exception An item with the same key has ...

Hi!

Boss says this probably is a bug in PDFsharp. Please provide us with your PDF file and your code so we can replicate the problem here using a debugger.
E-mail address sent via PM.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/