Cropping a PDF to png using Ghostscript.net

272 views Asked by At

I'm using visual studio 2015 with VB language Web application, my issue in brief I used Ghostscript to extract pdf first page to png its work fine but if pdf file name contain space its does not extract png and no error show up or if written on other language the error " Page number is not in pages number range!". I appreciate any help.

test.pdf ---> work fine

new york.pdf --->nothing happen no error show up

pdf file name not written in English --->error "Page number is not in pages number range!"

code is

Imports Ghostscript.NET
Imports Ghostscript.NET.Rasterizer
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing
Partial Class Default6
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim inputPdfPath As String = "d:\test\test.pdf"
        Dim outputPath As String = "d:\test\"
        Using Rasterizer = New GhostscriptRasterizer
            Rasterizer.CustomSwitches.Add("-dUseTrimBox")
            Rasterizer.CustomSwitches.Add("-g683x960")
            Rasterizer.CustomSwitches.Add("-f")
            Rasterizer.Open(inputPdfPath)
            Dim pageFilePath As String = Path.Combine(outputPath, "Page1" + ".jpg")
            Dim img As Image = Rasterizer.GetPage(100, 1)
            img.Save(pageFilePath, ImageFormat.Png)
            Console.WriteLine(pageFilePath)
        End Using
    End Sub
End Class
1

There are 1 answers

4
Dave Miller On

First posted in the comments section, I'm including information as answer, as it worked for the user.

Here is a link useful to solve the blank spaces trouble: https://www.vbforums.com/showthread.php?703121-RESOLVED-How-to-Shell-from-a-directory-with-spaces-in-path

For non-english characters, please try with the following approach: Illegal characters in path (Chinese characters)

If the last solution does not work, another alternative is to loop through files in directory and identify the one you are looking for by another meta-data ou file info. In this case, please check this link: vb.net how to loop through a directory listing?