PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Unable to open PDF after creating with PDF Sharp
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4312
Page 1 of 1

Author:  awdigrigoli [ Tue Jan 11, 2022 4:15 pm ]
Post subject:  Unable to open PDF after creating with PDF Sharp

Hello,

I get the following message when I attempt to open a PDF that I created with the attached code I got from here.

"Adobe Acrobat could not open "name of file", because it is either not a supported file type or has been damaged..."

I am unable to open from Windows Explorer directly or through my application. The file does open using the Process.Start method as in the code, but the program is just to create for later use so the Start.Process was left in temporarily.
The PDF is shown as 0 KB even though there is a page and name added using this code. Do I need to add more?

Here is the code:
Note: sWorkerFolder is a path to the file and the file name is entered by the user in a text field.

Dim newPDF As New Pdf.PdfDocument
newPDF.Info.Title = txtPDFFileName.Text.Trim
Dim page As Pdf.PdfPage = newPDF.AddPage
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)

Dim pen As XPen = New XPen(XColor.FromArgb(255, 0, 0))

Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
' Draw the text
gfx.DrawString("Hello, " & Environment.UserName & "!", font, XBrushes.Black, New XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.Center)
' Save the document...
Dim sFileName As String = sWorkFolder & txtPDFFileName.Text.Trim & " WC.pdf"
newPDF.Save(sFileName)
Process.Start(sFileName)

Author:  TH-Soft [ Tue Jan 11, 2022 9:52 pm ]
Post subject:  Re: Unable to open PDF after creating with PDF Sharp

awdigrigoli wrote:
The PDF is shown as 0 KB even though there is a page and name added using this code. Do I need to add more?
A PDF created by PDFsharp containing the text "Hello" should be larger than 1 kB, so seeing 0 kB looks definitely wrong.

To me it is a bit unclear what you do and when the error occurs.

Author:  awdigrigoli [ Wed Jan 12, 2022 1:20 pm ]
Post subject:  Re: Unable to open PDF after creating with PDF Sharp

The code works to generate a PDF and if I leave the Process.Start(filename) code in place the file opens and looks good.
However, after I close the file and then go back to view it, the error message I provided is displayed when Acrobat opens. The PDF I created does not reappear.
Is the Save() method from PdfDocument not working properly? I don't think that is the issue, but I not sure why the only bit of code I found online to use PDFSharp to create a PDF is not working.

Author:  awdigrigoli [ Wed Jan 12, 2022 7:32 pm ]
Post subject:  Re: Unable to open PDF after creating with PDF Sharp

I found that I am able to create the file and access it as long as I leave it in the location where it is created. The File.Copy command, not related to PdfSharp is where the issue resides. The copy is somehow corrupting the file.

Author:  TH-Soft [ Thu Jan 13, 2022 8:57 am ]
Post subject:  Re: Unable to open PDF after creating with PDF Sharp

awdigrigoli wrote:
The File.Copy command, not related to PdfSharp is where the issue resides. The copy is somehow corrupting the file.
And this command was not shown in this thread.
Support cases like that made us create the Issue Submission Template.

Thanks for the feedback, issue resolved.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/