任何方法可以更快地将图像转换为PDF

时间:2012-01-26 22:48:17

标签: c# .net vb.net

我目前有一段代码,它会生成一些我生成的图像,并使用PDFSharp将它们转换为PDF文件。据我所知,代码工作得非常好,但每页大约需要4到6秒才能进行转换。我只是想知道是否有更快的方法来进行这种转换。我尝试过使用iTextSharp,但除了仍然花费相同的时间外,它还没有正确渲染图像。下面是我现在用来创建PDF的代码。

Private Sub OnThread_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim _thread As BackgroundWorker = DirectCast(sender, BackgroundWorker)
    Dim _arguments As Object() = DirectCast(e.Argument, Object())
    Dim _pages As List(Of Image) = DirectCast(_arguments(0), List(Of Image))
    Dim _filePath As String = DirectCast(_arguments(1), String)

    'this code works, but is pretty slow (about 4 - 5 seconds per page)
    Dim _pdfDocument As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument()

    For i As Integer = 0 To (_pages.Count - 1)
        Dim _page As Image = _pages(i)
        Dim _pdfPage As PdfSharp.Pdf.PdfPage = _pdfDocument.Pages.Add()
        Dim _pdfCanvas As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(_pdfPage)
        Dim _pdfImage As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromGdiPlusImage(_page)
        _pdfCanvas.DrawImage(_pdfImage, New Rectangle(0, 0, Convert.ToInt32(_pdfPage.Width.Point), Convert.ToInt32(_pdfPage.Height.Point)))
        _pdfImage.Dispose()
        _pdfCanvas.Dispose()
        _thread.ReportProgress(0, New Integer() {i + 1, _pages.Count})
    Next

    _pdfDocument.Save(_filePath)
    _pdfDocument.Close()
End Sub

对此问题的任何解决方案都将不胜感激。如果你知道的话,我也欢迎C#例子。如果我在使用秒表测试速度时会有所帮助,那么下面的代码就是花费所有时间来完成的代码。

_pdfCanvas.DrawImage(_pdfImage, New Rectangle(0, 0, Convert.ToInt32(_pdfPage.Width.Point), Convert.ToInt32(_pdfPage.Height.Point)))

0 个答案:

没有答案