我有一個名爲aa.pdf的現有PDF文檔。這PDF文件有3頁。我想使用iTextSharp在aa.pdf的第一頁底部添加PDF表單域(或文本)。同時,我也希望添加的PDF表單域(或添加的文本)可以鏈接到另一個aa.pdf頁面。例如,在單擊位於aa.pdf第一頁的PDF表單域(或文本)後,此PDF文檔將跳到第二頁。如何使用iTextSharp添加PDF表單域(或文本)並鏈接到現有PDF文檔的頁面底部?
如何使用iTextSharp實現上述功能?
謝謝。
我有一個名爲aa.pdf的現有PDF文檔。這PDF文件有3頁。我想使用iTextSharp在aa.pdf的第一頁底部添加PDF表單域(或文本)。同時,我也希望添加的PDF表單域(或添加的文本)可以鏈接到另一個aa.pdf頁面。例如,在單擊位於aa.pdf第一頁的PDF表單域(或文本)後,此PDF文檔將跳到第二頁。如何使用iTextSharp添加PDF表單域(或文本)並鏈接到現有PDF文檔的頁面底部?
如何使用iTextSharp實現上述功能?
謝謝。
要在PDF中創建鏈接,請使用可在Chunk
上設置的PdfAction
,它可以選擇性地添加到Paragraph
。您可以選擇幾種不同類型的操作,您可能感興趣的兩種操作是NEXTPAGE
操作和/或GotoLocalPage
操作。第一個項目完成它所說的內容並進入下一頁。這一個很好,因爲你不必擔心搞清楚你在打什麼頁碼。第二項允許您指定要去的特定頁碼。最簡單的形式,你可以這樣做:
Chunk ch = new Chunk("Go to next page").SetAction(new PdfAction(PdfAction.NEXTPAGE));
這將創建一個Chunk
,你可以在任何你想要的方式添加。使用現有的PDF時,可以通過幾種不同的方法將文本添加到頁面中。一種使用ColumnText
對象的方法,它有一個名爲SetSimpleColumn
的方法,它允許您定義一個可以添加元素的簡單矩形。
最後,PDF閱讀器不會自動在PDF中處理鏈接,除非在懸停時提供不同的光標。更具體地說,與超鏈接變成不同顏色的網頁不同,PDF不會改變鏈接的顏色,除非您告訴它們,所以在創建它們時應該牢記這一點。另外,修改PDF時,您通常不希望在此過程中覆蓋現有的PDF,因爲這會寫入您閱讀的內容。有時候它會起作用,但更多的時候它不會突破,有時也很微妙。相反,寫入第二個文件,當完成後,擦除第一個文件並重命名第二個文件。
以下代碼是針對iTextSharp 5.1.2.0的完整工作的C#2010 WinForms應用程序。代碼的第一部分在桌面上創建一個名爲「aa.pdf」的示例PDF。如果你已經有了這個文件,你可以評論這一部分,但它在這裏,所以其他人可以重現這個例子。第二部分基於「aa.pdf」創建一個名爲「bb.pdf」的新文件。它在第一頁的底部添加了兩個文本鏈接。第一個鏈接將PDF推進到下一頁,而第二個鏈接將PDF推進到特定的頁碼。有關具體實施細節,請參閱代碼中的註釋。
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//Files that we'll be working with
string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "aa.pdf");
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "bb.pdf");
//Create a standard PDF to test with, nothing special here
using (FileStream fs = new FileStream(inputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Create 10 pages with labels on each page
for (int i = 1; i <= 10; i++) {
doc.NewPage();
doc.Add(new Paragraph(String.Format("This is page {0}", i)));
}
doc.Close();
}
}
}
//For the OP, this is where you would start
//Declare some variables to be used later
ColumnText ct;
Chunk c;
//Bind a reader to the input file
PdfReader reader = new PdfReader(inputFile);
//PDFs don't automatically make hyperlinks a special color so we're specifically creating a blue font to use here
iTextSharp.text.Font BlueFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLUE);
//Create our new file
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
//Bind a stamper to our reader and output file
using (PdfStamper stamper = new PdfStamper(reader, fs)) {
Chunk ch = new Chunk("Go to next page").SetAction(new PdfAction(PdfAction.NEXTPAGE));
//Get the "over" content for page 1
PdfContentByte cb = stamper.GetOverContent(1);
//This example adds a link that goes to the next page
//Create a ColumnText object
ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(0, 0, 200, 20);
//Add some text and make it blue so that it looks like a hyperlink
c = new Chunk("Go to next page", BlueFont);
//Set the action to go to the next page
c.SetAction(new PdfAction(PdfAction.NEXTPAGE));
//Add the chunk to the ColumnText
ct.AddElement(c);
//Tell the system to process the above commands
ct.Go();
//This example add a link that goes to a specific page number
//Create a ColumnText object
ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(200, 0, 400, 20);
//Add some text and make it blue so that it looks like a hyperlink
c = new Chunk("Go to page 3", BlueFont);
//Set the action to go to a specific page number. This option is a little more complex, you also have to specify how you want to "fit" the document
c.SetAction(PdfAction.GotoLocalPage(3, new PdfDestination(PdfDestination.FIT), stamper.Writer));
//Add the chunk to the ColumnText
ct.AddElement(c);
//Tell the system to process the above commands
ct.Go();
}
}
this.Close();
}
}
}
http://stackoverflow.com/questions/1848930/how-to-add-a-form-field-to-an-existing-pdf-with-itextsharp – 2012-02-02 14:44:58
不是欺騙的可能重複,只是一個容易混淆的問題。 – 2012-02-02 14:50:58