PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
PDFcreation out of of pics(jpg,tif) time and mem consuming https://forum.pdfsharp.net/viewtopic.php?f=2&t=1920 |
Page 1 of 1 |
Author: | jonesfxr [ Thu Feb 23, 2012 11:02 am ] |
Post subject: | PDFcreation out of of pics(jpg,tif) time and mem consuming |
Hi, I'm creating PDFs out of images(tif,jpg), where every image is on it's own page. While it's working fine for a small number of images, it's very time and memory consuming when it get's to a huge number of images meaning 200 pages and more, which isn't unusual here. Size of images is between 100 and 450KB (so far I've seen). Size of each page in PDF should be A4, if size of image is larger, page size will be adjusted. Am I doing something wrong or is there a better way to achieve the target? Any memory cleaning to be done at end of procedure? Here is the sample code: Code: Sub PDFCreate() Dim i As Integer Dim vPDFDoc As PdfDocument Dim vPDFPage As PdfPage Dim vXImg As XImage Dim vXGr As XGraphics Dim vslDebugStep As String vslDebugStep = "CreatePDF" vPDFDoc = New PdfDocument() For i = 1 To 274 Console.WriteLine(i.ToString) vPDFPage = New PdfPage() vPDFPage.Size = PageSize.A4 vXImg = XImage.FromFile(String.Format("1898109\1898109_{0}.FOB", i.ToString)) vXImg.Interpolate = False 'adjust PageSize If (vXImg.PointHeight > vPDFPage.Height.Value) Then vPDFPage.Height = vXImg.PointHeight If (vXImg.PointWidth > vPDFPage.Width.Value) Then vPDFPage.Width = vXImg.PointWidth vPDFDoc.Pages.Add(vPDFPage) vXGr = XGraphics.FromPdfPage(vPDFDoc.Pages(i - 1)) vXGr.DrawImage(vXImg, 0, 0) Next vslDebugStep = "SavePDF" vPDFDoc.Save("test.pdf") vPDFDoc.Close() End Sub BTW: using PdfSharp.dll Version 1.31.1789.0 GDI build thanks in advance for your help jones |
Author: | jonesfxr [ Thu Feb 23, 2012 2:57 pm ] |
Post subject: | Re: PDFcreation out of of pics(jpg,tif) time and mem consuming |
actually I rebuild the code and create temporary PDF with 20 pages. These temp files will then be concatenated to the final one (using code in concatenate sample). Much faster now. But I still don't like the idea to access file system that often and cleaning up afterwards. Still looking for a more elegant way. Any suggestion? regards jones |
Author: | jonesfxr [ Sun Feb 26, 2012 5:12 pm ] |
Post subject: | Re: PDFcreation out of of pics(jpg,tif) time and mem consuming |
Hi, I switched to WPF build and moved creating image on PDFPage to a function. Seems like my issues are gone (or at least reduced). Nevertheless I appreciate any tips and comments on my code, on how to tune it up or do it smarter. Code: ... Dim vPDFDoc As PdfDocument Dim vPDFPage As PdfPage Dim vPDFDocTemp As PdfDocument vPDFDoc = New PdfDocument() For i = 1 To nPages sPageName = mImageManager.GetPageFileName(i, oDoc) 'external function to determine filename of image vPDFDocTemp = PdfReader.Open(GetPDFDoc(sPageName), PdfDocumentOpenMode.Import) vPDFPage = vPDFDocTemp.Pages(0) vPDFDoc.Pages.Add(vPDFPage) Next vPDFDoc.Save(String.Format("{0}\{1}_000001_{2}.pdf", mExportPath, vDocId, mBatchID)) vPDFDoc.Close() vPDFDoc.Dispose() ... ... Private Function GetPDFDoc(ByVal sPageName As String) As MemoryStream Dim vReturnStream As New MemoryStream Dim vPDFDoc As New PdfDocument Dim vPDFPage As New PdfPage Dim vXImg As XImage Dim vXGr As XGraphics Dim vslDebugStep As String Try vPDFPage.Size = PageSize.A4 vXImg = XImage.FromFile(sPageName) vXImg.Interpolate = False 'adjust PageSize If (vXImg.PointHeight > vPDFPage.Height.Value) Then vPDFPage.Height = vXImg.PointHeight If (vXImg.PointWidth > vPDFPage.Width.Value) Then vPDFPage.Width = vXImg.PointWidth vPDFDoc.Pages.Add(vPDFPage) vXGr = XGraphics.FromPdfPage(vPDFPage) vXGr.DrawImage(vXImg, 0, 0) vPDFDoc.Save(vReturnStream, False) Catch ex As Exception Throw Finally vPDFDoc.Close() vPDFDoc.Dispose() vXImg.Dispose() vXGr.Dispose() GetPDFDoc = vReturnStream End Try End Function regards jones |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |