📜  使用Java将段落作为文本添加到 PDF

📅  最后修改于: 2022-05-13 01:54:45.633000             🧑  作者: Mango

使用Java将段落作为文本添加到 PDF

iText是一个开发的Java库,用于访问和操作PDF 文件,即提取和修改PDF 内容。 Java允许我们合并各种完全开发的包和模块,以便处理 PDF 文件。我们将看到如何使用iText库创建 PDF 文档并向其中添加一个段落。

1.PDFWriter

Java有一个内置包com.itextpdf.kernel.pdf ,它基本上提供了在Java中创建 PDF 文档的类和模块。此包中的一个可用类是PdfWriter。我们实例化此类的一个对象,并将文件路径以及我们希望创建的新 PDF 文件的名称作为参数传递。一个对象被传递到这个类,以便将文本附加到指定的文件位置。以下Java代码片段说明了此类的用法:

// path to create the file 
String file_path = "C:/appendtexttopdfjava.pdf";

// creating an object of PdfWriter class with file_path as argument
PdfWriter pdf_writer = new PdfWriter(file_path); 

这将在 C: 中创建一个新的 PDF 文件,名称为appendtexttopdfjava.pdf

2.PDF文档

com.itextpdf.kernel.pdf包含另一个类来表示 iText 中指定的 PDF 文件,它为用户提供了通过合并此类的各种方法来轻松添加各种功能,如页面字体、文件附件。需要通过将使用 PdfWriter 类创建的 pdf_writer 对象作为参数传递来实例化此类的对象。以下Java代码片段说明了此类的用法:

// Representing PDF document in iText 
PdfDocument pdf_doc = new PdfDocument(pdf_writer); 

3. 文件

com.itextpdf.layout的 Document 类将创建的 PdfDocument 对象作为参数,并实例化用作要执行的所有 PDF 操作的源的 Document。它充当需要修改或附加到文档文件的内容的容器。

// Instantiating a document object from pdf document object 
Document document = new Document(pdf_doc); 

当这个类的对象被创建时,文件被可视化为一个字符流,可以对其执行操作(添加新字符、修改以前的字符、删除等)。

4. 段落

Java内置包com.itextpdf.layout.element的 Paragraph 类基本上是 Document 类的子类。它使用文本流创建一个对象,即实际要添加到 PDF 文档的内容。内容可能是一行行,需要使用文档类提供的add()方法添加到文档类对象中。段落类基本上是文档类的一个元素。可以创建多个段落对象并将其添加到同一文档中。

将内容写入文档后,它被关闭。

//content to be added to the pdf document
String paragraph = "Geeks For Geeks makes you learn coding. It also provides competitions"; 
 
//Creating a paragraph class object
Paragraph para_obj = new Paragraph (paragraph); 
 

//adding paragraph to the document object 
document.add(para_obj); 
 


//closing the document after writing the contents
document.close();

下面的Java代码表示在Java文件中添加一个段落的函数:

Java
// importing thr required packages
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
  
public class AppendtoPdf {
    public static void main(String args[]) throws Exception
    {
        // path to create the file
        String file_path = "C:/appendtexttopdfjava.pdf";
  
        // creating PdfWriter object
        PdfWriter pdf_writer = new PdfWriter(file_path);
  
        // Representing PdfDocument object
        PdfDocument pdf_doc = new PdfDocument(pdf_writer);
  
        // Creating a Document
        // Instantiating a document object from pdf document
        // object
        Document document = new Document(pdf_doc);
  
        // paragraph to be added
        String para
            = "Learn Data Structures Online At Your Own Pace With 
              The Best Of Faculty In The Industry
                  .The Best Data Structures Course Available
                      Online From Skilled And Experienced
                          Faculty.Premium Video Lectures
                  .Active Discussion Forum
                  .Course Certificate.Get Certified.";
  
              // Creating Paragraph object
              Paragraph paragraph_obj
            = new Paragraph(para);
  
        // Adding paragraphs to document
        document.add(paragraph_obj);
        
        // Closing the document
        document.close();
        
        // final message
        System.out.println(
            "Finished writing contents to the file!");
    }
}


在终端上执行的代码在终端上产生以下输出,并在本地计算机上以 C: 格式保存文件。

Finished writing contents to the file!

保存的PDF文件内容如下: