Search code examples
c#vb.netghostscript.net

ghostscript PDF to Png crop x and y


i use ghostscript for my convertion PDF to PNG in vb.net when i crop my pdf then i convert this in png but ghostscript keep x and y position cropping in my picture.

I have solved this problem when i use gswin64.exe in cmd with : -c "<</Install {-48 -87 translate}>> setpagedevice"

but with dll Ghostcript.NET when i add this command in my code : oGSImage.CustomSwitches.Add("-c ""<</Install {-48 -87 translate}>> setpagedevice""") i have error message

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

my device is pngAlpha, if anyone could help me :)


Solution

  • I have found a solution : Initialize variable with the desired crop values

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

    i created rasterize object with Ghostscript.NET dll : I Get Height and width size page without 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()
    

    then, i create a new rasterize for get height and width size page with 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)
    

    And i Create function 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
    

    Now perfect work with nLeft nRight nBoth nUp crop, hoping that it will help some people :D