The following example will introduce how to personalize PDF pages through Java programming, including setting page size, margins, paper direction, page rotation, etc. Here you can choose from the following page sizes:;
At the same time, when setting document content rotation, the following angles can be supported for content rotation.
Use tools: Free flame.pdf of Java (free version)
About jar file reference:
Step 1: Step 1: create a new folder in the Java program to be named Lib. And copy the 2 jar files in the package to the new folder.
Step 2: after copying the files, add them to the reference class library: select the two jar files, right-click and select "Build Path" – "Add to Build Path". Complete the reference.
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){ //Establish PdfDocument object PdfDocument originalDoc = new PdfDocument(); //Load PDF file originalDoc.loadFromFile("input.pdf"); //Create a new PdfDocument Example PdfDocument newDoc = new PdfDocument(); //Traversing all PDF page Dimension2D dimension2D = new Dimension(); for (int i = 0; i < originalDoc.getPages().getCount(); i++) { PdfPageBase page = originalDoc.getPages().get(i); if (i == 0) { //Set the page width and height of the first page of the new document to the original 1.2 times 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 margin 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 the canvas and set the content to be scaled according to the size of the page 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 margin of the third page of the new document to 200 left and right, 50 up and 50 down PdfMargins margins = new PdfMargins(240, 50); //Set the page size of the third page of the new document to A3, Page rotation angle is 0, paper direction is horizontal PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.A3, margins, PdfPageRotateAngle.Rotate_Angle_0, PdfPageOrientation.Landscape); //Adjust the canvas and set the content to be scaled according to the size of the page 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()); } //Preservation PDF newDoc.saveToFile("pdfPageSetting.pdf"); } } }
After the code is completed, run the program and generate the document. After page setting, the effect is as follows:
(end of this paper)
Reprint please indicate the source!