PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:02 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Thu Dec 18, 2008 8:58 pm 
Offline

Joined: Thu Dec 18, 2008 8:48 pm
Posts: 2
I can delete a BMP or JPEG file after use DrawImage function. The files are opened and not closed. I can delete the files after close my program.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jan 07, 2009 9:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
You should be able to delete the file after disposing the document object (and related objects) where you used the images.
Images are cached and therefore locked while in use. That's not a bug, that's a feature.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 8:49 am 
Offline

Joined: Tue Jul 14, 2009 8:39 am
Posts: 2
I have the same problem with the locked images.
I use MigraDoc.DocumentObjectModel.Document and MigraDoc.Rendering.PdfDocumentRenderer to render my pdf.
On these Objects is no .Dispose()-function.

How can I release these lock? I even tried with set the Objects to null and let the GC do the work, but with no success. Any ideas?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 14, 2009 1:39 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Everything worked fine when I tested PDFsharp and MigraDoc using the GDI+ build.

With the WPF build of MigraDoc, the image file was locked and couldn't not be deleted.
I don't have a solution for this problem (I'll put it on the TODO list).

I called:
Code:
pdfRenderer.PdfDocument.Dispose();
pdfRenderer = null;
document = null;

in a test program based on the MigraDoc HelloWorld sample.

In the PDFsharp test program I called:
Code:
File.Copy(jpegSamplePath, "test$$$.jpg", true);
using (XImage image = XImage.FromFile("test$$$.jpg"))
{
  // Left position in point
  double x = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
  gfx.DrawImage(image, x, 0);
}
File.Delete("test$$$.jpg");

The using was all I needed to properly close the file again.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 15, 2010 1:20 pm 
Offline

Joined: Tue Jun 15, 2010 1:14 pm
Posts: 1
Had the same problem with original tif files being locked, tried your suggestion and now it works.
Good explanation here:
http://www.w3enterprises.com/articles/using.aspx

try
{
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());

XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);

//this locks up the source file:
//XImage img = XImage.FromFile(srcFilePath);
//xgr.DrawImage(img, 0, 0);

//this works:
using (XImage img = XImage.FromFile(srcFilePath))
{
xgr.DrawImage(img, 0, 0);
}

doc.Save(destFilePath);
doc.Close();

return true;
}


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 20, 2010 6:32 pm 
Offline

Joined: Mon Dec 20, 2010 6:23 pm
Posts: 4
Hello,

I had the same issue but after using .dispose() and .close() it worked fine.

But for some files (say 1 in every ten files) it still does not release the object and the file does not deleted. When I try to delete it manually, sometimes it gets deleted but somtimes it does not delete giving the following error, "The file cannot be deleted as it is being used by other program."

Here is my code:

Code:
Private Sub ConvertTiffToPdf(ByVal sPath1 As String, ByVal FileName As String)

        Dim aFileName2 As String
        aFileName2 = Path.GetFileNameWithoutExtension(FileName)
        Try
            Dim doc As New PdfDocument()
            Dim bm As New Bitmap(FileName)
            Dim total As Integer = bm.GetFrameCount(FrameDimension.Page)
            'Iterate through multiple Tiff pages
            For k As Int32 = 0 To total - 1
                Dim bm2 As New Bitmap(FileName)
                bm2.SelectActiveFrame(FrameDimension.Page, k)
                Dim image As XImage = XImage.FromGdiPlusImage(bm2)
                Dim page1 As PdfPage = doc.AddPage()
                page1.Size = PageSize.Letter
                page1.Orientation = PageOrientation.Portrait
                'Appends each tiff page to pdf file
                Dim gfx As XGraphics = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append)
                gfx.DrawImage(image, 0, 0, page1.Width, page1.Height)
                doc.Save(sPdfDistPath & aFileName2 & ".pdf")
                gfx.Dispose()
                image.Dispose()
                page1.Close()
                bm2.Dispose()
            Next
            doc.Close()
            bm.Dispose()
            lDocumentsConverted += 1
            LogMessage(Environment.NewLine & SuccessConst & "-----------------= Conversion Finished")
            ' Deletes the Tiff file from the source folder
            File.Delete(FileName)
        Catch
            LogMessage(Environment.NewLine & FailureConst & "Tiff File" & aFileName2 & "Conversion to Pdf Failed")
        End Try


Any suggestion will help thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 30, 2010 1:26 pm 
Offline

Joined: Mon Dec 20, 2010 6:23 pm
Posts: 4
Hello guys,

Please can someone reply to the above post.

The above code works fine but sometimes it blocks files and files do not get deleted.

Thanks for your help


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 31, 2010 2:25 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
I admit I'm guilty of not studying your code, but I suggest wrapping these code blocks with this construct instead of calling dispose, etc. and then see if your issue still happens. The reason I suggest this is that should any exceptions or other unexpected things happen, your resources still get cleaned up for you, otherwise you have to be sure that your close/dispose calls always get made properly...

Using { resourcelist | resourceexpression }
[ statements ]
End Using

The resources are properly disposed/cleaned-up, etc when the End Using is called.

ie: in C# I'd do this:

