1

I have a programming task. where I need to use a free library or a command line program to convert pdf to image using C#.

So far converting using Ghostscript works except that it creates a 8 red box in the center of the image:

The content of the pdf is pure white only, but why do i have 8 red box on the image? What did i do wrong?

Here's the code:

string outputImagesPath = null;


string inputPDFFile = null;
inputPDFFile = @"C:\Users\user\cover.pdf";
outputImagesPath = @"C:\user\Desktop\1.jpg";
string ghostScriptPath = @"C:\Users\gswin32.exe";

String ars = "-o" + outputImagesPath+ "%03d.png -sDEVICE=jpeg -dJPEGQ=100 " + inputPDFFile;
Process proc = new Process();
proc.StartInfo.FileName = ghostScriptPath;
proc.StartInfo.Arguments = ars;
proc.StartInfo.CreateNoWindow = true;
//proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string strOutput = proc.StandardOutput.ReadToEnd();
Console.WriteLine(strOutput);
proc.WaitForExit();

here is the link of the pdf, thank you https://drive.google.com/open?id=0B0auNx4EZsCUUkFHWGR4MjV5NzA

  • I believe your Error is here `+ outputImagesPath+ "%03d.png`. Shouldnt it be `+ outputImagesPath+ "%d.jpeg`? – lokusking Jul 11 '16 at 09:43

2 Answers2

1

Take a look here, use another nuget package that work with .net core and there is no need to install to the server or your local computer.best answer saving pdf pages as images

Mohammad Hassani
  • 511
  • 6
  • 14
-1

Most likely this is caused by a missing font or CIDFont, the rectangles are the .notdef glyph which is used when a glyph cannot be found. Of course, its not possible to tell without seeing the original PDF file.

However if you check the Ghostscript back channel (and no I cannot tell you how to do this with Ghostscript.NET as that is not an Artifex product) you will probably see warnings about missing glyphs.

I can look further but only if you make the PDF file available. It would also be helpful to know what version of Ghostscript you are using.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • i uploaded the pdf file. can you please help me. thank you – askquestionzero Jul 12 '16 at 01:15
  • Well, basically your file is insanely over-complicated, it includes multiple forms and spurious transparency which causes the uncompressed file to be 4.2 MB in size. One of the forms contains " (3'\)3'\)'HPR'HPR) Tj " which does in fact draw text. The font in question is an embedded CIDFont, and the CMap in use maps these to glyphs which are not present in the font. Hence the .notdef result. Basically your PDF file is broken as weel as being incredibly inefficient. The only way for you to 'fix' this is to remove the text from the file. – KenS Jul 12 '16 at 07:21
  • 1
    By the way, Ghostscript isn't 'free' its open source, that's not quite the same thing. Please make sure you are abiding by the terms of the AGPL under which it is licenced. – KenS Jul 12 '16 at 07:21