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 use Java to add page numbers to PDF documents

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use Java to add page numbers to PDF documents". In daily operation, I believe that many people have doubts about how to use Java to add page numbers to PDF documents. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use Java to add page numbers to PDF documents". Next, please follow the editor to study!

In our daily work, we often need to add page numbers to PDF documents in order to facilitate reading, retrieving and managing them. So let's share a way to quickly add page numbers to PDF documents in Java programs using free Free Spire.PDF for Java.

Basic steps:

Download the Free Spire.PDF for Java package and extract it

Import the Spire.Pdf.jar package under the lib folder into the Java application as a dependency, or install the JAR package through the Maven repository (see below for the code to configure the pom.xml file)

Create a new Java Class in the Java application (I call it AddPageNumbers here), then enter the appropriate Java code and run it

Configure the pom.xml file:

Com.e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.pdf.free 2.6.3

Java code

Import com.spire.pdf.PdfDocument;import com.spire.pdf.automaticfields.PdfCompositeField;import com.spire.pdf.automaticfields.PdfPageCountField;import com.spire.pdf.automaticfields.PdfPageNumberField;import com.spire.pdf.graphics.*;import java.awt.*;import java.awt.geom.Dimension2D;import java.awt.geom.Rectangle2D;public class AddPageNumbers {public static void main (String [] args) {/ / load PDF document PdfDocument pdf = new PdfDocument () Pdf.loadFromFile ("sample.pdf"); / / create font PdfTrueTypeFont font = new PdfTrueTypeFont (new Font ("Arial", Font.PLAIN, 10), true); / / get page size Dimension2D pageSize = pdf.getPages (). Get (0). GetSize (); / / initialize y coordinates float y = (float) pageSize.getHeight ()-40 / / traverse the page for in the document (int I = 0; I < pdf.getPages (). GetCount ()) {/ / initialize the page code field PdfPageNumberField number = new PdfPageNumberField (); / / initialize the total page number field PdfPageCountField count = new PdfPageCountField () / / create compound field PdfCompositeField compositeField = new PdfCompositeField (font, PdfBrushes.getBlack (), "Page {0} is {1}", number, count); / / set text alignment in compound field compositeField.setStringFormat (new PdfStringFormat (PdfTextAlignment.Right, PdfVerticalAlignment.Top)) / / measure text size Dimension2D textSize = font.measureString (compositeField.getText ()) / / set the location and size of the compound domain on the PDF page compositeField.setBounds (new Rectangle2D.Float (float) pageSize.getWidth ()-(float) textSize.getWidth ()) / 2, y, (float) textSize.getWidth (), (float) textSize.getHeight () / / add the compound field to the PDF page compositeField.draw (pdf.getPages (). Get (I). GetCanvas ());} / / Save as another document pdf.saveToFile ("AddPageNumbers.pdf");} at this point, the study on "how to use Java to add page numbers to PDF documents" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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