using ( Bitmap bm = new Bitmap(FileName))
{
// anything in here can use bm
} // bm gets completely cleaned up after this closure is hit.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 03, 2011 8:29 pm 
Offline

Joined: Mon Dec 20, 2010 6:23 pm
Posts: 4
Thank you so much for your reply.

I changed all the dispose with Using statment.

I still have the same issue of files not being deleted.

I checked the tiff files which were not getting deleted. I found out that I am getting some files which have a page missing.

for example: A tiff file has 3 pages. First page has info(image) in it. Second page has info on it. Third page is empty (has no info or image), but it shows as 3 page on the tiff file.
Files like these stop converting further and stop.

In the above code if you see, as it stops converting, it goes directly to handle exception instead of deleting the file.

I am getting error at this line:
bm2.SelectActiveFrame(FrameDimension.Page, k)



Is there a way to solve this issue using pdfsharp code.


thanks


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 04, 2011 6:52 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 339
amitp84 wrote:
I am getting error at this line:
bm2.SelectActiveFrame(FrameDimension.Page, k)
Is there a way to solve this issue using pdfsharp code.

No, the problem is in your code.

Looks as if you need a try before "SelectActiveFrame" and a catch after "gfx.DrawImage".

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 04, 2011 7:18 pm 
Offline

Joined: Wed Nov 11, 2009 9:40 am
Posts: 17
I'm not so strong in VB so my piece of advice may get off-track, but check yourself...

Try to get rid off manual call to Dispose by learning Using, read http://visualbasic.about.com/od/usingvbnet/a/disposeobj.htm.

If you write:
Code:
Using xoxo As ...
End Using


you'll get
Code:
finally { xoxo.Dispose() } (C# syntax)


automatically, for free.

I can imagine you can get locked files in this case: you run into Catch... block. Then you just write something to log but nothing is done to check&dispose whatever was used till you've go an exception.

Hope this helps.

_________________
Regards,
Remigijus Pankevičius


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 04, 2011 7:46 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
Amit,

Now that you've added in the 'using' statements, maybe it's time to repost your current code in a [ Code ] block like you did before so that we can see whether there's something else you're missing.

-Jeff


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 04, 2011 7:53 pm 
Offline

Joined: Mon Dec 20, 2010 6:23 pm
Posts: 4
Hey Jeff

Here is the code. I also made chnage as suggested by "() = > true". Moved the try and catch around.

It works fine now. It converts right and also deletes.
But with moving the try ctach around, the problem is that if there is a currupt page in the tiff file. The code will complete conversion till the pages which are ok and stop and the code will delete the file.


Code:
Private Sub ConvertTiffToPdf(ByVal sPath1 As String, ByVal FileName As String)

        Dim aFileName2 As String
        aFileName2 = Path.GetFileNameWithoutExtension(FileName)
        Try
            Dim doc As New PdfDocument()
            'Dim bm As New Bitmap(FileName)
            Using bm As New Bitmap(FileName)
                Dim total As Integer = bm.GetFrameCount(FrameDimension.Page)
                'Iterate through multiple Tiff pages
                For k As Int32 = 0 To total - 1
                    Using bm2 As New Bitmap(FileName)
                        Dim page1 As PdfPage = doc.AddPage()
                        bm2.SelectActiveFrame(FrameDimension.Page, k)
                        Using image As XImage = XImage.FromGdiPlusImage(bm2)
                            'Dim page1 As PdfPage = doc.AddPage()
                            page1.Size = PageSize.Letter
                            page1.Orientation = PageOrientation.Portrait
                            'Appends each tiff page to pdf file
                            Using gfx As XGraphics = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append)
                                gfx.DrawImage(image, 0, 0, page1.Width, page1.Height)
                                doc.Save(sPdfDistPath & aFileName2 & ".pdf")
                            End Using   'Dispose gfx
                            page1.Close()
                        End Using       'Dispose image
                    End Using   'Diapose bm2
                Next
                doc.Close()
            End Using       'Dispose bm
        Catch
            LogMessage(Environment.NewLine & FailureConst & "Tiff File: (" & aFileName2 & ") Conversion to Pdf Stoped due to Page missing in the tiff file")
        End Try
                   lDocumentsConverted += 1
            LogMessage(Environment.NewLine & SuccessConst & "-----------------= Conversion Finished")
            ' Deletes the Tiff file from the source folder
            File.Delete(FileName)
            End Sub


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 04, 2011 8:40 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
You want to be sure to try/catch/finally, (possibly) AROUND the bm2 using statement to allow you to continue on to the next Tiff image. If it throws in SelectActiveFrame(), you should defer the doc.AddPage() call until after the SelectActiveFrame() call to be sure you've got a good tiff file before adding a page you're not going to write to.

Also, when posting code, be sure to surround your code with the code /code tags shown below so that it retains formatting. I don't read raw VB well and without indentation, I'm hopeless. :lol:

Quote:
[code][/code]


Last edited by jeffhare on Thu Jan 06, 2011 6:33 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 06, 2011 6:06 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 339
amitp84 wrote:
I also made chnage as suggested by "() = > true".

No, you still need a try before "SelectActiveFrame" and the corresponding catch before "next".

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


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

All times are UTC


Who is online

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