PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Sources files locked after PDF processes
http://forum.pdfsharp.de/viewtopic.php?f=2&t=1211
Page 1 of 1

Author:  ChrisX [ Fri Jun 04, 2010 7:52 pm ]
Post subject:  Sources files locked after PDF processes

I have some code to convert a series of Tiffs (multiple files, not a multipage Tiff) into a single pdf. The code works fine to do this; however it appears to be locking up access to the Tiffs even after it is done. So when I go to delete the tiff files I get an IOException stating that the process cannot access the file because is being used by another process. I am fairly confident it is the pdf conversion that is doing this as if I skip that step and just delete the tiffs everything works fine.

The code for the conversion is as follows. Any help would be appreciated.

Code:
        public static void ConvertTifFiles(ArrayList sourceFiles, string destination)
        {
            PdfDocument doc = new PdfDocument();
            for (int i = 0; i < sourceFiles.Count; i++)
            {
                string fileName = Path.GetFileNameWithoutExtension((string)sourceFiles[i]);
                string source = (string)sourceFiles[i];
                doc.Pages.Add(new PdfPage());
                XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i], XGraphicsPdfPageOptions.Append);
                XImage img = XImage.FromFile(source);
                xgr.DrawImage(img, 0, 0);
            }
            doc.Save(destination);

doc.Close();
}

Author:  Thomas Hoevel [ Mon Jun 07, 2010 8:40 am ]
Post subject:  Re: Sources files locked after PDF processes

Try the GDI+ build.

Author:  mxmissile [ Thu Nov 07, 2013 2:46 pm ]
Post subject:  Re: Sources files locked after PDF processes

Necro I know, but the GDI build does not handle CMYK tif's so I am stuck with the WPF build. Is there another work around for this? My code almost exactly as the OP. It's leaving locks on the Tif files.

Author:  Thomas Hoevel [ Thu Nov 07, 2013 3:21 pm ]
Post subject:  Re: Sources files locked after PDF processes

XImage implements IDisposable, but the sample code given above does not call Dispose() nor frees the XImage. This could make a difference.

Or use streams as shown here:
http://stackoverflow.com/a/19839642/162529

Author:  mxmissile [ Thu Nov 07, 2013 4:10 pm ]
Post subject:  Re: Sources files locked after PDF processes

XImage.FromFile does not take a Stream parameter. By the way, that stackoverflow question is mine.

I've narrowed it down to this code, running this code *alone* will leave the lock.

using (var image = XImage.FromFile(path))
{}

Author:  Thomas Hoevel [ Thu Nov 07, 2013 4:26 pm ]
Post subject:  Re: Sources files locked after PDF processes

mxmissile wrote:
XImage.FromFile does not take a Stream parameter.
It does - but better use XImage.FromStream().

mxmissile wrote:
By the way, that stackoverflow question is mine.
I know - but others may also read this thread.

Author:  mxmissile [ Thu Nov 07, 2013 5:31 pm ]
Post subject:  Re: Sources files locked after PDF processes

I'm still not seeing XImage.FromStream().

Looking here: https://pdfsharp.codeplex.com/SourceCon ... /XImage.cs I'm not seeing it, what am I missing? I would love to use that method.

Author:  Thomas Hoevel [ Thu Nov 07, 2013 5:54 pm ]
Post subject:  Re: Sources files locked after PDF processes

mxmissile wrote:
I'm still not seeing XImage.FromStream().
Sorry, my fault. :oops:
Will be in the next public release.

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