Java Print records for PDF methods

Source: Internet
Author: User
Tags print format

[Java]View Plaincopy
  1. Public class Printdemo {
  2. public static void Main (string[] args) {
  3. JFileChooser filechooser = new JFileChooser (); //Create a print job
  4. int state = Filechooser.showopendialog (null);
  5. if (state = = jfilechooser.approve_option) {
  6. File File = Filechooser.getselectedfile (); //Get the selected file
  7. //Build a Print Request property set
  8. Hashprintrequestattributeset Pras = new Hashprintrequestattributeset ();
  9. //Set print format because the type is not determined, so select AutoSense
  10. Docflavor flavor = Docflavor.input_stream. AutoSense;
  11. //Find all available Print Services
  12. Printservice printservice[] = Printservicelookup
  13. . Lookupprintservices (flavor, Pras);
  14. //Locate the default print service
  15. Printservice Defaultservice = Printservicelookup
  16. . Lookupdefaultprintservice ();
  17. //Show Print dialog box
  18. Printservice service = Serviceui.printdialog (null, $ ,
  19. Printservice, Defaultservice, Flavor, Pras);
  20. if (service! = null) {
  21. try {
  22. Docprintjob job = Service.createprintjob (); //Create a print job
  23. FileInputStream FIS = new FileInputStream (file); //construct the file stream to be printed
  24. Docattributeset das = new Hashdocattributeset ();
  25. Doc doc = new Simpledoc (FIS, Flavor, das);
  26. Job.print (Doc, Pras);
  27. } catch (Exception e) {
  28. E.printstacktrace ();
  29. }
  30. }
  31. }
  32. }
  33. }

But after trying to find that the PDF file is not printed in PDF format, but print out garbled (that is, the binary code of the file)

The initial conjecture is that the printer is not recognized. So the above Docflavor changed to Docflavor.input_stream. PDF;

Then printservice[] will be null, why? Because the Lookupdefaultprintservice () method only returns printers that support Docflavor ~

If you do not filter the printer through Docflavor, and print directly, the error will be: Invalid flavor, that is, the type is not supported ~

In fact, we look at Docflavor, it is actually sent to the printer data flow Content-type, similar to the web, that is, do not support the Content-type, then it is more easily understood.


What about that? Don't we have a way to print a PDF file from a Java program? No matter what, at least try it ~

Discover that there is no particularly good solution on the web. Probably collected several kinds of:

1, relatively lazy method, the plug-in program to achieve printing, the most typical is Adobe Reader

2, using Pdfrenderer and other third-party libraries to convert the PDF file into a picture, then you can call Printservice to print


Let's say the first way.

First, you need Adobe Reader on your computer, and you can call Adobe Reader to print a command that relies on acrord32 .

We know that the runtime object is required to invoke the command line in Java, with the following code:

[Java]View Plaincopy
  1. Public Static boolean printpdf (String pdfpath) {
  2. try{
  3. Runtime.getruntime (). EXEC ("cmd.exe/c start acrord32/p/h" + Pdfpath);
  4. return true;
  5. }catch (Exception e) {
  6. E.printstacktrace ();
  7. return false;
  8. }
  9. }

This calls the Adobe Reader's Acrord32.exe method, which describes the parameters of the command below.

Acrord32.exe filename-Execute adobe Reader and browse pdf

Other parameters that are supported are:

acrord32.exe/p filename-executes Adobe Reader and prints a PDF file

acrord32.exe/t path PrinterName drivername portname

Initialize Adobe Reader, do not eject the Print dialog box, and then end printing

The four parameters required for the/t command are

Path-Print file paths

PrinterName-Printer Name

drivername-Printer driver name

PortName-Printer Port

Other parameters:

/n opens a new Adobe Reader, although Adobe Reader is turned on (silent printing is not necessary)

/s Open an Adobe reader does not open splash screen

/o open an Adobe reader without opening the Open File dialog box

/h to minimize the opening of an Adobe Reader (silent print usage)

Java Print records for PDF methods

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.