PDFBox添加文档

在前一章中,我们讨论了如何将页面添加到PDF文档。 在本章中,我们将讨论如何将文本添加到现有的PDF文档。

将文本添加到现有的PDF文档

可以使用PDFBox库将内容添加到文档,它提供一个名称为PDPageContentStream的类,其中包含在PDFDocument的页面中插入文本,图像和其他类型内容所需的方法。

以下是创建空白文档并将内容添加到其中的页面的步骤。

第1步:加载现有文档

使用PDDocument类的load()方法加载现有文档。 因此,请实例化此类并加载所需的文档,如下所示。

File file = new File("Path_of_the_document"); 
PDDocument doc = document.load(file);

第2步:获取所需的页面

使用getPage()方法获取文档中的所需页面。 通过将索引传递给此方法来检索所需页面的对象,如下所示。

PDPage page = doc.getPage(1);

第3步:准备内容流

使用PDPageContentStream类的对象插入各种数据元素。 因此,需要将文档对象和页面对象传递给此类的构造函数,通过传递在前面的步骤中创建的这两个对象来实例化此类,如下所示。

PDPageContentStream contentStream = new PDPageContentStream(doc, page);

第4步:开始文本

在PDF文档中插入文本时,可以使用PDPageContentStream类的beginText()endText()方法指定文本的开始点和结束点,如下所示。

contentStream.beginText(); 
// 其它代码 .....
code to add text content 
// 其它代码 .....
contentStream.endText();

因此,使用beginText()方法开始文本,如下所示。

contentStream.beginText();

第5步:设置文本的位置

使用newLineAtOffset()方法,可以在页面中设置内容流的位置。

//Setting the position for the line 
contentStream.newLineAtOffset(25, 700);

第6步:设置字体

可以使用PDPageContentStream类的setFont()方法将文本的字体设置为所需的样式,如下所示。 要使用此方法,需要传递字体的类型和大小。

contentStream.setFont( font_type, font_size );

第7步:插入文本

使用PDPageContentStream类的ShowText()方法将文本插入到页面中,如下所示。 该方法以字符串的形式接受所需的文本。

contentStream.showText(text);

第8步:结束文本

插入文本后,需要使用PDPageContentStream类的endText()方法结束文本,如下所示。

contentStream.endText();

第9步:关闭PDPageContentStream

使用PDPageContentStream类的close()方法关闭对象,如下所示。

contentstream.close();

第10步:保存文档

添加所需内容后,使用PDDocument类的save()方法保存PDF文档,如以下代码块中所示。

doc.save("Path");

步骤11:关闭文件

最后,使用PDDocument类的close()方法关闭文档,如下所示。

doc.close();

示例

本示例演示如何将内容添加到文档中的页面。 在这里将创建一个Java程序来加载保存在F:\worksp\pdfbox目录中的my_doc.pdf的PDF文档,并为其添加一些文本。 将此代码保存在AddingContent.java 的文件中。

package com.zyiz;

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 AddingContent {
   public static void main (String args[]) throws IOException {

      //Loading an existing document
      File file = new File("F:\\worksp\\pdfbox\\my_doc.pdf");
      PDDocument document = PDDocument.load(file);

      //Retrieving the pages of the document 
      PDPage page = document.getPage(0);
      PDPageContentStream contentStream = new PDPageContentStream(document, page);

      //Begin the Content stream 
      contentStream.beginText(); 
      // contentStream.set
      //Setting the font to the Content stream  
      contentStream.setFont(PDType1Font.HELVETICA_BOLD, 14);

      //Setting the position for the line 
      contentStream.newLineAtOffset(25, 500);

      String text = "This is the sample document and we are adding content to it. - By zyiz.net";

      //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
      document.save(new File("F:\\worksp\\pdfbox\\new-doc-text.pdf"));

      //Closing the document
      document.close();
   }
}

执行上面示例代码后,在指定路径中找到并打开PDF文档:new-doc-text.pdf,则可以观察到给定内容已添加到文档中,如下所示。


上一篇:PDFBox文档属性

下一篇:PDFBox添加多行文档

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程