spire.pdf组件提供了两种将文档附加到pdf的方式。一种是通过调用pdfattachmentcollection类的add() 方法将文档作为文件附件添加到pdf,另一种则是将文档作为注释附加到pdf的页面中。
以下代码将对这两种方式逐一进行讲述。
添加附件
将文档作为文件附件添加到pdf
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("test.pdf");
//加载需要附加的文档
pdfattachment attachment = new pdfattachment("new.pdf");
//将文档添加到原pdf文档的附件集合中
pdf.attachments.add(attachment);
//保存文档
pdf.savetofile("attachment1.pdf");
vb.net
'加载pdf文档
dim pdf as new pdfdocument("test.pdf")
'加载需要附加的文档
dim attachment as new pdfattachment("new.pdf")
'将文档添加到原pdf文档的附件集合中
pdf.attachments.add(attachment)
'保存文档
pdf.savetofile("attachment1.pdf")
将文档作为注释附加到pdf文档的页面
c#
//加载pdf文档
pdfdocument doc = new pdfdocument("test.pdf");
//给文档添加一个新页面
pdfpagebase page = doc.pages.add();
//添加文本到页面
pdftruetypefont font1 = new pdftruetypefont(new font("arial", 16f, system.drawing.fontstyle.bold));
page.canvas.drawstring("attachments:", font1, pdfbrushes.cornflowerblue, new point(50, 50));
//将文档作为注释添加到页面
pdftruetypefont font2 = new pdftruetypefont(new font("arial", 12f, system.drawing.fontstyle.bold));
pointf location = new pointf(52, 80);
string label = "report.docx";
byte[] data = file.readallbytes("report.docx");
sizef size = font2.measurestring(label);
rectanglef bounds = new rectanglef(location, size);
page.canvas.drawstring(label, font2, pdfbrushes.mediumpurple, bounds);
bounds = new rectanglef(bounds.right 3, bounds.top, font2.height / 2, font2.height);
pdfattachmentannotation annotation1 = new pdfattachmentannotation(bounds, "report.docx", data);
annotation1.color = color.purple;
annotation1.flags = pdfannotationflags.nozoom;
annotation1.icon = pdfattachmenticon.graph;
annotation1.text = "report.docx";
(page as pdfnewpage).annotations.add(annotation1);
//保存文档
doc.savetofile("attachment2.pdf");
vb.net
'加载pdf文档
dim doc as new pdfdocument("test.pdf")
'给文档添加一个新页面
dim page as pdfpagebase = doc.pages.add()
'添加文本到页面
dim font1 as new pdftruetypefont(new font("arial", 16f, system.drawing.fontstyle.bold))
page.canvas.drawstring("attachments:", font1, pdfbrushes.cornflowerblue, new point(50, 50))
'将文档作为注释添加到页面
dim font2 as new pdftruetypefont(new font("arial", 12f, system.drawing.fontstyle.bold))
dim location as new pointf(52, 80)
dim label as [string] = "report.docx"
dim data as byte() = file.readallbytes("report.docx")
dim size as sizef = font2.measurestring(label)
dim bounds as new rectanglef(location, size)
page.canvas.drawstring(label, font2, pdfbrushes.mediumpurple, bounds)
bounds = new rectanglef(bounds.right 3, bounds.top, font2.height / 2, font2.height)
dim annotation1 as new pdfattachmentannotation(bounds, "report.docx", data)
annotation1.color = color.purple
annotation1.flags = pdfannotationflags.nozoom
annotation1.icon = pdfattachmenticon.graph
annotation1.text = "report.docx"
trycast(page, pdfnewpage).annotations.add(annotation1)
'保存文档
doc.savetofile("attachment2.pdf")
获取附件
根据附件添加方式的不同,获取附件也分为以下两种相应的方式。
获取文件附件
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("attachment1.pdf");
//获取文档的第一个文件附件
pdfattachment attachment = pdf.attachments[0];
//获取该附件的信息
console.writeline("name: {0}", attachment.filename);
console.writeline("mimetype: {0}", attachment.mimetype);
console.writeline("description: {0}", attachment.description);
console.writeline("creation date: {0}", attachment.creationdate);
console.writeline("modification date: {0}", attachment.modificationdate);
//将附件的数据写入到新文档
file.writeallbytes(attachment.filename, attachment.data);
console.readkey();
vb.net
'加载pdf文档
dim pdf as new pdfdocument("attachment1.pdf")
'获取文档的第一个文件附件
dim attachment as pdfattachment = pdf.attachments(0)
'获取该附件的信息
console.writeline("name: {0}", attachment.filename)
console.writeline("mimetype: {0}", attachment.mimetype)
console.writeline("description: {0}", attachment.description)
console.writeline("creation date: {0}", attachment.creationdate)
console.writeline("modification date: {0}", attachment.modificationdate)
'将附件的数据写入到新文档
file.writeallbytes(attachment.filename, attachment.data)
console.readkey()
获取注释附件
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("attachment2.pdf");
//实例化一个list并将文档内所有页面的attachment annotations添加到该list
list attaches = new list();
foreach (pdfpagebase page in pdf.pages)
{
foreach (pdfannotation annotation in page.annotationswidget)
{
attaches.add(annotation as pdfattachmentannotationwidget);
}
}
//遍历list,将附件数据写入到新文档
for (int i = 0; i < attaches.count; i )
{
file.writeallbytes(attaches[i].filename, attaches[i].data);
}
vb.net
'加载pdf文档
dim pdf as new pdfdocument("attachment2.pdf")
'实例化一个list并将文档内所有页面的attachment annotations添加到该list
dim attaches as new list()
for each page as pdfpagebase in pdf.pages
for each annotation as pdfannotation in page.annotationswidget
attaches.add(trycast(annotation, pdfattachmentannotationwidget))
next
next
'遍历list,将附件数据写入到新文档
for i as integer = 0 to attaches.count - 1
file.writeallbytes(attaches(i).filename, attaches(i).data)
next
删除附件
删除文件附件
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("attachment1.pdf");
//删除文档的所有文件附件
for (int i = 0; i < pdf.attachments.count; i )
{
pdf.attachments.removeat(i);
}
//保存文档
pdf.savetofile("remove.pdf");
vb.net
'加载pdf文档
dim pdf as new pdfdocument("attachment1.pdf")
'删除文档的所有文件附件
for i as integer = 0 to pdf.attachments.count - 1
pdf.attachments.removeat(i)
next
'保存文档
pdf.savetofile("remove.pdf")
删除注释附件
c#
//加载pdf文档
pdfdocument pdf = new pdfdocument("attachment2.pdf");
//删除文档的所有注释附件
foreach (pdfpagebase page in pdf.pages)
{
for (int i = 0; i < page.annotationswidget.count; i )
{
pdfannotation annotation = page.annotationswidget[i] as pdfattachmentannotationwidget;
page.annotationswidget.remove(annotation);
}
}
//保存文档
pdf.savetofile("result.pdf");
vb.net
'加载pdf文档
dim pdf as new pdfdocument("attachment2.pdf")
'删除文档的所有注释附件
for each page as pdfpagebase in pdf.pages
for i as integer = 0 to page.annotationswidget.count - 1
dim annotation as pdfannotation = trycast(page.annotationswidget(i), pdfattachmentannotationwidget)
page.annotationswidget.remove(annotation)
next
next
'保存文档
pdf.savetofile("result.pdf")