我找到了一个解决方案:使用所需的作物值初始化变量
Dim nLeft as integer = 20
Dim nRight as integer = 20
Dim nBoth as integer = 40
Dim nUp as integer = 20
我使用 Ghostscript.NET dll 创建了光栅化对象:我在没有 CustomSwitches 的情况下获取高度和宽度大小页面(“-dPDFFitPage”)
Dim rasterize As Rasterizer.GhostscriptRasterizer = New Rasterizer.GhostscriptRasterizer()
rasterize.Open("PDFPath")
Dim nHeightBased As Integer = rasterize.GetPage(72, 72, 1).Height
Dim nWidthBased As Integer = rasterize.GetPage(72, 72, 1).Width
rasterize.Close()
然后,我使用 CustomSwitches(“-dPDFFitPage”)创建一个新的栅格化以获取高度和宽度大小页面
rasterize = New Rasterizer.GhostscriptRasterizer()
rasterize.CustomSwitches.Add("-dPDFFitPage")
rasterize.Open(cPathPDF)
Dim nHeightBound As Integer = nHeightBased - rasterize.GetPage(72, 72, 1).Height
Dim nWidthBound As Integer = nWidthBased - rasterize.GetPage(72, 72, 1).Width
Dim nWidthPDF As Integer = rasterize.GetPage(72, 72, 1).Width
Dim nHeightPDF As Integer = rasterize.GetPage(72, 72, 1).Height
rasterize.Close()
Dim nWidthCrop As Integer = (nWidthPDF + nWidthBound) - (nLeft + nRight)
Dim nHeightCrop As Integer = (nHeightPDF + (nHeightBound / 2)) - (nBoth + nUp)
CropPDF("PathPDF", nLeft, nBoth, nWidthCrop, nHeightCrop)
我创建函数 CropPDF :
现在我们采用 gswinc32.exe 或 gswinc64.exe 和 .dll 并在我的示例中复制/粘贴到新路径中,我使用“PathEXE”
Public Function CropPDF(ByVal cPathPDF As String, ByVal nLeft As Integer, ByVal nBoth As Integer, ByVal nWidthCrop As Integer, ByVal nHeightCrop As Integer)
Dim cPathWithoutExtension = Path.GetDirectoryName("PDFPath") & "/" & Path.GetFileNameWithoutExtension("PDFPath")
Dim gsPath As String = HttpContext.Current.Server.MapPath("PathEXE")
Dim gsArgsList As List(Of String) = New List(Of String)
gsArgsList.Add("-sDEVICE=pdfwrite")
gsArgsList.Add(" -dFIXEDMEDIA")
gsArgsList.Add(" -dDEVICEWIDTHPOINTS=" & nWidthCrop)
gsArgsList.Add(" -dDEVICEHEIGHTPOINTS=" & nHeightCrop)
gsArgsList.Add(" -o """ & cPathWithoutExtension & "_Crop.pdf""")
gsArgsList.Add(" -c ""<</Install {-" & nLeft & " -" & nBoth & " translate} >> setpagedevice """)
gsArgsList.Add(" -f " & cPathPDF)
Dim gsArgs As String = String.Join(Nothing, gsArgsList)
Process.Start(gsPath, gsArgs).WaitForExit()
Dim OFI As FileInfo = New FileInfo(cPathPDF)
OFI.Delete()
Dim DestOFI As FileInfo = New FileInfo(cPathWithoutExtension & "_Crop.pdf")
DestOFI.MoveTo(cPathPDF)
Return cPath
End Function
现在与 nLeft nRight nBoth nUp 作物完美合作,希望它能帮助一些人:D