ghostscript PDF 到 Png 裁剪 x 和 y

ghostscript PDF to Png crop x and y

我在 vb.net 中使用 ghostscript 将 PDF 转换为 PNG 当我裁剪我的 pdf 然后我将其转换为 png 但 ghostscript 在我的图片中保持 x 和 y 位置裁剪。

我在 cmd 中使用 gswin64.exe 时解决了这个问题:-c "<</Install {-48 -87 translate}>> setpagedevice"

但是使用 dll Ghostcript.NET 当我在我的代码中添加此命令时:oGSImage.CustomSwitches.Add("-c ""<</Install {-48 -87 translate}>> setpagedevice""") 我有错误消息

Ghostscript.NET.GhostscriptAPICallException: An error occured when call to 'gsapi_init_with_args' is made: -100

我的设备是pngAlpha,如果有人能帮助我:)

那个特定的参数从命令行执行 PostScript,它并不是严格意义上的 'switch'。把它放在错误的地方,它不会工作,解释器将退出。我的猜测是(或类似的)正在发生的事情,解释器没有按照它想要的方式提供数据。

您可以将 -c "" -f 之间的文本放入一个文本文件,然后 运行 该文件 然后在命令行上的 PDF 文件 (即作为参数)如果 Ghostscript.NET 让你这样做。

我找到了解决办法: 使用所需的裁剪值初始化变量

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 :

Now we take gswinc32.exe or gswinc64.exe and .dll and copy/paste in a new path in my example i use "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