PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 26, 2024 5:41 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri Jan 03, 2014 1:42 pm 
Offline

Joined: Fri Jan 03, 2014 1:18 pm
Posts: 2
I have a PDF file which doesn't behave well with PDF sharp.
It seems to lack "Kids" and causes an exception in PdfPages.GetKids(...)

Code:
        PdfArray kids = kid.Elements["/Kids"] as PdfArray;
        //newTHHO 15.10.2007 begin
        if (kids == null)
        {
          PdfReference xref3 = kid.Elements["/Kids"] as PdfReference;
          kids = xref3.Value as PdfArray; // <--- Null reference here as kid.Elements does not contain tag "/Kids"
        }


Attached a sample project including the "faulty" PDF file.
Is there possible to make PDFSharp tolerate this?



Attachments:
ConcatenateDocuments.zip [41.51 KiB]
Downloaded 465 times
Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 03, 2014 3:24 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 340
laroom wrote:
Is there possible to make PDFSharp tolerate this?
We'll check this after the holidays.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 13, 2014 5:51 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!

I can replicate the exception, but cannot fix it. I will ask Stefan.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 14, 2014 1:56 pm 
Offline

Joined: Fri Jan 03, 2014 1:18 pm
Posts: 2
Any news on this issue?


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 16, 2014 1:50 pm 
Offline

Joined: Tue Jul 15, 2014 8:56 pm
Posts: 13
I had this problem and fixed it by just making an empty array when there is no kids, seems to work for me.

Code:
 
        if (xref3 == null)
          {
              kids = new PdfArray();
          }
          else
          {
          kids = xref3.Value as PdfArray;
          }


Full edited function shown below

Code:
    PdfDictionary[] GetKids(PdfReference iref, PdfPage.InheritedValues values, PdfDictionary parent)
    {
      // TODO: inherit inheritable keys...
      PdfDictionary kid = (PdfDictionary)iref.Value;

      if (kid.Elements.GetName(Keys.Type) == "/Page")
      {
        PdfPage.InheritValues(kid, values);
        return new PdfDictionary[] { kid };
      }
      else
      {
        Debug.Assert(kid.Elements.GetName(Keys.Type) == "/Pages");
        PdfPage.InheritValues(kid, ref values);
        List<PdfDictionary> list = new List<PdfDictionary>();
        PdfArray kids = kid.Elements["/Kids"] as PdfArray;
        //newTHHO 15.10.2007 begin
        if (kids == null)
        {
          PdfReference xref3 = kid.Elements["/Kids"] as PdfReference;

          if (xref3 == null)
          {
              kids = new PdfArray();
          }
          else
          {
          kids = xref3.Value as PdfArray;
          }

        }
        //newTHHO 15.10.2007 end
        foreach (PdfReference xref2 in kids)
          list.AddRange(GetKids(xref2, values, kid));
        int count = list.Count;
        Debug.Assert(count == kid.Elements.GetInteger("/Count"));
        //return (PdfDictionary[])list.ToArray(typeof(PdfDictionary));
        return list.ToArray();
      }
    }



ps - pdfsharp is fantastic, I use a combo of it and the google pdfium code (for rendering and text extraction) to do all my pdf systems now. Happy to contribute where possible.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 22, 2014 11:51 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
kensands wrote:
I had this problem and fixed it by just making an empty array when there is no kids, seems to work for me.
Thanks for the feedback. I'm afraid the fix does not work for the PDF file given in the first post.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 303 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