C # Converting office to pdf and pdf to images

Source: Internet
Author: User

Before the National Day holiday, the company had a project to use office, pdf, and image conversion. I had never been in touch with it before, so I sorted out the online solution, the simplest and most effective method is summarized as follows: office-> pdf app Adobe Acrobat 8 Pro's javasmakerapi. dll assembly; pdf-> png (jpg, gif ...) apply Ghostscript. The following details:

I. preparations:

1. install Adobe Acrobat 8 Pro. I installed version 8.1.2 In your installation directory (for example, my own: C: \ Program Files \ Adobe \ Acrobat 8.0 \ mongomaker \ Common \) Find PDFMakerAPI in the common directory. dll assembly, copy it to the DLL folder in the project (this folder is the folder where you save the DLL file. The name is subject to your project), and add reference to it in the project.

2. install Ghostscript, I installed version 8.63, need to use other DLL: FontBox-0.1.0-dev.dll, IKVM. GNU. classpath. dll, IKVM. runtime. dll, PDFBox-0.7.3.dll, where IKVM. GNU. classpath. dll, The PDFBox-0.7.3.dll to add reference to it in the project, the other two (4 dll put) in the DLL folder.

3. Configure Web. config for Ghostscript:

<Deleetask>
<Add key = "GhostScriptView" value = "C:/Program Files/gs/gs8.63/bin"/>
<Add key = "GhostScriptArguments" value = "-dSAFER-dBATCH-dNOPAUSE-r150-sDEVICE = jpeg-dGraphicsAlphaBits = 4"/>
</AppSettings>

Find your own Ghostscript installation directory and modify it.

II. Application:

1. office-> pdf

Reference namespace: using javasmakerapilib. The key code is as follows:

1 ///

2 // parameter: docfile, absolute path of the source office file and file name (C: \ office \ myDoc.doc); printpath, PDF file storage path (D: \ myPdf); printFileName, save

3 // the file name of the saved file (mynewdomainmetadata)

4 ///

5

6 objectmissing = System. Type. Missing;

7 Export makerappapp = new export makerapp ();

8 app. CreatePDF (docfile, printpath + printFileName, inclumakersettings. kConvertAllPages, false, true, true, missing );

2.pdf-> Image

Reference namespace: using orgdomainbox. pdmodel. The key code is as follows:

01 ///

02 // <param name = "pdfFile"> physical path of the PDF file </param>

03 // <param name = "imgPath"> physical path of the converted image file </param>

04 ///

05 public static void synchronized toimages (string pdfFile, string imgPath)

06 {

07 PDDocument doc = PDDocument. load (pdfFile );

08 int pageCount = doc. getDocumentCatalog (). getAllPages (). size (); // calculate the total number of pages in a pdf document

09

10 string pdfFileName = Path. GetFileName (pdfFile );

11 int index = pdfFileName. LastIndexOf ('.');

12 if (index! =-1)

13 pdfFileName = pdfFileName. Substring (0, index );

14

15 string imgFile = Path. Combine (imgPath, pdfFileName); // The converted image file.

16

17 if (pageCount = 0) return;

18 if (pageCount = 1)

19 {

20 imgFile + = ". png ";

21 if (File. Exists (imgFile ))

22 {

23 File. Delete (imgFile );

24}

25}

26 else

27 {

28 for (int I = 0; I <pageCount; I ++)

29 {

30 string _ imgFile = imgFile + (I + 1). ToString () + ". png ";

31 if (File. Exists (_ imgFile ))

32 {

33 File. Delete (_ imgFile );

34}

35}

36 imgFile + = "mongod.png ";

37}

38

39 ProcessStartInfo info = new ProcessStartInfo ();

40 info. CreateNoWindow = true;

41 info. WindowStyle = ProcessWindowStyle. Hidden;

42 info. WorkingDirectory = System. Configuration. ConfigurationManager. AppSettings ["GhostScriptView"];

43 info. Arguments = System. Configuration. ConfigurationManager. receivettings ["GhostScriptArguments"] + @ "-sOutputFile =" + imgFile + "" + pdfFile;

44 info. FileName = @ "gswin32c.exe ";

45 Process subProcess = new Process ();

46 subProcess. StartInfo = info;

47 subProcess. Start ();

48 subProcess. WaitForExit (int. MaxValue );

49}

After completing the preceding steps, you can easily and perfectly convert the office image to a color image.

3. Summary and description:

Acrobat 8 Pro needs to be activated and registered;

When Acrobat 8 Pro generates a pdf file, its own virtual printer pop-up box will pop up. When the office file is ppt or xls, the file will be opened and closed, and the doc will not;

The pdf storage path for office conversion should not contain Chinese characters;

Finally, if you have a message about the program installation package and DLL requirements, I would like to wish you a pleasant job.

 

From: xiaobao LOVE Ji Aisi

Related Article

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.