目录

PDFBox - 添加多行( Adding Multiple Lines)

在上一章提供的示例中,我们讨论了如何在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 <b class="notranslate">showText()</b> method of the 
   ContentStream class";

用上面提到的字符串替换上一章中示例的string text并执行它。 执行后,您将收到以下输出。

单线扩展

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

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

Steps

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

第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步:使用换行符插入多个字符串()

您可以使用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();

例子 (Example)

此示例演示如何使用PDFBox在PDF中添加多行。 将此程序保存在名为AddMultipleLines.java.的文件中AddMultipleLines.java.

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 AddMultipleLines {
   public static void main(String args[]) throws IOException {
      //Loading an existing document
      File file = new File("C:/PdfBox_Examples/my_pdf.pdf");
      PDDocument doc = document.load(file);
      //Creating a PDF Document
      PDPage page = doc.getPage(1);  
      PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
      //Begin the Content stream 
      contentStream.beginText(); 
      //Setting the font to the Content stream
      contentStream.setFont( PDType1Font.<i>TIMES_ROMAN</i>, 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. <b class="notranslate">ShowText</b>(text1);
      contentStream.newLine();
      contentStream. <b class="notranslate">ShowText</b>(text2);
      //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_Examples/new.pdf"));
      //Closing the document
      doc.close();
   }
}

使用以下命令从命令提示符编译并执行保存的Java文件。

javac AddMultipleLines.java 
java AddMultipleLines

执行时,上述程序将给定文本添加到文档中并显示以下消息。

Content added

如果在指定路径中验证PDF文档new.pdf ,则可以观察到给定内容以多行添加到文档中,如下所示。

添加多行
↑回到顶部↑
WIKI教程 @2018