PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 10:01 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Mar 10, 2009 6:33 pm 
Offline

Joined: Tue Mar 10, 2009 6:12 pm
Posts: 2
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);


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 11, 2009 9:06 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 11, 2009 3:23 pm 
Offline

Joined: Tue Mar 10, 2009 6:12 pm
Posts: 2
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 30, 2009 5:18 pm 
Offline

Joined: Thu Jul 30, 2009 5:09 pm
Posts: 4
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 ...


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 30, 2009 6:24 pm 
Offline

Joined: Thu Jul 30, 2009 5:09 pm
Posts: 4
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 ...


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 03, 2009 11:07 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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 ...

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 03, 2009 11:26 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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 ...

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 05, 2009 5:05 am 
Offline

Joined: Thu Jul 30, 2009 5:09 pm
Posts: 4
Thanks for your replies.


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

All times are UTC


Who is online

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