PDFBox添加多行文档

在前一章中提供的示例中,学习了如何在PDF中向页面添加文本,但通过此程序,只能添加适合单行的文本。 如果您尝试添加更多内容,则不会显示超出行间距的所有文字。

例如,如果传递以下字符串在上一章中执行上述程序,则只会显示其中的一部分。

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 showText() method of the 
   ContentStream class";

用上面提到的字符串替换上一章中例子的字符串文本并执行它。 执行后,将得到类似以下输出。

如果仔细观察输出,可以看到只显示了一部分字符串。

要将多行添加到PDF,需要使用setLeading()方法设置前导,并在每行完成后使用newline()方法切换到新行。

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

第1步:加载现有文档

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

File file = new File("Path of the document"); 
PDDocument doc = PDDocument.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步:设置文本引导

可以使用setLeading()方法设置文本引导,如下所示。

contentStream.setLeading(14.5f);

第8步:使用newline()插入多个字符串

使用PDPageContentStream类的ShowText()方法插入多个字符串,方法是使用newline()方法将每个字符串分开,如下所示。

contentStream. ShowText(text1); 
contentStream.newLine(); 
contentStream. ShowText(text2);

第9步:结束文本

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

contentStream.endText();

第10步:关闭PDPageContentStream

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

contentstream.close();

第11步:保存文档

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

doc.save("Path");

第12步:关闭文件

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

doc.close();

示例

本示例演示如何使用PDFBox在PDF中添加多行。 此程序保存在名称为AddMultipleLines.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.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class AddMultipleLines {
   public static void main(String args[]) throws IOException {

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

      //Creating a PDF Document
      PDPage page = doc.getPage(1); 
      PDFont font = PDType0Font.load(doc, new File("c:/windows/fonts/times.ttf"));

      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 );
      System.out.println(" getName => "+font.getName());
     // contentStream.setFont( font, 16 );

      //Setting the leading
      contentStream.setLeading(14.5f);

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

      String text1 = "This is an example of adding text to a page in the pdf document. we can add as many lines";
      String text2 = "as we want like this using the ShowText()  method of the ContentStream class";
      //Adding text in the form of string
      contentStream.showText(text1);
      contentStream.newLine();
      contentStream.newLine();
      contentStream.showText(text2);
      contentStream.newLine();
      //Ending the content stream
      contentStream.endText();

      System.out.println("Content added");

      //Closing the content stream
      contentStream.close();

      //Saving the document
      doc.save(new File("F:/worksp/pdfbox/new-mul-doc.pdf"));

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

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


上一篇:PDFBox添加文档

下一篇:PDFBox读取文档

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

扫描二维码
程序员编程王

扫一扫关注最新编程教程