iTextSharp的拷贝文件,调整为自定义大小,和中心 [英] iTextSharp copy document, resize to custom size, and center

查看:158
本文介绍了iTextSharp的拷贝文件,调整为自定义大小,和中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与此一段时间挣扎这么想过我会张贴此对谁也许能敲出一个解决方案的大师。

I've been struggling with this for awhile so thought I would post this for any gurus who might be able to knock out a solution.

我有一个PDF文件,我想复制,当我把它复制,我想调整页面自定义尺寸。例如,原始文档具有8 1/2×16。新的文档需要由22在新文件的8 1/2来自原稿的宽度是8 1/2页大小; 22是固定的(恒定)。什么复杂的问题是,新的文档需要具有居中页面上的旧文档的内容。因此,新的文档中就需要有3英寸顶部3英寸底部使原来16英寸为中心的利润率。宽度并不重要,只是高度需要被居中。

I have a PDF document that I'd like to copy and when I copy it I would like to resize the pages to custom size. For example, the original document has a page size of 8 1/2 x 16. The new document needs to be 8 1/2 by 22. The 8 1/2 in the new doc comes from the width of the original; the 22 is fixed (constant). What complicates the issue is that the new doc needs to have the old doc contents centered on the page. So in the new document it would need to have margins of 3 inches top, 3 inches bottom so that the original 16 inches was centered. Width doesn't matter, just the height needs to be centered.

创建了以下解决方案,它的工作原理。回发到社区中希望它可以帮助别人。解决办法是在VB.NET。

Created the following solution which works. Posting back to the community in the hopes it may help someone. Solution is in VB.NET.

要使用电话resizeAndRotateDocument与2布尔值。 旋转 - 旋转每个页面90度 - 真的,假的。和调整 - 调整大小的页面,一个22英寸的身高与利润率加入TOP / bottome为中心页面 - 真/假。而且两个字符串:输入PDF文件的处理的名称和输出PDF文件创建的名称

To use call resizeAndRotateDocument with 2 booleans. "rotate" - rotate each page 90 degrees - true, false. and "resize" - resizes the page to a 22 inch height with margins added to top/bottome to center page - true/false. And also 2 strings: the name of the input pdf file to process and the name of the output pdf file to create.

    Private Sub AdjustMediaBoxSize(ByRef mediaBoxSize As PdfArray, ByVal rotationIsPresent As Boolean)

    Const Fixed_22_Inch_Page As Integer = 22 * 72

    Dim ll_x, ll_y, ur_x, ur_y As Integer
    Dim heightDiff, heightAdjustment As Integer
    Dim new_ll_x, new_ll_y, new_ur_x, new_ur_y As Integer

    ' Read current coordinates of the Mediabox
    ll_x = mediaBoxSize(0).ToString
    ll_y = mediaBoxSize(1).ToString

    ur_x = mediaBoxSize(2).ToString
    ur_y = mediaBoxSize(3).ToString

    ' Figure out the height difference and the adjustment factor.
    If rotationIsPresent = True Then
        heightDiff = Fixed_22_Inch_Page - ur_x
    Else
        heightDiff = Fixed_22_Inch_Page - ur_y
    End If

    ' adjustment needed to top and to bottom
    heightAdjustment = heightDiff / 2

    ' Apply the adjustments; only use the ones we need.
    new_ll_x = ll_x - heightAdjustment
    new_ur_x = ur_x + heightAdjustment

    new_ll_y = ll_y - heightAdjustment
    new_ur_y = ur_y + heightAdjustment

    If rotationIsPresent = True Then
        mediaBoxSize(0) = New iTextSharp.text.pdf.PdfNumber(new_ll_x)
        mediaBoxSize(2) = New iTextSharp.text.pdf.PdfNumber(new_ur_x)
    Else
        ' Make the adjustment. Value is passed back by reference.
        mediaBoxSize(1) = New iTextSharp.text.pdf.PdfNumber(new_ll_y)
        mediaBoxSize(3) = New iTextSharp.text.pdf.PdfNumber(new_ur_y)
    End If

End Sub

