C# 如何使用iTextSharp在现有PDF文档的页面底部添加PDF表单字段(或文本)和链接?
我有一个名为aa.PDF的现有PDF文档。此PDF文档有3页。我想使用iTextSharp在aa.PDF的第一页底部添加一个PDF表单字段(或文本) 同时,我也希望添加的PDF表单字段(或添加的文本)可以链接到aa.PDF的另一个页面。例如,单击aa.PDF第一页中的PDF表单字段(或文本)后,此PDF文档将跳转到第二页 如何使用iTextSharp实现上述功能C# 如何使用iTextSharp在现有PDF文档的页面底部添加PDF表单字段(或文本)和链接?,c#,.net,pdf,itextsharp,C#,.net,Pdf,Itextsharp,我有一个名为aa.PDF的现有PDF文档。此PDF文档有3页。我想使用iTextSharp在aa.PDF的第一页底部添加一个PDF表单字段(或文本) 同时,我也希望添加的PDF表单字段(或添加的文本)可以链接到aa.PDF的另一个页面。例如,单击aa.PDF第一页中的PDF表单字段(或文本)后,此PDF文档将跳转到第二页 如何使用iTextSharp实现上述功能 谢谢。要在PDF中创建链接,您可以使用pdp版本,该版本可以在区块上设置,该区块可以选择性地添加到段落。有几种不同类型的操作可供选择,
谢谢。要在PDF中创建链接,您可以使用
pdp版本
,该版本可以在区块
上设置,该区块可以选择性地添加到段落
。有几种不同类型的操作可供选择,您可能感兴趣的两种类型是NEXTPAGE
操作和/或GotoLocalPage
操作。第一项按它所说的做,并转到下一页。这一个很好,因为你不必担心弄清楚你的页码。第二项允许您指定要转到的特定页码。以最简单的形式,您可以执行以下操作:
Chunk ch = new Chunk("Go to next page").SetAction(new PdfAction(PdfAction.NEXTPAGE));
这将创建一个块
,您可以以任何方式添加该块。使用现有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();
}
}
}
使用系统;
使用System.IO;
使用System.Windows.Forms;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
命名空间Windows窗体应用程序1{
公共部分类Form1:Form{
公共表格1(){
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e){
//我们将处理的文件
字符串inputFile=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),“aa.pdf”);
字符串outputFile=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),“bb.pdf”);
//创建一个标准的PDF进行测试,这里没有什么特别的
使用(FileStream fs=newfilestream(inputFile,FileMode.Create,FileAccess.Write,FileShare.None)){
使用(文档文档=新文档(页面大小.字母)){
使用(PdfWriter writer=PdfWriter.GetInstance(doc,fs)){
doc.Open();
//创建10页,每页上都有标签
对于(int i=1;我可能重复的不是重复的问题,只是一个措词混乱的问题。