PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Aug 19, 2019 8:15 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Apr 02, 2019 4:17 am 
Offline

Joined: Tue Apr 02, 2019 4:10 am
Posts: 1
PDF File Reading PDFSharp exceptions :

We have implemented the pdfsharp logic to read pdf file, but somehow we got many below errors.

Note :File uploading as expected, but exception are coming.

Can you please help me on this below error.


Code:
System.Text.StringBuilder text = new System.Text.StringBuilder();
            bool result = false;
            string currentText = string.Empty;
            PdfDocument pdfReaderObj = new PdfDocument();
            try
            {
                if (File.Exists(fileName))
                {
                   [b]pdfReaderObj = PdfReader.Open(fileName, PdfDocumentOpenMode.ReadOnly)[/b]

                    for (int page = 0; page < pdfReaderObj.PageCount; page++)
                    {
                        CObject content = ContentReader.ReadContent(pdfReaderObj.Pages[page]);
                        var extractedText = ExtractText(content);
                        currentText = System.Text.Encoding.UTF8.GetString(System.Text.ASCIIEncoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, System.Text.Encoding.Default.GetBytes(extractedText.ToString())));
                        text.Append(currentText);
                    }
                }

                if (result = !string.IsNullOrWhiteSpace(text.ToString()))
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (PdfSharp.PdfSharpException pdfEx)
            {
               
               
                     //throwing error message
               
            }
            catch (Exception ex)
            {
              //throwing error message
            }


    ErrorMessage : FileName :"" >> ReadPdfFile,Error :PdfSharp.Pdf.IO.PdfReaderException:
    Token '1' was not expected
    at PdfSharp.Internal.ParserDiagnostics.ThrowParserException(String message) at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream) at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider) at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider) at PdfSharp.Pdf.IO.PdfReader.Open(String path, PdfDocumentOpenMode openmode)


    EventLogEntryType : Error ErrorMessage : FileName :"">> ReadPdfFile,Error PdfSharp.Pdf.IO.PdfReaderException: Unexpected character '0xffff' in PDF stream
    The file may be corrupted. If you think this is a bug in PDFsharp, please send us your PDF file. at PdfSharp.Internal.ParserDiagnostics.ThrowParserException
    (String message) at PdfSharp.Internal.ParserDiagnostics.HandleUnexpectedCharacter(Char ch) at PdfSharp.Pdf.IO.Parser.ReadSymbol(Symbol symbol)
    at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream) at
    PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
    at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider) at PdfSharp.Pdf.IO.PdfReader.Open


    ErrorMessage : FileName :"" >> ReadPdfFile,Error :PdfSharp.Pdf.IO.PdfReaderException:
    Unexpected token 'R' in PDF stream
    The file may be corrupted. If you think this is a bug in PDFsharp, please send us your PDF file. at PdfSharp.Internal.ParserDiagnostics.ThrowParserException(String message) at PdfSharp.Internal.ParserDiagnostics.HandleUnexpectedToken(String token) at PdfSharp.Pdf.IO.Parser.ReadSymbol(Symbol symbol) at
    PdfSharp.Pdf.IO.Parser.ReadXRefStream(PdfCrossReferenceTable xrefTable) at PdfSharp.Pdf.IO.Parser.ReadXRefTableAndTrailer(PdfCrossReferenceTable xrefTable)at PdfSharp.Pdf.IO.Parser.ReadTrailer() at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider) atPdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider) at PdfSharp.Pdf.IO.PdfReader.Open
    (String path, PdfDocumentOpenMode openmode)


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 02, 2019 8:13 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2956
Location: Cologne, Germany
There are many corrupt PDF files around.

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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