iText - 設定字型



本章將介紹如何使用 iText 庫在 PDF 文件中設定文字的顏色和字型。

設定 PDF 文字的字型

您可以透過例項化Document類來建立一個空的 PDF 文件。例項化此類時,需要將PdfDocument物件作為引數傳遞給它的建構函式。

要向文件新增段落,需要例項化Paragraph類並將此物件使用add()方法新增到文件中。您可以使用setFontColor()setFont()方法分別設定文字的顏色和字型。

以下是設定 pdf 文件中文字顏色和字型的步驟。

步驟 1:建立 PdfWriter 物件

PdfWriter類表示 PDF 的 DocWriter。此類屬於com.itextpdf.kernel.pdf包。此類的建構函式接受一個字串,表示要建立 PDF 檔案的路徑。

透過向其建構函式傳遞一個字串值(表示您需要建立 PDF 的路徑)來例項化 PdfWriter 類,如下所示。

// Creating a PdfWriter 
String dest = "C:/itextExamples/fonts.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

當此型別的物件傳遞給 PdfDocument(類)時,新增到此文件的每個元素都將寫入指定的檔案。

步驟 2:建立 PdfDocument

PdfDocument類是在 iText 中表示 PDF 文件的類。此類屬於com.itextpdf.kernel.pdf包。要例項化此類(以寫入模式),需要將PdfWriter類的物件傳遞給它的建構函式。

透過將 PdfWriter 物件傳遞給它的建構函式來例項化 PdfDocument 類,如下所示。

// Creating a PdfDocument  
PdfDocument pdfDoc = new PdfDocument(writer); 

建立 PdfDocument 物件後,可以使用其類提供的相應方法新增各種元素,例如頁面、字型、檔案附件和事件處理程式。

步驟 3:建立 Document 類

com.itextpdf.layout包中的Document類是建立自包含 PDF 時的根元素。此類的建構函式之一接受PdfDocument類的物件。

透過傳遞在前面步驟中建立的PdfDocument類的物件來例項化 Document 類,如下所示。

// Creating a Document   
Document document = new Document(pdfDoc); 

步驟 4:建立文字

透過例項化com.itextpdf.layout.element包中的Text類來建立文字,如下所示。

// Creating text object 
Text text = new Text("Tutorialspoint"); 

步驟 5:設定文字的字型和顏色

使用com.itextpdf.kernel.font包中PdfFontFactory類的createFont()方法建立PdfFont物件,如下所示。

// Setting font of the text PdfFont 
font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD); 

現在,使用Text類的setFont()方法將字型設定為該方法。將PdfFont物件作為引數傳遞,如下所示。

text1.setFont(font);

要將顏色設定為文字,請呼叫 Text 類的setFontColor()方法,如下所示。

// Setting font color 
text.setFontColor(Color.GREEN); 

步驟 6:將文字新增到段落

建立一個Paragraph類物件,並使用其add()方法新增上面建立的文字,如下所示。

// Creating Paragraph 
Paragraph paragraph = new Paragraph();  

// Adding text to the paragraph 
paragraph.add(text); 

步驟 7:將段落新增到文件

使用Document類的add()方法將段落新增到文件中,如下所示。

doc.add(paragraph1)

步驟 8:關閉文件

使用Document類的close()方法關閉文件,如下所示。

// Closing the document 
document.close(); 

示例

以下 Java 程式演示瞭如何使用 iText 庫在 PDF 中設定文字的顏色和字型。它建立一個名為fonts.pdf的 PDF 文件,格式化文字並將其儲存在C:/itextExamples/路徑中。

將此程式碼儲存在名為FormatingTheText.java的檔案中。

import com.itextpdf.io.font.FontConstants; 
import com.itextpdf.kernel.color.Color; 
import com.itextpdf.kernel.font.PdfFontFactory; 
import com.itextpdf.kernel.font.PdfFont; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfWriter; 

import com.itextpdf.layout.Document; 
import com.itextpdf.layout.element.Paragraph; 
import com.itextpdf.layout.element.Text;  

public class FormatingTheText {     
   public static void main(String args[]) throws Exception {        
      // Creating a PdfWriter object   
      String dest = "C:/itextExamples/fonts.pdf";   
      PdfWriter writer = new PdfWriter(dest);             
   
      // Creating a PdfDocument object      
      PdfDocument pdf = new PdfDocument(writer);                   
   
      // Creating a Document object       
      Document doc = new Document(pdf);
   
      // Creating text object       
      Text text1 = new Text("Tutorialspoint");              
   
      // Setting font of the text       
      PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);       
      text1.setFont(font);                 
   
      // Setting font color
      text1.setFontColor(Color.GREEN);
   
      // Creating text object
      Text text2 = new Text("Simply Easy Learning");
      text2.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA));         
      
      // Setting font color
      text2.setFontColor(Color.BLUE);
      
      // Creating Paragraph
      Paragraph paragraph1 = new Paragraph();
      
      // Adding text1 to the paragraph
      paragraph1.add(text1);
      paragraph1.add(text2);
      
      // Adding paragraphs to the document
      doc.add(paragraph1);
      doc.close();       
      
      System.out.println("Text added to pdf ..");   
   }     
}   

使用以下命令從命令提示符編譯並執行儲存的 Java 檔案:

javac FormatingTheText.java 
java FormatingTheText 

執行後,上述程式將建立一個顯示以下訊息的 PDF 文件。

Text added to pdf ..

如果驗證指定的路徑,您可以找到建立的 PDF 文件,如下所示。

Fonts
廣告