调整现有pdfpage的大小不是以居中为中心

时间:2015-10-17 06:43:03

标签: pdf resize pdfsharp

所有

我正在尝试调整现有PDF文件的大小,但结果没有居中,我做错了什么?我使用的是PDFsharp,请参阅下面的代码:

Private Sub ResizePDF(sourcePDF As String, outPDF As String)
    Dim form As XPdfForm = XPdfForm.FromFile(sourcePDF)
    Dim doc2 As New PdfSharp.Pdf.PdfDocument()
    doc2.Options.CompressContentStreams = True
    doc2.Options.ColorMode = PdfSharp.Pdf.PdfColorMode.Rgb
    For pagenumber As Integer = 1 To (form.PageCount + 1) - 1
        Dim page As PdfSharp.Pdf.PdfPage = doc2.Pages.Add()
        form.PageNumber = pagenumber
        page.Width = form.PixelWidth + 15.36
        page.Height = form.PixelHeight + 40.32
        Dim graph As XGraphics = XGraphics.FromPdfPage(page)
        Dim rect As New XRect(0, 0, page.Width, page.Height)
        graph.DrawImage(form, rect)
    Next
    If IO.Directory.Exists(IO.Path.GetDirectoryName(outPDF)) = False Then IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(outPDF))
    doc2.Save(outPDF)
End Sub

输入文件:https://www.dropbox.com/s/po87x8c44r90py2/HFD_20151007_0_001_061.pdf?dl=0

输出文件:https://www.dropbox.com/s/pt5pcyfayri6ory/HFD_20151007_0_001_061_output.pdf?dl=0

1 个答案:

答案 0 :(得分:0)

我们为您提供了一种解决方法:请拨打graph.DrawImage(form, rect),而不是致电graph.DrawImage(form, rect.X - form.Page.MediaBox.X1, rect.Y + form.Page.MediaBox.Y1, rect.Width, rect.Height) graph.DrawImage(form, rect)
我不确定这是否是有效的VB.NET,但它适用于C#,并且适用于VB.NET,几乎没有变化。

这是PDFsharp 1.50 beta 2及所有早期版本所需的解决方法。

切换到PDFsharp 1.50 beta 3或更高版本(尚未提供)时,您将不得不再次使用初始代码{{1}}。