PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 19, 2024 7:29 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Feb 23, 2012 11:02 am 
Offline

Joined: Thu Feb 23, 2012 10:47 am
Posts: 6
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


Last edited by jonesfxr on Thu Feb 23, 2012 3:01 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 23, 2012 2:57 pm 
Offline

Joined: Thu Feb 23, 2012 10:47 am
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 26, 2012 5:12 pm 
Offline

Joined: Thu Feb 23, 2012 10:47 am
Posts: 6
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


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

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 35 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