In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the methods of Java printing PDF documents", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the methods of Java printing PDF documents"!
Use tools: Spire.PDF for Java
Jar file acquisition and import:
Method 1: download the jar package. After downloading, extract the file and import the Spire.Pdf.jar under the lib folder into the java program.
Method 2: can be imported through the maven library. Refer to the import method.
Java code example
[example 1] Silent printing
That is, print the PDF document directly by using the default printer. When printing, we can set the number of printed copies, set the margin of paper printing, and so on.
Import com.spire.pdf.*;import java.awt.print.*; public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); Paper loPaper = loPageFormat.getPaper () / / Delete the default margin loPaper.setImageableArea (0Jing 0jinloPageFormat.getWidth (), loPageFormat.getHeight ()); / / set the number of printed copies loPrinterJob.setCopies (2); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); try {loPrinterJob.print ();} catch (PrinterException e) {e.printStackTrace () }}}
[example 2] display a print dialog box to print an PDF document
Import com.spire.pdf.*;import java.awt.print.*; public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); Paper loPaper = loPageFormat.getPaper () / / Delete the default margin loPaper.setImageableArea (0Magne0Magneto PageFormat.getWidth (), loPageFormat.getHeight ()); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); / / Show print dialog box if (loPrinterJob.printDialog ()) {try {loPrinterJob.print () } catch (PrinterException e) {e.printStackTrace ();}
[example 3] Custom paper size when printing
Import com.spire.pdf.*;import java.awt.print.*;public class Print {public static void main (String [] args) {/ / load document PdfDocument pdf = new PdfDocument (); pdf.loadFromFile ("Sample.pdf"); PrinterJob loPrinterJob = PrinterJob.getPrinterJob (); PageFormat loPageFormat = loPrinterJob.defaultPage (); / / set print paper size Paper loPaper = loPageFormat.getPaper () LoPaper.setSize (500600); loPageFormat.setPaper (loPaper); loPrinterJob.setPrintable (pdf,loPageFormat); try {loPrinterJob.print ();} catch (PrinterException e) {e.printStackTrace ();} so far, I believe you have a deeper understanding of "what are the methods of Java printing PDF documents?" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.