基於iTextSharp(C#)創建PDF文件

1.基礎知識

1.1 Chunk(塊)

Chunk是添加到Document 對象的用於表示文本的最小對象。Chunk對象包含一個StringBuffer用於存放文本,文本中的字符是相同的字體格式(樣式、大小、顏色),這些格式在Font對象中設置。Chunk還可通過成員函數更改其他屬性,比如背景色、上(下)標、下劃線、刪除線等。
作者在第二版《iText in Action》中所用的例子基於一個數據庫,爲了降低複雜性,本人在學習過程中並未使用該數據庫,而是使用一些簡單的操作來實踐iText的功能。
下面是使用Chunk的一個例子:

Document doc = new Document(PageSize.B5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("F:\\test.pdf", FileMode.Create));
writer.InitialLeading = 30;
doc.Open();

doc.Add(new Chunk("China"));
doc.Add(Chunk.NEWLINE);
Font font = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.WHITE);
Chunk content = new Chunk("  BeiJing", font);
content.SetBackground(BaseColor.BLACK, 1F, 1F, 1F, 1F);
content.SetUnderline(1F, -1F); ;
doc.Add(content);

doc.Close();

作者說,這個例子比較特殊,因爲Chunk對象用於組合成其他的對象如Phrase或Paragraph等來表示文字,一般不直接把Chunk對象添加到Document對象中,除了一些特殊的如Chunk.NEWLINE。
行間距(leading)
Chunk 對象並不會自動設置行間距。PdfWriter的setInitialLeading操作用於設置行間距,但在iTextSharp中,通過直接給 PdfWriter的InitialLeading屬性賦值來實現。在上面的例子中,如果不設置行間距,兩個Chunk對象會寫在同一行。
Font對象
iText(包括Chunk對象)默認使用12pt的Helvetica字體,並且這個默認設置不能更改。如果需要使用其他字體的對象,需要創建一個類廠(factory class)來生產使用所需字體的對象。
Font font = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.WHITE);
創建一個Font對象,使用COURIER字體,12號,黑體,白色。
content.SetBackground(BaseColor.BLACK, 1F, 1F, 1F, 1F);
設置字體的背景色爲黑色。
doc.Add(content);

1.2 Phrase(短語)

Phrase對象是帶有行間距的Chunk對象的列表。

Document doc = new Document(PageSize.B5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("F:\\test.pdf", FileMode.Create));
doc.Open();

Font bold_underlined = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12);

Phrase phrase = new Phrase();
phrase.Leading = 50;
Chunk chunk1 = new Chunk("Hello!", bold_underlined);
Chunk chunk2 = new Chunk("How are you?", normal);
phrase.Add(chunk1);
phrase.Add(Chunk.NEWLINE);
phrase.Add(chunk2);

doc.Add(phrase);
Document doc = new Document(PageSize.B5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("F:\\test.pdf", FileMode.Create));
doc.Open();

Font bold_underlined = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12);

Phrase phrase = new Phrase();
Chunk chunk1 = new Chunk("Hello!", bold_underlined);
Chunk chunk2 = new Chunk("How are you?", normal);
phrase.Add(chunk1);
phrase.Add(Chunk.NEWLINE);
phrase.Add(chunk2);

doc.Add(phrase);

doc.Close();

在上面例子中,不需要再給PdfWriter對象設置行間距,Phrase會在兩個Chunk對象之間設置默認的行間距。iText中,默認行間距將設置爲Phrase或Paragraph中文字的大小的1.5倍。
在iTextSharp中,可對Phrase的Leading屬性賦值來設置行間距。
嵌入字體
Font類中可用的字體通常指的是standard type 1字體集。iText並不會將這些字體嵌入到文檔中。
Stand type 1字體集曾被稱作built-in或Base 14字體集。這些字體並不支持除美國和西歐以外的字符集。如果使用其他字符集,需要通過其他的方法。
使用中文字體
使用BaseFont對象可使iText到指定的位置尋找字體,並明確是否將字體嵌入到文檔中。
BaseFont simheiBase = BaseFont.CreateFont(@”C:\Windows\Fonts\simhei.ttf”, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font simheiFont = new Font(simheiBase, 12);
如果使用Chunk.NEWLINE換行,iText會使用默認字體,如果使用new Chunk(“\n”,NORMAL)則會使用給Chunk設置的字體。

1.3 Paragraph對象

Paragraph對象可理解爲能進行更多屬性設置的Phrase對象和一個換行符。
Paragraph類繼承自Phrase類。創建Paragraph實例的方法與創建Phrase實例的方法是一樣的。但Paragraph實例可以設置文本對齊方式、縮進以及段前段後空間。
設置對齊方式
設置Alignment屬性可以調整Paragraph對象中文字的對齊方式。如:
//設置Paragraph對象的對齊方式爲兩端對齊
contentPara1.Alignment = Element.ALIGN_JUSTIFIED;
默認情況使用左對齊。

設置縮進
iTextSharp中,Paragraph有三個屬性可以設置縮進:
//設置Paragraph對象的縮進
contentPara1.IndentationLeft = 20;
contentPara1.IndentationRight = 20;
contentPara1.FirstLineIndent = 20;
IndentationLeft屬性設置左側縮進。
IndentationRight屬性設置右側縮進。
FirstLineIndent屬性設置首行左側縮進。
三個值都可設爲正負值。

Paragraph之間的間距
iTextSharp中,通過設置Paragraph的SpacingBefore和SpacingAfter屬性調整Paragraph對象與之間或之後段落的間距。例如:
//設置Paragraph對象與後面Paragraph對象之間的間距
contentPara1.SpacingAfter = 36;

文字分行問題
iText默認的規則是儘可能多的將完整單詞放在同一行內。iText當遇到空格或連字符纔會分行,可以通過重新定義分隔符(split character)來改變這種規則。

分隔符(the split character)
使用nonbreaking space cha

參考資料:

1.iText官網
http://itextpdf.com/

2.SourceForge中的iTextSharp項目主頁
https://sourceforge.net/projects/itextsharp/

3.GitHub中的iTextSharp項目主頁
https://github.com/itext/itextsharp

發佈了61 篇原創文章 · 獲贊 78 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章