ghostscript PDF到Png裁剪x和y

时间:2018-06-13 09:45:15

标签: c# vb.net ghostscript.net

我使用ghostscript将我的转换PDF转换为PNG在vb.net中当我裁剪我的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:调用&#39; gsapi_init_with_args&#39;时发生错误制作:-100

我的设备是pngAlpha,如果有人可以帮助我的话:)。

2 个答案:

答案 0 :(得分:0)

该特定参数从命令行执行PostScript,它不是严格意义上的&#39;开关&#39;因此。把它放在错误的地方并且它不会工作,解释器将退出。我的猜测是(或类似的)正在发生的事情,口译员没有按照自己想要的方式提供数据。

你可以把文字放在-c&#34;&#34; -f到文本文件中然后在命令行上运行该文件然后运行PDF文件(即作为参数),如果Ghostscript.NET允许你这样做的话。

答案 1 :(得分:0)

我找到了一个解决方案: 使用所需的裁剪值初始化变量

Dim nLeft as integer = 20
Dim nRight as integer = 20
Dim nBoth as integer = 40
Dim nUp as integer = 20

我使用Ghostscript.NET dll创建了rasterize对象: 我没有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