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

The given key was not present in the dictionary
https://forum.pdfsharp.net/viewtopic.php?f=3&t=1031
Page 1 of 1

Author:  AngelX [ Thu Jan 21, 2010 7:18 pm ]
Post subject:  The given key was not present in the dictionary

I have a file that when trying to copy pages it gives a KeyNotFoundException error.
Error happens at line 5 on following code:
Code:
1      PdfDocument PDFDoc = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
2      PdfDocument document = new PdfSharp.Pdf.PdfDocument();
3      for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
4      {
5          document.AddPage(PDFDoc.Pages[Pg]);
6      }


I can forward the PDF in a email if needed, appreciate the help.

Detail on error:
Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given
key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at PdfSharp.Pdf.Advanced.PdfImportedObjectTable.get_Item(PdfObjectID external
ID)
at PdfSharp.Pdf.PdfObject.FixUpObject(PdfImportedObjectTable iot, PdfDocument
owner, PdfObject value)
at PdfSharp.Pdf.PdfObject.FixUpObject(PdfImportedObjectTable iot, PdfDocument
owner, PdfObject value)
at PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObject
Table, PdfDocument owner, PdfObject externalObject)
at PdfSharp.Pdf.PdfPages.CloneElement(PdfPage page, PdfPage importPage, Strin
g key, Boolean deepcopy)
at PdfSharp.Pdf.PdfPages.ImportExternalPage(PdfPage importPage)
at PdfSharp.Pdf.PdfPages.Insert(Int32 index, PdfPage page)
at PdfSharp.Pdf.PdfDocument.AddPage(PdfPage page)
at Watermark.Program.Main()

Author:  AngelX [ Fri Jan 22, 2010 6:13 pm ]
Post subject:  Re: The given key was not present in the dictionary

Never mind, I also encountered the iref issue with the PDFs I'm trying to watermark so after reading this is not supported I had no choice but to change the library to iTextSharp, which by the way works great, it's very stable and I've now completed my project.

If you are still interested in the PDF file I can still email it.

Author:  purushothaman [ Tue Nov 22, 2016 8:22 am ]
Post subject:  Re: The given key was not present in the dictionary

Can you send the sample file with fix. Because I am also getting same problem. So I have tried with iTextSharp to read PDF file like as below,

Code:
Dim oReader As New iTextSharp.text.pdf.PdfReader(fileStream)
Dim pdfdocument As New iTextSharp.text.Document(oReader.GetPageSizeWithRotation(1))
Dim n As Integer = oReader.NumberOfPages
Dim outputStream As New MemoryStream
Dim pdfwriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfdocument, outputStream)
pdfwriter.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4)

pdfdocument.Open()
Dim cb As iTextSharp.text.pdf.PdfContentByte = pdfwriter.DirectContent
Dim page As iTextSharp.text.pdf.PdfImportedPage

Dim rotation As Integer

Dim i As Integer = 0
While i < n
i += 1
pdfdocument.SetPageSize(oReader.GetPageSizeWithRotation(i))
pdfdocument.NewPage()
page = pdfwriter.GetImportedPage(oReader, i)
rotation = oReader.GetPageRotation(i)
If rotation = 90 OrElse rotation = 270 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
oReader.GetPageSizeWithRotation(i).Height)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
0)
End If
End While

'---- Keep the stream open!
pdfwriter.CloseStream = False

'----- we close the document
pdfdocument.Close()

return  PdfSharp.Pdf.IO.PdfReader.Open(outputStream, openmode)


But links are disabled. Please let me know how to handle this issue.

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