在上一章提供的示例中,我们讨论了如何在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()方法并在完成每一行后使用换行符()方法切换到新行.
以下是创建空文档并向其中的页面添加内容的步骤.
您可以使用PDDocument类的 load()方法加载现有文档.因此,实例化此类并加载所需的文档,如下所示.
File file = new File("Path of the document"); PDDocument doc = PDDocument.load(file);
您可以使用在文档中获取所需页面getPage()方法.通过将索引传递给此方法来检索所需页面的对象,如下所示.
PDPage page = doc.getPage(1);
您可以使用类的对象插入各种数据元素命名为 PDPageContentStream .您需要将文档对象和页面对象传递给此类的构造函数,因此,通过传递前面步骤中创建的这两个对象来实例化此类,如下所示.
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
在PDF文档中插入文本时,可以指定开始和结束使用 PDPageContentStream 类的 beginText()和 endText()方法的文本点,如下所示.
contentStream.beginText(); ……………………….. code to add text content ……………………….. contentStream.endText();
因此,请使用 beginText()方法开始文本,如下所示.
contentStream.beginText();
使用 newLineAtOffset()方法,您可以在页面中的内容流上设置位置.
//Setting the position for the line contentStream.newLineAtOffset(25, 700);
您可以使用
contentStream.setFont(font_type,font_size);
您可以使用 setLeading()设置前导文本方法如下所示.
contentStream.setLeading(14.5f);
您可以使用 ShowText插入多个字符串() PDPageContentStream 类的方法,通过使用换行符()方法对每个方法进行划分,如下所示.
contentStream. ShowText(text1); contentStream.newLine(); contentStream. ShowText(text2);
插入文本后,需要使用
contentStream.endText() ;
使用<b关闭 PDPageContentStream 对象> close()方法如下所示.
contentstream.close();
添加所需内容后,使用保存PDF文档 PDDocument 类的save()方法,如以下代码块所示.
doc.save("Path");
最后,使用关闭() PDDocument 类的方法,如下所示.
doc.close();
此示例演示如何使用PDFBox在PDF中添加多行.将此程序保存在名为 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(); } }
使用以下命令从命令提示符编译并执行保存的Java文件.
javac AddMultipleLines.java java AddMultipleLines
执行时,上述程序添加给定文本到文档并显示以下消息.
Content added
如果您在指定路径中验证PDF文档 new.pdf ,您可以观察到给定内容以多行添加到文档中,如下所示.