10.JAVA向PDF中添加多行文本(PDFBOX)
[翻译]在上一章提供的示例中,我们讨论了如何向PDF页面添加文本,但通过该程序,您只能添加适合单行的文本。如果尝试添加更多内容,超出单行空间的所有文本将不会显示。
[原文]In the example provided in the previous chapter we discussed how to add text to a page in a PDF but through this program, you can only add the text that would fit in a single line. If you try to add more content, all the text that exceeds the line space will not be displayed.
Previous /'pri.vi.s/ 之前的
Chapter /'taep.t/ 章节
Discussed /d'skst/ 讨论
Text /tekst/ 文本
Page /ped/ 页面
Single /'s.ɡl/ 单个的
Line /lan/ 行
Content /'kɑn.tent/ 内容
Exceeds /k'sidz/ 超出
Displayed /d'spled/ 显示
[翻译]例如,如果您执行上一章的程序并传递以下字符串,只会显示其中的一部分。
[原文]For example, if you execute the above program in the previous chapter by passing the following string only a part of it will be displayed.
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";
Execute /'ek.s.kjut/ 执行
String /str/ 字符串
Displayed /d'spled/ 显示
Part /pɑrt/ 部分
[翻译]将上一章示例中的字符串text替换为上述字符串并执行。执行后,您将获得以下输出。
[原文]Replace the string text of the example in the previous chapter with the above mentioned string and execute it. Upon execution, you will receive the following output.
Replace /r'ples/ 替换
String /str/ 字符串
Execute /'ek.s.kjut/ 执行
Output /'at.pt/ 输出
[翻译]如果您仔细观察输出,可以注意到只显示了字符串的一部分。
[原文]If you observe the output carefully, you can notice that only a part of the string is displayed.
Observe /b'zv/ 观察
Output /'at.pt/ 输出
Carefully /'ker.fl.i/ 仔细地
Notice /'no.ts/ 注意到
String /str/ 字符串
Displayed /d'spled/ 显示
[翻译]为了在PDF中添加多行文本,您需要使用setLeading()方法设置行间距,并在每行结束后使用newline()方法换行。
[原文]In order to add multiple lines to a PDF you need to set the leading using the setLeading() method and shift to new line using newline() method after finishing each line.
Multiple /'ml.t.pl/ 多个的
Lines /lanz/ 行
Leading /'li.d/ 行间距
Method /'meθ.d/ 方法
Shift /ft/ 移动
Newline /'nju.lan/ 新行
Steps 步骤
[翻译]以下是创建空文档并向其中页面添加内容的步骤。
[原文]Following are the steps to create an empty document and add contents to a page in it.
Steps /steps/ 步骤
Create /kri'et/ 创建
Empty /'emp.ti/ 空的
Document /'dɑ.kj.mnt/ 文档
Contents /'kɑn.tents/ 内容
Page /ped/ 页面
Step 1: Loading an Existing Document 步骤 1:加载现有文档
[翻译]您可以使用PDDocument类的load()方法加载现有文档。因此,实例化此类并按以下方式加载所需文档。
[原文]You can load an existing document using the load() method of the PDDocument class. Therefore, instantiate this class and load the required document as shown below.
File file = new File("Path of the document");
PDDocument doc = PDDocument.load(file);
Load /lod/ 加载
Method /'meθ.d/ 方法
Instantiate /n'staen.i.et/ 实例化
Required /r'kward/ 所需的
Document /'dɑ.kj.mnt/ 文档
Step 2: Getting the Required Page 步骤 2:获取所需页面
[翻译]您可以使用getPage()方法获取文档中的所需页面。通过将页面索引传递给此方法,检索所需页面的对象,如下所示。
[原文]You can get the required page in a document using the getPage() method. Retrieve the object of the required page by passing its index to this method as shown below.
PDPage page = doc.getPage(1);
- Retrieve the object of the required page /r'triv/ 检索
Object /'ɑb.dekt/ 对象
Required /r'kward/ 所需的
Page /ped/ 页面
Index /'n.deks/ 索引
Method /'meθ.d/ 方法
Step 3: Preparing the Content stream 步骤 3:准备内容流
[翻译]您可以使用名为PDPageContentStream类的对象插入各种数据元素。您需要向此类的构造函数传递文档对象和页面对象,因此,通过传递前几步创建的这两个对象来实例化此类,如下所示。
[原文]You can insert various kinds of data elements using the object of the class named PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
Insert /n'st/ 插入
Data /'de.t/ 数据
Elements /'el..mnts/ 元素
Object /'ɑb.dekt/ 对象
Constructor /kn'strk.t/ 构造函数
Instantiate /n'staen.i.et/ 实例化
Previous /'pri.vi.s/ 之前的
Step 4: Beginning the Text 步骤 4:开始文本
[翻译]在PDF文档中插入文本时,您可以使用PDPageContentStream类的beginText()和endText()方法指定文本的起点和终点,如下所示。因此,使用beginText()方法开始文本,如下所示。
[原文]While inserting text in a PDF document, you can specify the start and end points of the text using the beginText() and endText() methods of the PDPageContentStream class as shown below.
contentStream.beginText();
..
code to add text content
..
contentStream.endText();
[翻译]因此,使用beginText()方法开始文本,如下所示。
[原文]Therefore, begin the text using the beginText() method as shown below.
contentStream.beginText();
Insert /n'st/ 插入
Text /tekst/ 文本
Specify /'spes..fa/ 指定
Start /stɑrt/ 开始
End /end/ 结束
Methods /'meθ.dz/ 方法
Step 5: Setting the Position of the Text 步骤 5:设置文本位置
[翻译]使用newLineAtOffset()方法,您可以在页面中的内容流上设置位置。
[原文]Using the newLineAtOffset() method, you can set the position on the content stream in the page.
//Setting the position for the line
contentStream.newLineAtOffset(25, 700);
Position /p'z.n/ 位置
Content /'kɑn.tent/ 内容
Stream /strim/ 流
Page /ped/ 页面
Method /'meθ.d/ 方法
Step 6: Setting the Font 步骤 6:设置字体
[翻译]您可以使用PDPageContentStream类的setFont()方法将文本字体设置为所需的样式,如下所示。在此方法中,您需要传递字体类型和字体大小。
[原文]You can set the font of the text to the required style using the setFont() method of the PDPageContentStream class as shown below to this method you need to pass the type and size of the font.
contentStream.setFont( font_type, font_size );
Font /fɑnt/ 字体
Style /stal/ 样式
Method /'meθ.d/ 方法
Type /tap/ 类型
Size /saz/ 大小
Step 7: Setting the Text Leading 步骤 7:设置文本行间距
[翻译]您可以使用setLeading()方法设置文本行间距,如下所示。
[原文]You can set the text leading using the setLeading() method as shown below.
contentStream.setLeading(14.5f);
Leading /'li.d/ 行间距
Method /'meθ.d/ 方法
Step 8: Inserting Multiple Strings Using newline() 步骤 8:使用newline()插入多行字符串
[翻译]您可以使用PDPageContentStream类的showText()方法插入多个字符串,并通过newline()方法分隔每一行,如下所示。
[原文]You can insert multiple strings using the ShowText() method of the PDPageContentStream class, by dividing each of them using the newline() method as shown below.
contentStream.ShowText(text1);
contentStream.newLine();
contentStream.ShowText(text2);
Insert /n'st/ 插入
Multiple /'ml.t.pl/ 多个的
Strings /strz/ 字符串
Method /'meθ.d/ 方法
Dividing /d'va.d/ 分隔
Newline /'nju.lan/ 新行
Step 9: Ending the Text 步骤 9:结束文本
[翻译]插入文本后,您需要使用PDPageContentStream类的endText()方法结束文本,如下所示。
[原文]After inserting the text, you need to end the text using the endText() method of the PDPageContentStream class as shown below.
contentStream.endText();
Insert /n'st/ 插入
Text /tekst/ 文本
End /end/ 结束
Method /'meθ.d/ 方法
Step 10: Closing the PDPageContentStream 步骤 10:关闭PDPageContentStream
[翻译]使用close()方法关闭PDPageContentStream对象,如下所示。
[原文]Close the PDPageContentStream object using the close() method as shown below.
contentstream.close();
Close /kloz/ 关闭
Object /'ɑb.dekt/ 对象
Method /'meθ.d/ 方法
Step 11: Saving the Document 步骤 11:保存文档
[翻译]添加所需内容后,使用PDDocument类的save()方法保存PDF文档,如以下代码块所示。
[原文]After adding the required content, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
doc.save("Path");
Save /sev/ 保存
Required /r'kward/ 所需的
Content /'kɑn.tent/ 内容
Document /'dɑ.kj.mnt/ 文档
Method /'meθ.d/ 方法
Step 12: Closing the Document 步骤 12:关闭文档
[翻译]最后,使用PDDocument类的close()方法关闭文档,如下所示。
[原文]Finally, close the document using the close() method of the PDDocument class as shown below.
doc.close();
Close /kloz/ 关闭
Document /'dɑ.kj.mnt/ 文档
Method /'meθ.d/ 方法
Example 示例
[翻译]此示例演示如何使用PDFBox在PDF中添加多行文本。将此程序保存在名为AddMultipleLines.java的文件中。
[原文]This example demonstrates how to add multiple lines in a PDF using PDFBox. Save this program in a file with name 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.TIMES_ROMAN, 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. ShowText(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();
}
}
Demonstrates /'dem.n.strets/ 演示
Multiple /'ml.t.pl/ 多个的
Lines /lanz/ 行
Save /sev/ 保存
Program /'pro.ɡraem/ 程序
[翻译]使用以下命令从命令提示符编译并执行保存的Java文件。
[原文]Compile and execute the saved Java file from the command prompt using the following commands.
javac AddMultipleLines.java
java AddMultipleLines
Compile /km'pal/ 编译
Execute /'ek.s.kjut/ 执行
Command /k'maend/ 命令
Prompt /prɑmpt/ 提示符
[翻译]执行上述程序后,将向文档添加给定文本,并显示以下消息。
[原文]Upon execution, the above program adds the given text to the document and displays the following message.
Content added
Execution /ek.s'kju.n/ 执行
Adds /aedz/ 添加
Text /tekst/ 文本
Document /'dɑ.kj.mnt/ 文档
Displays /d'splez/ 显示
Message /'mes.d/ 消息
[翻译]如果您验证指定路径中的PDF文档new.pdf,可以观察到已将给定内容以多行形式添加到文档中,如下所示。
[原文]If you verify the PDF Document new.pdf in the specified path, you can observe that the given content is added to the document in multiple lines as shown below.
Verify /'ver..fa/ 验证
Document /'dɑ.kj.mnt/ 文档
Specified /'spes..fad/ 指定的
Path /paeθ/ 路径
Content /'kɑn.tent/ 内容
Added /'aed.d/ 添加的
Multiple /'ml.t.pl/ 多个的
Lines /lanz/ 行
Observe /b'zv/ 观察