PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Oct 22, 2017 4:47 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Jan 12, 2017 7:21 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Hi PdfSharp Team,

Below code wrote to reading existing PDF file and Get page count.

using (PdfDocument InputDocument = PdfReader.Open(PDFPath, PdfDocumentOpenMode.Import))
{
if (InputDocument != null && InputDocument.Pages.Count != 0)
_ActualPageCount = InputDocument.PageCount;
}

This code executed with out any issue, but for particular PDF file throw error at line "InputDocument.Pages.Count" message with " Object reference not set to an instance".

Kindly need your help to fix this problem.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 12, 2017 7:49 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Hi PdfSharp Team,

Below code wrote to reading existing PDF file and Get page count.

using (PdfDocument InputDocument = PdfReader.Open(PDFPath, PdfDocumentOpenMode.Import))
{
if (InputDocument != null && InputDocument.Pages.Count != 0)
_ActualPageCount = InputDocument.PageCount;
}

This code executed with out any issue, but for particular PDF file throw error at line "InputDocument.Pages.Count" message with " Object reference not set to an instance".

Kindly need your help to fix this problem.

Thanks and Regards,

Raj.A


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 12, 2017 9:19 am 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2720
Location: Cologne, Germany
rajarul wrote:
This code executed with out any issue, but for particular PDF file throw error at line "InputDocument.Pages.Count" message with " Object reference not set to an instance".
We need the PDF file to replicate the issue.

See also:
viewtopic.php?f=2&t=832

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 13, 2017 4:32 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Hi PdfSharpTeam,

I attached PDF file to replicated this issue.Kindly let me know,if you have any difficulty.


Thanks and Regards,

Raj.A


Attachments:
SWINX.zip [14.27 KiB]
Downloaded 35 times
Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 16, 2017 4:48 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2720
Location: Cologne, Germany
Hi, Raj.A!
rajarul wrote:
I attached PDF file to replicated this issue.Kindly let me know,if you have any difficulty.
You attached a couple of HTML files, but no PDF.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 18, 2017 7:44 am 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Hi Thomas,

My apologies for this mistake.

Now you can download from from URL :https://github.com/rajarul82/SamplePDF/blob/master/adlibris.pdf.Since file size not allow to attach.

Thanks and Regards,

Raj.A


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 18, 2017 12:19 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2720
Location: Cologne, Germany
Hi, Raj.A,

Thanks for the file.
Something goes wrong when PDFsharp tries to open that PDF file. I don't know yet what goes wrong or how to fix it.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 19, 2017 2:19 pm 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Hi Thomas,

Any updated regards this issue.

Thanks and Regards,

Raj.A


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 21, 2017 5:03 pm 
Offline

Joined: Wed Jun 21, 2017 4:57 pm
Posts: 2
Hi:

I am also receiving an object error on the PageCount property, on several pdf's that FoxIt can easily open. So far, our solution has been: Open with FoxIt, save to another file, then upload the file again. However, the users are getting restless.

Thanks,
Steve


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 22, 2017 7:29 pm 
Offline

Joined: Tue Sep 30, 2014 12:29 pm
Posts: 16
The linked file contains a (somewhat) broken xref table.
The xref table starts with
xref
1 7
when it should be
xref
0 7

According to the Pdf-Spec, Version 1.7:
Quote:
For a file that has never been incrementally updated, the cross-reference section shall contain only one subsection, whose object numbering begins at 0.


When PdfSharp reads the rest of the file, this leads to references inside the pdf pointing to the wrong objects.

A possible fix may be forcing PdfSharp to always start with object number 0, regardless of the number given in the xref table.

A place to fix this is in PdfSharp.Pdf.IO.Parser.cs
There is a Method called "ReadXRefTableAndTrailer".
Replace the complete method with this code:
(Note: i used version 1.32)
Code:
        PdfTrailer ReadXRefTableAndTrailer(PdfReferenceTable xrefTable)
        {
            Debug.Assert(xrefTable != null);

            Symbol symbol = ScanNextToken();
            // Is it an xref stream?
            if (symbol == Symbol.Integer)
                throw new PdfReaderException(PSSR.CannotHandleXRefStreams);
            // TODO: It is very high on the todo list, but still undone
            Debug.Assert(symbol == Symbol.XRef);
            var firstSection = true;
            while (true)
            {
                symbol = ScanNextToken();
                if (symbol == Symbol.Integer)
                {
                    int start = this.lexer.TokenToInteger;
                    var firstId = start;
                    if (firstSection)
                        firstId = 0;            // first section (that may be the one and only section) shall start with object number 0 (Pdf Reference 1.7, 7.5.4)
                    int length = ReadInteger();
                    var lastId = firstId + length;
                    for (int id = firstId; id < lastId; id++)
                    {
                        int position = ReadInteger();
                        int generation = ReadInteger();
                        ReadSymbol(Symbol.Keyword);
                        string token = lexer.Token;
                        if (id == 0 && firstSection)
                        {
                            if (token == "f")           // first entry is a free entry, ok
                                continue;
                            else
                            {
                                id = start;             // free entry is missing, use given object id (should we throw in this case ?)
                                lastId = start + length;
                            }
                        }
                        // Skip unused entries.
                        if (token != "n")
                            continue;
                        // Even it is restricted, an object can exists in more than one subsection.
                        // (PDF Reference Implementation Notes 15).
                        PdfObjectID objectID = new PdfObjectID(id, generation);
                        // Ignore the latter one
                        if (xrefTable.Contains(objectID))
                            continue;
                        xrefTable.Add(new PdfReference(objectID, position + lexer.StartOffset));
                    }
                    firstSection = false;
                }
                else if (symbol == Symbol.Trailer)
                {
                    ReadSymbol(Symbol.BeginDictionary);
                    PdfTrailer trailer = new PdfTrailer(this.document);
                    this.ReadDictionary(trailer, false);
                    return trailer;
                }
                else
                    throw new PdfReaderException(PSSR.UnexpectedToken(this.lexer.Token));
            }
        }

With these changes, i was able to open the linked pdf.

Maybe the PdfSharp team has better/additional ideas to fix this ?
(well, it's actually not a fix for something, it just makes the library more tolerant against errors originating elsewhere)


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 26, 2017 4:11 pm 
Offline

Joined: Wed Jun 21, 2017 4:57 pm
Posts: 2
Hey, thanks (void). This fix seems to work very well for version 1.32. Unfortunately, I need to use version 1.5, and it doesn't work there.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 27, 2017 12:55 pm 
Offline

Joined: Mon Dec 28, 2015 6:04 am
Posts: 8
Thanks for your support, But I had used some other library to handle this issue.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 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:  
Powered by phpBB® Forum Software © phpBB Group