About the two days to study the Java Print PDF method record

Source: Internet
Author: User
Tags print format

These two days in the study of Java Call printer print PDF file method, learned a lot of things, especially to record.

On the Java Print Network The most and also Java-Orthodox printing method is to use Printservice, a set of more standard printing code is as follows:

public class Printdemo {public static void main (string[] args) {JFileChooser filechooser = new JFileChooser ();//create print job in T state = Filechooser.showopendialog (null), if (state = = jfilechooser.approve_option) {File File = Filechooser.getselectedfile (); Gets the selected file//Build Print Request Property Set Hashprintrequestattributeset Pras = new Hashprintrequestattributeset ();//sets the print format because the type is not determined, So choose autosensedocflavor Flavor = Docflavor.input_stream. autosense;//Find all available Print Services Printservice printservice[] = printservicelookup.lookupprintservices (flavor, Pras);// Locate the default print service Printservice Defaultservice = Printservicelookup.lookupdefaultprintservice ();//Display the Print dialog box Printservice Service = Serviceui.printdialog (null, $, 200,printservice, Defaultservice, Flavor, Pras); if (service! = null) {try {DOCP Rintjob job = Service.createprintjob (); Create a print job FileInputStream fis = new FileInputStream (file); Construct the file stream to be printed docattributeset das = new Hashdocattributeset ();D oc Doc = new Simpledoc (FIS, Flavor, das); Job.print (Doc, Pras) ;} catch (Exception E{E.printstacktrace ();}}}}} 

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:

public static Boolean printpdf (String pdfpath) {try{runtime.getruntime (). EXEC ("cmd.exe/c start acrord32/p/h" + Pdfpath) ; return true;} catch (Exception e) {e.printstacktrace (); return false;}}

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)

About the two days to study the Java Print PDF method record

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.