Private Sub resizeAndRotateDocument(ByVal rotate As Boolean, ByVal resize As Boolean, ByVal inPDF As String, ByVal outPDF As String)

    Const desiredRot As Integer = 90

    Dim reader As New PdfReader(inPDF)
    Dim pageCount As Integer = reader.NumberOfPages

    Dim pageDict As PdfDictionary
    Dim mediabox As New PdfArray
    Dim cropbox As New PdfArray

    For i As Integer = 1 To pageCount

        pageDict = reader.GetPageN(i)

        ' Rotation is hard coded to 90 degrees.
        If rotate = True Then
            pageDict.Put(PdfName.ROTATE, New PdfNumber(desiredRot))
        End If

        If resize = True Then
            ' Read the current mediabox dimensions. 
            ' Then adjust the size to scale up to 22 inches adjusting for rotation if necessary.
            ' Finally, write the updated mediabox back to the dictionary -> to the reader.

            mediabox = pageDict.GetAsArray(PdfName.MEDIABOX)
            AdjustMediaBoxSize(mediabox, rotate)
            pageDict.Put(PdfName.MEDIABOX, mediabox)

        End If

        ' Since the PDFs are created in-house, they should NEVER contain a cropbox.
        cropbox = pageDict.GetAsArray(PdfName.CROPBOX)
        If cropbox IsNot Nothing Then
            MsgBox("Error ... found a valid cropbox !!!")
        End If

    Next

    ' Use the stamper to create and write the pdf output file.
    Dim pdfStamper As New PdfStamper(reader, New FileStream(outPDF, FileMode.Create))
    pdfStamper.Close()

End Sub

和再次感谢布鲁诺!

推荐答案

看看在从我的书RotatePages 的例子。在 ManipulatePdf()的方法,我循环一翻,我走的页字典,我更改 /旋转键将页面旋转。这不是你所需要的,但原理是相似的。

Take a look at the RotatePages example from my book. In the ManipulatePdf() method, I loop over the pages, I take the page dictionary, and I change the /Rotate key to rotate the page. That's not what you need, but the principle is similar.

您需要获得 /媒体框和<$从页字典C $ C> /裁剪框值:

PdfArray mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
PdfArray cropbox = pageDict.getAsArray(PdfName.CROPBOX);

在很多情况下,裁剪框将 。在这种情况下,你可以放心地忽略它。

In many cases, cropbox will be null in which case you can safely ignore it.

您已经通过页16寸测量8 1/2,所以我想媒体框将是一个数组,看起来像这样: [0 0 612 1152]

You have pages measuring 8 1/2 by 16 inch, so I guess mediabox will be an array that looks like this: [ 0 0 612 1152 ]

的零点是左下角的坐标。在右上角有坐标X = 612(72点¯x8.5)和y = 1152(72×16)。

The zeros are the coordinates of the lower-left corner. The upper-right corner has the coordinate x = 612 (72 x 8.5) and y = 1152 (72 x 16).

现在,你需要更换/媒体框项一个类似的数组,你从第二个值中减去216(72×3),并添加216到第四位。换句话说:你会用下列阵列结束 [0 -216 612 1368] 。替换 /媒体框(和 /裁剪框如果有的话)这种改变的阵列以同样的方式我更换了 /旋转键,你有你的示例代码。

Now you need to replace the /MediaBox entry with a similar array where you subtract 216 (72 x 3) from the second value and add 216 to the fourth. In other words: you'll end up with the following array [ 0 -216 612 1368 ]. Replace the /MediaBox (and the /CropBox if present) with such an altered array the same way I replaced the /Rotate key, and you have your sample code.

我也不会,如果我是你用绝对值,我想减去216无论从任何值出现在你的页字典在数组中查找

I wouldn't use absolute values if I were you, I'd subtract 216 from whatever value is present in the array you find in the page dictionary.

声明:我是原作者iText的,以及作为的iText在行动的书籍从中例如旋转拍摄。我是一个Java开发人员;我付出了其他开发人员端口的例子,从我的书到C#。请不要问我写出来你的例子在C#中,因为我从来没有写任何C#代码,只有Java代码。我敢肯定,你就可以编写会见在这个岗位所提供的资料您需要的代码。

Disclaimer: I'm the original author of iText, as well as of the "iText in Action" books from which the rotate example was taken. I'm a Java developer; I paid other developers to port the examples from my book to C#. Please don't ask me to write out your example in C# because I've never written any C# code, only Java code. I'm sure you'll be able to write the code that meets your needs with the information given in this post.

这篇关于iTextSharp的拷贝文件,调整为自定义大小,和中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