PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 27, 2020 5:26 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: PageCount
PostPosted: Mon Nov 02, 2020 5:19 pm 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Hallo,
in my Visual VB Projekt i have implemented PdfSharp.dll (Version 1.31.1789.0) for create a PDF from an Image.
Now I need the function PageCount, but the code:
Dim z As Integer = PdfReader.Open(e.FullPath, PdfDocumentOpenMode.Import).PageCount
causes an error:
bei PdfSharp.Pdf.IO.Lexer.ReadStream(Int32.length)

When I make an update to Version 1.5, causes an error this code:
Code:
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim image As XImage = XImage.FromFile(s)
gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))

(cannot DrawImage)
What can I do?

Best regards
Norbert


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Mon Nov 02, 2020 5:45 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 592
Location: CCAA
Hi!
strzata wrote:
What can I do?
Maybe show some more code? Some more information about the error?
Which PDF do you try to open?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Mon Nov 02, 2020 6:37 pm 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Thank you for the fast reaction.
All of my documents come from our new Brother ADS 2400 scanner.
Here is my code that works fine:
Code:
    Private Function SaveToPDF() As Boolean
        Dim files() As String = Directory.GetFiles("c:\temp\scans")
        If files.Length < 1 Then
            MessageBox.Show("Keine Dokumente vorhanden")
            Return False
        End If

        Dim di As DirectoryInfo = New DirectoryInfo(tbFolder.Text)
        Dim filename As String = ""
        If Not di.Exists Then di.Create()

        Using doc As New PdfDocument
            For Each s As String In files
                Dim page As PdfPage = doc.AddPage()
                page.Size = PageSize.A4
                Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
                Dim image As XImage = XImage.FromFile(s)

                If (CInt(image.PointWidth) > CInt(image.PointHeight)) Then
                    page.Orientation = PageOrientation.Landscape
                End If

                gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))
                image.Dispose()
                File.Delete(s)
            Next
            filename = tbFilename.Text & "-" & Format(Now, "HHmmss")
            doc.Save(tbFolder.Text & "\" & filename & ".pdf")
        End Using

        Return True
    End Function

He doesn't work at
Code:
            Dim z As Integer = PdfSharp.Pdf.IO.PdfReader.Open(e.FullPath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import).PageCount

In version 1.5 the following does not work
Code:
gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))

Why Notify me when a reply is posted doesn't work?


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Tue Nov 03, 2020 8:34 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 592
Location: CCAA
So you create the PDF and thus you a) have the PDF in memory and can query the page count any time and b) you create the PDF and can easily count how often you call "AddPage()".
So why do you call "Dim z As Integer = PdfSharp.Pdf.IO.PdfReader.Open(e.FullPath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import).PageCount"?
Why do you access the property directly without assigning the PDF document to a variable?

What's the problem with the line "gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))" with version 1.5x? Are you using the latest 1.51 version?

Maybe some notification e-mails get lost...

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Tue Nov 03, 2020 11:49 am 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Thanks for the answer.
We mainly scan via our VB.Net program. But this doesn't work with some devices. In these cases we use the manufacturer's software. This provides us with a finished PDF in which we want to determine the number of pages.

The code
Code:
Dim z As Integer = PdfSharp.Pdf.IO.PdfReader.Open(e.FullPath, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import).PageCount

throws an exception
Code:
by PdfSharp.Pdf.IO.Lexer.ReadStream(Int32 length)
by PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences)
by PdfSharp.Pdf.IO.PdfReader.Open(Stream, stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
...


Quote:
What's the problem with the line "gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))" with version 1.5x? Are you using the latest 1.51 version?

At the moment I have uninstalled version 1.5 and want to solve it with my old version first.


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Wed Nov 04, 2020 7:35 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 592
Location: CCAA
strzata wrote:
At the moment I have uninstalled version 1.5 and want to solve it with my old version first.
Many bugs have been fixed in the 6 or 7 years since version 1.32 was published.
IIRC this includes exceptions being thrown by corrupt PDF files coming from certain scanner software.
Corrupt files will still often throw exceptions.

But it is your decision which version you use.

Simple test: Open the PDF with Adobe Reader and use "Save as" to create a new file. If the new file works without issues then the original file is corrupt.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Wed Nov 04, 2020 8:22 am 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Thanks! Are new Scanners (2 weeks old): one HP 9022 and one Brother 2400
I'll keep looking for a solution and get back to you later.
I already tend to switch to the new software. I will later download the version 1.5x via Github directly in Visual Studio. Would that be okay?

Best regards
Norbert


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Wed Nov 04, 2020 1:00 pm 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Now I have installed the Version 1.50.5147.0 in VisualStudio Vb.Net
PageCount (Dim z as Integer = IO.PdfReader ...) actually brings the error message
Unexpected character '0xffff' in PDF stream. The file may be corrupted.
I used another 51 page PDF (no scan) and now the code is executing correctly!

I have now 2 problems:
1. What can I do so that the PDFs delivered by the (new) scanners can be processed? I only need the pagecount function here - no more.
2. gfx.DrawImage (see code above) does not work
Errors:
a) The value of type "Rectangle" cannot be converted to "XPoint"
b) The value of type "Rectangle" cannot be converted to "XRect"

I have imported in my VB project:
Imports PdfSharp
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System.Drawing.Imaging


Please help us - it is very important to our practice.


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Wed Nov 04, 2020 2:09 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 592
Location: CCAA
strzata wrote:
1. What can I do so that the PDFs delivered by the (new) scanners can be processed? I only need the pagecount function here - no more.
PDFsharp is not really good when it comes to dealing with corrupt files. A workaround described somewhere on this forum is using iTextSharp to open and save the PDF and then finally open it with PDFsharp. iTextSharp does a better job when it comes to fixing corrupt PDF files. Older versions of iTextSharp with a more permissive license can be used for legal/financial reasons.

Re "gfx.DrawImage(image, New Rectangle(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))"

Maybe try "gfx.DrawImage(image, New XRect(0, 0, CInt(page.Width.Point), CInt(page.Height.Point)))"
Maybe try "gfx.DrawImage(image, 0, 0, page.Width.Point, page.Height.Point)"
Maybe try "gfx.DrawImage(image, 0, 0, CInt(page.Width.Point), CInt(page.Height.Point))"

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Wed Nov 04, 2020 2:33 pm 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
Perfect!
gfx.DrawImage(image, New XRect(0, 0, CInt(page.Width.Point), CInt(page.Height.Point))) works fine! Thank you!!!

Would work my code "SaveScannedFile" (see above) with iTextSharp? What should I change?


Top
 Profile  
Reply with quote  
 Post subject: Re: PageCount
PostPosted: Thu Nov 05, 2020 7:10 am 
Offline

Joined: Mon Nov 02, 2020 4:54 pm
Posts: 7
The function has been rewritten using iTextSharp. Now everything works fine.
Thank you so much and very warm greetings Thomas
from Norbert


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 19 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