使用与PDFSharp重用的语句变量

时间:2013-12-01 10:34:52

标签: .net vb.net using pdfsharp

以下代码使用PDFSharp将pdf文档的页面拆分为小于A4的页面和大于A3的页面:

''' <summary>
''' Process the list of pdfs
''' </summary>
Public Sub ProcessPdfs()

    Dim tempPath As String

    ' Code omitted

    ' Generate a temporary path in case pdfs need to be saved
    If String.IsNullOrEmpty(Me.tempFolder) OrElse Not Directory.Exists(Me.tempFolder) Then
        tempFolder = Path.GetTempPath()
    End If
    tempPath = Path.Combine(Me.tempFolder, Path.GetRandomFileName + ".pdf")

    ' Loop through the pages of the pdfs and process each page in turn. Processing involves
    ' determining the size of the page, then shrinking, adding the footer and then adding to
    ' the appropriate output pdf
    For Each referenceNumber As String In Me.Pdfs.Keys
        For Each pdf As PdfDocument In Me.Pdfs(referenceNumber)
            ' Save the pdf to disk for PDFSharp to be able to read it properly
            If String.IsNullOrEmpty(pdf.FullPath) Then
                pdf.Save(tempPath)
                pdf = PdfReader.Open(tempPath)
            End If
            For Each page As PdfPage In pdf.Pages

                ' Code omitted

                Select Case pageArea
                    Case Is <= a4PageArea
                        Call AddPage(referenceNumber, pdf, page, PageSize.A4)
                    Case Else
                        Call AddPage(referenceNumber, pdf, page, PageSize.A3)
                End Select
            Next
        Next
    Next

    ' Code omitted

    ' Delete temporary pdfs if there are any
    If File.Exists(tempPath) Then
        File.Delete(tempPath)
    End If

End Sub

''' <summary>
''' Add the specified page to the specified output document
''' </summary>
''' <returns>The page which was added to the output pdf</returns>
Private Function AddPage(ByVal ReferenceNumber As String, ByVal ParentPdf As PdfDocument, ByVal ParentPdfPage As PdfPage, ByVal PageSize As PageSize) As PdfPage

    ' Code omitted

    ' Copy the specified page onto thew newly created page
    Using parentForm As XPdfForm = XPdfForm.FromFile(ParentPdf.FullPath)
        parentForm.PageIndex = ParentPdf.Pages.Cast(Of PdfPage)().ToList().IndexOf(ParentPdfPage)
        scaleFactor = 1
        ' Create PdfSharp graphics object with which to write onto the page
        Using graphics As XGraphics = XGraphics.FromPdfPage(outputPdfPage)
            graphics.SmoothingMode = XSmoothingMode.HighQuality

            ' Code omitted

            ' Draw the page
            Call graphics.DrawImage(parentForm, targetRect)
        End Using
    End Using

    Return outputPdfPage

End Function

这是做一个pdf,读取esch页面,然后缩放它,使其适合要打印它的页面的大小。

PDFSharp无法打开在Adobe v6中创建的文档,因此我使用iTextSharp在PDFSharp可以打开的版本中重建pdf。这些PDF在内存中重建,由于某种原因,需要将它们写入磁盘,以便PDFSharp正确处理它们。

ProcessPdfs()中,我检查pdf是否有物理路径,如果不是,我将其保存在临时位置。

我发现的问题是AddPage()似乎一直在使用相同的pdf。我检查了在磁盘上创建的临时pdf文件,它们是正确的,即每次都不同。

但是XPdfForm.FromFile(ParentPdf.FullPath)在第一个using语句中加载的文件永远不会改变。 就好像代码意识到文件路径没有改变,所以决定不重新加载文件。

我认为使用using语句将确保变量将在末尾处理掉,因此每次都会重新重新加载文件。我误会了吗?或者这里发生了什么?

顺便说一下,我通过将每个pdf文件保存在不同的文件名下来解决这个问题。这就是为什么我认为每次根据文件名...

重复使用using块中的变量

1 个答案:

答案 0 :(得分:0)

XPdfForm在内部缓存文档 - 文件名是密钥。如果您重新使用新文档的文件名,将使用旧的缓存文档。

缓存是线程本地的。

所以这不是一个错误,它是一个功能。

应该可以使用流而不是文件。