私はしばらくこれに苦労していたので、解決策を打ち破ることができるかもしれない教祖のためにこれを投稿すると思いました.
コピーしたい PDF ドキュメントがあり、コピーするときにページのサイズをカスタム サイズに変更したいと考えています。たとえば、元のドキュメントのページ サイズは 8 1/2 x 16 です。新しいドキュメントは 8 1/2 x 22 である必要があります。新しいドキュメントの 8 1/2 は元の幅から得られます。22 は固定 (定数) です。問題を複雑にしているのは、新しいドキュメントでは古いドキュメントのコンテンツをページの中央に配置する必要があることです。したがって、新しいドキュメントでは、元の 16 インチが中央に配置されるように、上に 3 インチ、下に 3 インチの余白が必要です。幅は関係ありません。高さだけを中央に配置する必要があります。
iTextSharp のコード フラグメントを探しています。
機能する次のソリューションを作成しました。誰かの助けになることを期待してコミュニティに投稿します。ソリューションはVB.NETにあります。
使用するには、resizeAndRotateDocument を 2 つのブール値で呼び出します。"rotate" - 各ページを 90 度回転します - true、false。「サイズ変更」 - ページを 22 インチの高さにサイズ変更し、上部/下部から中央ページにマージンを追加します - true/false。また、2 つの文字列: 処理する入力 pdf ファイルの名前と、作成する出力 pdf ファイルの名前。
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
ブルーノ、ありがとう!