PDFsharp & MigraDoc Foundation http://forum.pdfsharp.com/ |
|
PageCount http://forum.pdfsharp.com/viewtopic.php?f=2&t=4195 |
Page 1 of 1 |
Author: | strzata [ Mon Nov 02, 2020 5:19 pm ] |
Post subject: | PageCount |
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 |
Author: | TH-Soft [ Mon Nov 02, 2020 5:45 pm ] |
Post subject: | Re: PageCount |
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? |
Author: | strzata [ Mon Nov 02, 2020 6:37 pm ] |
Post subject: | Re: PageCount |
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? |
Author: | TH-Soft [ Tue Nov 03, 2020 8:34 am ] |
Post subject: | Re: PageCount |
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... |
Author: | strzata [ Tue Nov 03, 2020 11:49 am ] |
Post subject: | Re: PageCount |
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. |
Author: | TH-Soft [ Wed Nov 04, 2020 7:35 am ] |
Post subject: | Re: PageCount |
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. |
Author: | strzata [ Wed Nov 04, 2020 8:22 am ] |
Post subject: | Re: PageCount |
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 |
Author: | strzata [ Wed Nov 04, 2020 1:00 pm ] |
Post subject: | Re: PageCount |
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. |
Author: | TH-Soft [ Wed Nov 04, 2020 2:09 pm ] |
Post subject: | Re: PageCount |
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))" |
Author: | strzata [ Wed Nov 04, 2020 2:33 pm ] |
Post subject: | Re: PageCount |
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? |
Author: | strzata [ Thu Nov 05, 2020 7:10 am ] |
Post subject: | Re: PageCount |
The function has been rewritten using iTextSharp. Now everything works fine. Thank you so much and very warm greetings Thomas from Norbert |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |