除文本和图形外,PDF 文件还可以包含作为附件嵌入的完整文件。这使得成套文档的交换更加方便和可靠。Spire.PDF 提供两种方式来添加附件:
- 文档级附件(Document Level Attachment):附加在 PDF 文档级别的文件不会出现在页面上,只能在 PDF 阅读器的"附件"面板中查看。
- 注释级附件(Annotation Attachment):文件会被添加到页面的特定位置。此类附件以回形针图标显示在页面上,审阅者可双击图标打开文件。
本文演示如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中向 PDF 文档添加或删除这两种类型的附件。
安装 Spire.PDF for .NET
首先,您需要将 Spire.PDF for .NET 包中的 DLL 文件作为引用添加到您的 .NET 项目中。您可以通过此链接下载这些 DLL 文件,或通过 NuGet 进行安装。
cs
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中向 PDF 添加附件
通过使用 PdfDocument.Attachments.Add() 方法,可以轻松将附件添加到"附件"面板。以下是具体步骤:
-
创建一个 PdfDocument 对象。
-
使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
-
基于外部文件创建一个 PdfAttachment 对象。
-
使用 PdfDocument.Attachments.Add() 方法将附件添加到 PDF 中。
-
使用 PdfDocument.SaveToFile() 方法将文档保存为新的 PDF 文件。
示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Attachments;
namespace AttachFilesToPDF
{
class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//加载示例 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//基于外部文件创建 PdfAttachment 对象
PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\Data.xlsx");
//将附件添加到 PDF
doc.Attachments.Add(attachment);
//保存文档
doc.SaveToFile("Attachment.pdf");
}
}
}
在 C# 和 VB.NET 中向 PDF 添加注释级附件
注释级附件既会显示在"附件"面板中,也会出现在文档的特定页面上。
示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.IO;
namespace AnnotationAttachment
{
class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//加载示例 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//获取指定页
PdfPageBase page = doc.Pages[0];
//在 PDF 上绘制文字标签
String label = "Here is the report:";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Bold), true);
float x = 35;
float y = doc.Pages[0].ActualSize.Height - 220;
page.Canvas.DrawString(label, font, PdfBrushes.Red, x, y);
//基于外部文件创建 PdfAttachmentAnnotation 对象
String filePath = "C:\\Users\\Administrator\\Desktop\\Report.pptx";
byte[] data = File.ReadAllBytes(filePath);
SizeF size = font.MeasureString(label);
RectangleF bounds = new RectangleF((float)(x + size.Width + 5), (float)y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bounds, "Report.docx", data);
annotation.Color = Color.Purple;
annotation.Flags = PdfAnnotationFlags.Default;
annotation.Icon = PdfAttachmentIcon.Graph;
annotation.Text = "Click here to open the file";
//将注释附件添加到 PDF 页面
page.AnnotationsWidget.Add(annotation);
//保存文档
doc.SaveToFile("Annotation.pdf");
}
}
}
在 C# 和 VB.NET 中从 PDF 中移除附件
可以通过 PdfDocument.Attachments 属性访问 PDF 文档中的附件,并使用 PdfAttachmentCollection 对象的 RemoveAt() 方法或 Clear() 方法来删除附件。
示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Attachments;
namespace RemoveAttachments
{
class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//加载 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Attachment.pdf");
//获取附件集合
PdfAttachmentCollection attachments = doc.Attachments;
//删除指定附件
attachments.RemoveAt(0);
//删除所有附件
//attachments.Clear();
//保存文件
doc.SaveToFile("DeleteAttachments.pdf");
}
}
}
在 C# 和 VB.NET 中从 PDF 中移除注释级附件
注释是基于页面的元素。若要获取文档中的所有注释,需要遍历每一页并获取该页上的注释。接着判断某个注释是否为注释级附件,最后通过注释集合的 Remove() 方法将其移除。
示例代码如下:
cs
using Spire.Pdf;
using Spire.Pdf.Annotations;
namespace RemoveAnnotationAttachments
{
class Program
{
static void Main(string[] args)
{
//创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//加载 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Annotation.pdf");
//遍历所有页面
for (int i = 0; i < doc.Pages.Count; i++)
{
//获取注释集合
PdfAnnotationCollection annotationCollection = doc.Pages[i].AnnotationsWidget;
//遍历注释
for (int j = 0; j < annotationCollection.Count; j++)
{
//判断当前注释是否为 PdfAttachmentAnnotationWidget 类型
if (annotationCollection[j] is PdfAttachmentAnnotationWidget)
{
//移除注释附件
annotationCollection.Remove((PdfAnnotation)annotationCollection[j]);
}
}
}
//保存文件
doc.SaveToFile("DeleteAnnotationAttachments.pdf");
}
}
}
申请临时许可证
如果您想去除生成文档中的评估提示,或解除功能限制,请申请一个 30 天的试用许可证。