In C #, the producer creator is called to generate a PDF file.

Source: Internet
Author: User

Http://blog.csdn.net/mcai4gl2/article/details/7294625

A short project was created a while ago to generate a report. The generated report is about the stock price record. There are no ready-made packages and third-party programs for us to use. I heard that the pages rendered by WPF can be saved as PDF files. However, I did not dare to practice them. I finally used my previous methods, office InterOP, insert a lot of placeholder into a template in a Word document, then use C # code to dynamically convert placeholder into data, and then save it. This method is actually quite powerful, but there are a few restrictions and unpleasant points. First, you must install the Office on the machine where the program runs. Word is required. If you want to generate a graph, you also need excel.
Second,. Net Office InterOP is essentially a com package, so performance and stability cannot be too demanding. Third, office InterOP is still annoying. I complained for a long time, but this method is still feasible. However, what should I do if I need to generate a report in PDF format, but the Office does not support PDF generation? I think that I can directly use the pdfcreator virtual printer every time I need to generate a PDF file. I checked it and found that the pdfcreator has a COM API. This problem is solved.

In C #, how do I call the volume creator to generate a PDF file?

Step 1: Install the volume creator (this seems nonsense)

Step 2: Create a csung project and add creator.exe com to reference it

Step 3: See the following code:

[CSHARP]
View plaincopy
  1. Public override void process (itask task, int index)
  2. {
  3. If (! File. exists (path. Combine (fromlocation, fromfilename )))
  4. {
  5. Throw new filenotfoundexception ("file:" + path. Combine (fromlocation, fromfilename) + "does not exists ");
  6. }
  7. Monitor. Enter (lockobject );
  8. Clspdfcreator creator = NULL;
  9. Try
  10. {
  11. Creator = new clspdfcreator ();
  12. Creator. eerror + = new _ clspdfcreator_eerroreventhandler (creator_eerror );
  13. Creator. eready + = new _ clspdfcreator_ereadyeventhandler (creator_eready );
  14. String paramters = "/noprocessingatstartup ";
  15. If (! Creator. cstart ())
  16. {
  17. Throw new exception ("cannot launch failed creator. Error:" + error );
  18. }
  19. VaR opt = creator. coptions;
  20. Opt. useautosave = 1;
  21. Opt. useautosavedirectory = 1;
  22. Opt. autosavedirectory = This. tolocation;
  23. Opt. autosaveformat = 0;
  24. Opt. autosavefilename = This. tofilename;
  25. Creator. coptions = OPT;
  26. Creator. cclearcache ();
  27. Creator. cdefaprinprinter = "pdfcreator ";
  28. If (! Creator. cisprintable (path. Combine (fromlocation, fromfilename )))
  29. {
  30. Throw new exception ("file:" + path. Combine (fromlocation, fromfilename) + "is not printable .");
  31. }
  32. Creator. cprintfile (path. Combine (fromlocation, fromfilename ));
  33. Creator. cprinterstop = false;
  34. Ready = false;
  35. VaR duration = new timespan (0, 0, 0, timeoutinsec );
  36. Datetime lastcheck = datetime. now;
  37. Datetime starttime = lastcheck;
  38. While (! Ready & (lastcheck-starttime) <duration ))
  39. {
  40. System. Threading. thread. Sleep (500 );
  41. Lastcheck = datetime. now;
  42. }
  43. Creator. cprinterstop = true;
  44. Thread. Sleep (1000 );
  45. Creator. cClose ();
  46. If (! Ready)
  47. {
  48. Throw new exception ("PDF creation failed. This maybe due to timeout .");
  49. }
  50. }
  51. Finally
  52. {
  53. Monitor. Exit (lockobject );
  54. }
  55. }
  56. Void creator_eready ()
  57. {
  58. This. Ready = true;
  59. }
  60. Void creator_eerror ()
  61. {
  62. Error = creator. cerror. description;
  63. }

The code is easy to understand. I am very lazy. I didn't re-write the code into a project. I directly pasted the code in the project. If you want to use it, you need to modify it a little. The argument is that the producer Creator does not support multithreading. Therefore, if the program itself is multithreading, you need to add a lock mechanism in the Code to ensure that the calls generated by PDF are single-threaded. In addition, the default value is used for the printer name, which has not been modified. However, these problems are difficult for you to look.

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.