Ghostscript converts a PDF file to a PNG image.

Source: Internet
Author: User

Due to project requirements, you need to convert the original PDF document into an image file, and find some methods for converting PDF files to images on the Internet. After testing a few files, there are problems like this or that.

1. pdflibnet. dll. This type is quite useful at the beginning and can be converted to images and HTML, but it seems that it is no longer updated. In addition, text loss occurs during conversion (some documents ).

2. Adobe Acrobat x pro. This is too large and requires registration and activation. The code found on the Internet is not very useful in Web mode. You need to create a service or command line program to call it as a middleware.

3. ghostscript. There are also a lot of DEMO code on the Internet, not many, which is also suitable for my requirements. So I tested it.

However, for Online Demo code, you need to use DLL files such as product_box and ikvm (Consumer box does not seem to have been updated for a long time ), however, in the Demo code, you only need to use one operation to get the number of PDF pages. After testing, it seems a bit redundant.

Ghostscript can directly output all PDF pages. If you need to output a specified page, you need to use the pdf page data.

Therefore, you only need to install ghostscript on the server. However, what you need to mention here is that the version 9.00 or above in the test is not very useful, and the version 8.71 is good, very useful

In version 9.00 and later, the Font Processing seems to have bugs or I have not configured them myself (by modifying: lib/cidfmap ).

During the test, the font cannot be properly loaded;

Substituting font Times-bold for timesnewromanps-boldmt.
Loading NimbusRomNo9L-Medi font from % Rom % resource/font/NimbusRomNo9L-Medi... 2432860 1126845 2423648 1025245 3 done.
Loading a TT font from C:/Windows/fonts/simsun. TTC to emulate a CID font simsun... done.
Substituting font Times-Roman for timesnewromanpsmt.
Loading NimbusRomNo9L-Regu font from % Rom % resource/font/NimbusRomNo9L-Regu... 3770200 2395423 2776248 1391721 3 done.

Text loss still occurs when Xuan changes the document

Later I switched to version 8.71 and then tested it:

Modify: lib/cidfmap:

Append the content below the % substitutions section (the following content is only for document settings that cannot be converted normally during the test. After modification, the document is converted successfully ):

/Microsoftyahei </filetype/TrueType/subfontid 0/CSI [(gb1) 2]/path (C:/Windows/fonts/msyh. TTF)>;
/Lucidasansunicode </filetype/TrueType/subfontid 0/CSI [(gb1) 2]/path (C:/Windows/fonts/msyh. TTF)>;

Running result:

Loading a TT font from C:/Windows/fonts/msyh. TTF to emulate a CID font microsoftyahei... done.
* *** Warning: Pattern stream has unbalanced Q/Q operators (too 1_q's)
Loading a TT font from C:/Windows/fonts/msyh. TTF to emulate a CID font lucidasansunicode... done.

The generated image text is normal. pdf files that cannot be converted can be converted to images correctly, but the text is not lost.

In actual operations, I remove the DLL reference of the export box, because the project has referenced itextsharp. dll, which can obtain the number of pages.

# Region convert toimages /// <summary> // Convert PDF to PNG, install <C> ghostscript </C> /// </Summary> // <Param name = "pdffile"> pdf file path </param> // <param name = "imgpath"> image storage folder path </param> Public static void inclutoimages (string pdffile, string imgpath) {// if the document does not exist if (! Filehelper. fileexists (pdffile) {return;} int pagecount = utils. getpdfpagecount (pdffile); // if the document is empty, if (pagecount = 0) {return ;} // The converted image file // the file name generation rule % d indicates the data label // % 03d 2017001.png, 002.png string imgfile = string. concat (imgpath, "mongod.png"); string bin = system. web. configuration. webconfigurationmanager. appsettings ["ghostscriptbin"]; string EXE = @ "gswin32c.exe"; string arguments = system. web. configuration. webconfigurationmanager. appsettings ["ghostscriptarguments"]; using (system. diagnostics. process proc = new system. diagnostics. process () {try {Proc. startinfo. filename = string. concat (bin, exe); Proc. startinfo. workingdirectory = bin; Proc. startinfo. arguments = string. concat (arguments, "-soutputfile = \" ", imgfile," \ "\" ", pdffile," \ ""); Proc. startinfo. useshellexecute = false; Proc. startinfo. createnowindow = true; Proc. startinfo. redirectstandarderror = true; Proc. startinfo. redirectstandardinput = false; Proc. startinfo. redirectstandardoutput = true; Proc. startinfo. windowstyle = system. diagnostics. processwindowstyle. hidden; Proc. errordatareceived + = new datareceivedeventhandler (effectoimages_errordatareceived); // redirect error output Proc. outputdatareceived + = new datareceivedeventhandler (pdftoimages_outputdatareceived); // redirect the output Proc. start (); Proc. beginerrorreadline (); // if this line is not added, some PDF documents may fail to be converted. beginoutputreadline (); // wait for exiting Proc. waitforexit (Int. maxvalue);} finally {Proc. close ();}}}

 

Ghostscript converts a PDF file to a PNG image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.