如何使用Java向PDF添加文本。(How to add text to a PDF using Java.)

优质
小牛编辑
127浏览
2023-12-01

问题描述 (Problem Description)

如何使用Java向PDF添加文本。

解决方案 (Solution)

以下是使用Java将文本添加到PDF文档的示例程序。

import java.io.File; 
import java.io.IOException;  
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDType1Font;  
public class AddingTextToAPdf {  
   public static void main(String args[]) throws IOException {     
      //Loading an existing document 
      PDDocument doc = PDDocument.load(new File("C:/pdfBox/AddText_IP.pdf")); 
      //Creating a PDF Document 
      PDPage page = doc.getPage(0);       
      PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
      //Begin the Content stream 
      contentStream.beginText(); 
      //Setting the font to the Content stream  
      contentStream.setFont( PDType1Font.TIMES_ROMAN, 16 ); 
      //Setting the position for the line 
      contentStream.newLineAtOffset(25, 725 ); 
      String text = "This is an example of adding text to a page in the pdf document. 
         we can add as many lines as we want like this using the draw string method 
         of the ContentStream class"; 
      //Adding text in the form of string 
      contentStream.showText(text); 
      //Ending the content stream 
      contentStream.endText(); 
      System.out.println("Content added");       
      //Closing the content stream 
      contentStream.close();      
      //Saving the document  
      doc.save(new File("C:/pdfBox/AddText_OP.pdf")); 
      //Closing the document  
      doc.close();  
   }  
} 

输入 (Input)

添加文本输入

输出 (Output)

添加文本输出