在Asp.Net中操作PDF – iTextSharp - 使用鏈接和書簽
轉自:http://www.cnblogs.com/CareySon/archive/2011/11/04/2236239.html
用戶和PDF文檔的交互可以通過錨(鏈接)和書簽進行,接著我前面iTextSharp的系列文章,本篇文章主要講通過iTextSharp創建的PDF中鏈接和書簽的基礎知識
鏈接
iTextSharp的Anchor對象和HTML中的錨非常相似,它們都允許創建鏈接到外部文檔或是內部文檔的鏈接。但和HTML中的鏈接不同的是,iTextSharp的鏈接默認沒有任何樣式,有鑒于此,我建議對于鏈接來說,都應該加下劃線并且使用藍色字體來告訴用戶這段文字有著鏈接的功能:
string path = Server.MapPath("PDFs");Document doc = new Document(); try
{
PdfWriter.GetInstance(doc, new FileStream(path + "/Anchors.pdf", FileMode.Create)); doc.Open(); Font link = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, new Color(0, 0, 255)); Anchor anchor = new Anchor("www.mikesdotnetting.com", link); anchor.Reference = "http://www.mikesdotnetting.com"; doc.Add(anchor);
}
catch (DocumentException dex)
{
Response.Write(dex.Message);
}
catch (IOException ioex)
{
Response.Write(ioex.Message);
}
finally
{
doc.Close();
}</pre>
上面代碼創建了一個外部鏈接,當點擊鏈接后會打開瀏覽器并導向指定鏈接.
內部鏈接在HTML中是通過為<a>標簽添加NAME屬性來實現的,iTextSharp也是用的這種方法:
Anchor click = new Anchor("Click to go to Target");click.Reference = "#target";
Paragraph p1 = new Paragraph();
p1.Add(click);
doc.Add(p1);
Paragraph p2 = new Paragraph();
p2.Add(new Chunk("\n\n\n\n\n\n\n\n"));
doc.Add(p2);
Anchor target = new Anchor("This is the Target");
target.Name = "target";
Paragraph p3 = new Paragraph();
p3.Add(target);
doc.Add(p3);</pre>
第一個段落包含一個就像HTML中<a>一樣的指向”#target”的錨記,第二個段落包含了多個換行符,第三個段落包含了一個錨記,并設置成和第一個段落引用名一樣的Name.結果是當你點擊”Click to go to target”時,PDF現在無論讀到哪了,都會導向這個錨記,現在”This is target”會出現在PDF頂端.
錨記的一個代替方案是使用Chunk類的SetLocalGoto()和 SetLocalDestination()方法:
Paragraph p4 = new Paragraph();p4.Add(new Chunk("Click "));
p4.Add(new Chunk("here", link).SetLocalGoto("GOTO"));
p4.Add(new Chunk(" to find local goto"));
p4.Add(new Chunk("\n\n\n\n\n\n\n\n\n"));
Paragraph p5 = new Paragraph();
p5.Add(new Chunk("Local Goto Destination").SetLocalDestination("GOTO"));
doc.Add(p4);
doc.Add(p5);</pre>
第一個段落使用的字體傳達的意思告訴用戶這個是一個超鏈接,Chunk.SetLocalGoto()接受一個String類型的參數,這個String是錨記的名稱。接下來又加入了幾個換行符。接下來使用了SetLocalDestination()方法設置當前位置的名稱,他和之前定義的SetLocalGoto()方法中的指定的位置名稱相對應。當生成PDF時,”Here”有下劃線并且是藍色的,點擊后會將PDF導向到”Local Goto Destination”處于最頂端。
書簽
很多時候你打開一個PDF時,你的PDF瀏覽程序都會顯示一個數狀的文檔結構,每一個樹枝或者葉節點都會鏈接到一章或者一節,iTextSharp通過Chapter和Section對象提供了生成樹狀導航的功能.
書簽最大的對象是Chapter,每一個Chapter對象都會另起一頁,而Section對象必須加在Chapter對象或其他Section對象之中:
Chapter chapter1 = new Chapter(new Paragraph("This is Chapter 1"),1);Section section1 = chapter1.AddSection(20f, "Section 1.1", 2);
Section section2 = chapter1.AddSection(20f, "Section 1.2", 2);
Section subsection1 = section2.AddSection(20f, "Subsection 1.2.1", 3);
Section subsection2 = section2.AddSection(20f, "Subsection 1.2.2", 3);
Section subsubsection = subsection2.AddSection(20f, "Sub Subsection 1.2.2.1", 4);
Chapter chapter2 = new Chapter(new Paragraph("This is Chapter 2"), 1);
Section section3 = chapter2.AddSection("Section 2.1", 2);
Section subsection3 = section3.AddSection("Subsection 2.1.1", 3);
Section section4 = chapter2.AddSection("Section 2.2", 2);
chapter1.BookmarkTitle = "Changed Title";
chapter1.BookmarkOpen = true;
chapter2.BookmarkOpen = false;
doc.Add(chapter1);
doc.Add(chapter2);</pre>
上面的圖片清楚解釋了之前的代碼。代碼開始,創建了一個Chapter對象并傳入了一個Paragraph對象作為參數,第二個參數表示當前章是第幾章。這里設置為1,然后把一個Section對象加入到當前章,為Section的構造函數傳入的三個參數分別為,Float類型的參數代表左邊的縮進,第二個String參數代表在書簽和頁面上顯示的節點名稱,最后一個參數是當前節點的縮進層次.這個例子中,Section 1.1是樹的第二層,SubSection1被加入了Section1.1所以SubSection1是樹的第三層。弄懂了上面的解釋后,剩下的代碼看起來就直接多了。
最后的代碼(chapter1.BookmarkTitle = "Changed Title";)可以通過設置BookmarkTitle屬性將樹狀導航的名字改了,上面代碼將Chapter1的BookmarkOpen設置為True將Chapter2的BookmarkOpen設置為False.最后將所有Chapter加入到Document中.
Chapter和Section對內存的消耗是驚人的,所以對于它們的使用也要十分謹慎,如果你需要創建手冊一類的PDF文檔時,最好將其安排在你的Web服務器空閑時做。