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

PDFDocument.Save possibly failing.
https://forum.pdfsharp.net/viewtopic.php?f=2&t=672
Page 1 of 1

Author:  carter [ Tue Mar 10, 2009 6:33 pm ]
Post subject:  PDFDocument.Save possibly failing.

I'm running pdfsharp on a few terminals and about 5-10 times a day i'm finding 0kb pdf files that are being saved. The logic i have is inside a try/catch and my software isn't reporting any errors. I checked out the source for the pdfdocument object and i found under the public void Save(Stream stream, bool closeStream) there is just a try/finally. So it would seem that if there is any execption whatsoever then it would get ignored. I could check the length of the stream to check to see if the stream has been successfully written to, but what if the write fails 4kb through? Also, i'd like to know what the exception is so i could find out whether it is a permissions problem, os problem, or software problem. Below is the code i am using.

Code:
      FileStream ^fs = File::Create(sFileNamePath);
      fs->SetLength(0);

      PdfDocument ^pdfDocument = gcnew PdfDocument(fs);

      for each (Image ^img in arrImages)
      {
         PdfPage ^page = pdfDocument->AddPage();
         page->Orientation = PdfSharp::PageOrientation::Landscape;

         XGraphics ^graphics = XGraphics::FromPdfPage(page);
         XImage ^xImage = XImage::FromGdiPlusImage(img);

         graphics->DrawImage(xImage, Point(10, 10));
      }

      pdfDocument->Save(fs, false);

Author:  Thomas Hoevel [ Wed Mar 11, 2009 9:06 am ]
Post subject:  Re: PDFDocument.Save possibly failing.

Hi!
carter wrote:
I checked out the source for the pdfdocument object and i found under the public void Save(Stream stream, bool closeStream) there is just a try/finally. So it would seem that if there is any execption whatsoever then it would get ignored.

I don't think that exceptions are ignored by try/finally without catch.
Could be tested by adding something like
Code:
catch/*(Exception ex)*/
{
  throw;
}

to the try/finally statements.

Author:  carter [ Wed Mar 11, 2009 3:23 pm ]
Post subject:  Re: PDFDocument.Save possibly failing.

Thomas Hoevel wrote:
I don't think that exceptions are ignored by try/finally without catch.


You're right. I thought for sure that try/finally was the equivalent to "On Error Resume.." in vb. I'll do more testing and see what i can come up with.

Author:  kdlc [ Thu Jul 30, 2009 5:18 pm ]
Post subject:  Re: PDFDocument.Save possibly failing.

I've seen this kind of behavior before in Vista. It happens if the PDF file you're trying to save is in use by another process like adobe or the Vista Preview Pane.

kinda scary if your users are working in a really important PDF file and it gets wiped out when saving changes ...

Author:  kdlc [ Thu Jul 30, 2009 6:24 pm ]
Post subject:  Re: PDFDocument.Save possibly failing.

Here's another example:
Dim sFileName As String = "C:\Documents and Settings\Marcelo\My Documents\VB_2005_color.pdf"
Dim xDoc As PdfSharp.Pdf.PdfDocument = PdfReader.Open(sFileName, PdfDocumentOpenMode.InformationOnly)
With xDoc
.Info.Author = Me.txtAuthor.Text
.Info.Subject = Me.txtSubject.Text
.Info.Keywords = Me.txtKeywords.Text
.Save(sFileName) 'Should get an error "Cannot save a PDF document with no pages."
End With
xDoc = Nothing

I know that the Open mode should be Modify mode, but still, It shouldn't wipe the file ...

Author:  Thomas Hoevel [ Mon Aug 03, 2009 11:07 am ]
Post subject:  Re: PDFDocument.Save possibly failing.

kdlc wrote:
I know that the Open mode should be Modify mode, but still, It shouldn't wipe the file ...

Only call Save(filename) if you don't need the file anymore.
A developer should always have backups ...

Author:  Thomas Hoevel [ Mon Aug 03, 2009 11:26 am ]
Post subject:  Re: PDFDocument.Save possibly failing.

kdlc wrote:
I've seen this kind of behavior before in Vista. It happens if the PDF file you're trying to save is in use by another process like adobe or the Vista Preview Pane.
kinda scary if your users are working in a really important PDF file and it gets wiped out when saving changes ...

With every Windows version, Adobe Reader locks the file it is showing.
It won't get wiped because it's locked.

It's up to your application to retry Save() if it failed because of a lock.
Like Office does: if Save fails, it automatically displays the SaveAs dialog.

Or have your application lock the PDF file. This prevents Adobe Reader to lock it and saving changes will succeed.
This can only happen if you don't lock the file allowing your users to lock the file by opening it with Reader while your application has it in memory and allows modifications.
Not scary if SaveAs jumps in if Save failed.

No need to be scared if you let your users work with carefully tested applications ...

Author:  kdlc [ Wed Aug 05, 2009 5:05 am ]
Post subject:  Re: PDFDocument.Save possibly failing.

Thanks for your replies.

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