Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the detailed setting of PDF page through Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to achieve the PDF page through Java detailed settings, I hope you have some gains after reading this article, let's discuss it together!

use tooling

Free Spire.PDF of Java 2.2.2 (Free Edition)

Method 1:

Download and quote jar through official website, as follows:

Step 1: Create a new folder in your Java program. You can name it Lib. Copy the jar file (as shown below) in the product package to the newly created folder.

Step 2: After copying the file, add it to the reference library: select the jar file, right-click and select "Build Path" - "Add to Build Path". Complete quote.

Method 2:

Installed via maven.

Java example (for reference) import com.spire.pdf.*; import com.spire.pdf.graphics.PdfMargins;import java.awt.*; import java.awt.geom.Dimension2D;import java.awt.geom.Point2D;public class PageSettings_PDF { public static void main (String[] args){ //Create PdfDocument object PdfDocument originalDoc = new PdfDocument(); //Load PDF file originalDoc.loadFromFile ("input.pdf"); //Create a new PdfDocument instance PdfDocument newDoc = new PdfDocument(); //Traverse all PDF pages Dimension2D dimension2D = new Dimension(); for (int i = 0; i < originalDoc.getPages().getCount(); i++) { PdfPageBase page = originalDoc.getPages().get(i); if (i == 0) { //Set the width and height of the first page of the new document to 1.2 times the original float scale = 1.2f; float width = (float) page.getSize().getWidth() * scale; float height = (float) page.getSize().getHeight() * scale; dimension2D.setSize(width, height); //Set the margin of the first page of the new document to 50 left and right, 100 up and down PdfMargins margins = new PdfMargins(50, 100); PdfPageBase newPage = newDoc.getPages().add(dimension2D, margins); //copy the contents of the original document to the new document newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } if (i == 1) { //Set the margins of the second page of the new document to 100 left and right, 100 up and down PdfMargins margins = new PdfMargins(100,100); //Set the page size of the second page of the new document to A3 PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.A3, margins); //Adjust canvas, settings are also scaled according to page size double wScale = (PdfPageSize.A3.getWidth() - 10) / PdfPageSize.A3.getWidth(); double hScale = (PdfPageSize.A3.getHeight() - 10) / PdfPageSize.A3.getHeight(); newPage.getCanvas().translateTransform(wScale, hScale); //copy the contents of the original document to the new document newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } if (i == 2) { //Set the margins of the third page of the new document to 200 left and right, 50 up and down PdfMargins margins = new PdfMargins(240, 50); //Set the page size of the third page of the new document to A3, page rotation angle to 0, and paper orientation to horizontal PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.A3, margins, PdfPageRotateAngle.Rotate_Angle_0, PdfPageOrientation.Landscape); //Adjust canvas, settings are also scaled according to page size double wScale = PdfPageSize.A4.getHeight() / page.getSize().getWidth(); double hScale = PdfPageSize.A4.getWidth() / page.getSize().getHeight(); newPage.getCanvas().translateTransform(wScale, hScale); //copy the contents of the original document to the new document newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float()); } //Save PDF newDoc.saveToFile("pdfPageSetting.pdf"); } }} After reading this article, I believe you have a certain understanding of "how to achieve detailed settings for PDF pages through Java." If you want to know more about related knowledge, please pay attention to the industry information channel. Thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report