C# 添加、获取及删除PDF附件

前言

附件在PDF文档中很常见,这些附件可以是PDF或其他类型的文件。在PDF中,附件有两种存在方式,一种是普通的文件附件(document-level file attachment),另一种是注释(annotation)。本文主要介绍如何在C#应用程序中给PDF文档添加附件以及从PDF文档获取附件、删除附件。

我们都知道.NET Framework 本身并没有直接操作PDF的类库,因此在.NET应用程序中操作PDF文档必须要借助第三方组件提供的dll。本文主要使用的是Free Spire.PDF组件的dll。

实现

1.  添加附件

以下代码将介绍两种将文档附加到PDF的方式。一种是将文档作为文件附件添加到PDF,另一种则是将文档作为注释附加到PDF的页面中。

1.1  将文档作为文件附件添加到PDF

该方法是通过调用PdfAttachmentCollection类的Add()方法将文档添加到PdfDocument对象的Attachments集合中。

//加载PDF文档
PdfDocument pdf = new PdfDocument("Test.pdf"); //加载需要附加的文档
PdfAttachment attachment = new PdfAttachment("New.pdf");
//将文档添加到原PDF文档的附件集合中
pdf.Attachments.Add(attachment); //保存文档
pdf.SaveToFile("Attachment1.pdf");

1.2  将文档作为注释(annotation)附加到PDF文档的页面

创建注释时,我们需要用到以下类PdfAttachmentAnnotation:

namespace Spire.Pdf.Annotations
{
// Summary:
// Represents an attachment annotation.
public class PdfAttachmentAnnotation : PdfFileAnnotation
{
//
// Parameters:
// rectangle:
// Bounds of the annotation.
//
// fileName:
// A string value specifying the full path to the file to be embedded in the
// PDF file.
public PdfAttachmentAnnotation(RectangleF rectangle, string fileName);
//
//
// Parameters:
// rectangle:
// Bounds of the annotation.
//
// fileName:
// A string value specifying the full path to the file to be embedded in the
// PDF file.
//
// data:
// A byte array specifying the content of the annotation's embedded file.
//
// Remarks:
// If both FileName and FileContent are specified, the FileContent takes precedence.
public PdfAttachmentAnnotation(RectangleF rectangle, string fileName, byte[] data);
//
//
// Parameters:
// rectangle:
// The rectangle.
//
// fileName:
// A string value specifying the full path to the file to be embedded in the
// PDF file.
//
// stream:
// The stream specifying the content of the annotation's embedded file.
//
// Remarks:
// If both FileName and FileContent are specified, the FileContent takes precedence.
public PdfAttachmentAnnotation(RectangleF rectangle, string fileName, Stream stream); public override string FileName { get; set; }
//
// Summary:
// Gets or Sets attachment's icon.
public PdfAttachmentIcon Icon { get; set; } protected override void Initialize();
protected override void Save();
}
}

代码段:

//加载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(, )); //将文档作为注释添加到页面
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, System.Drawing.FontStyle.Bold));
PointF location = new PointF(, );
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 + , bounds.Top, font2.Height / , 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");

2.  获取附件

根据附件添加方式的不同,获取附件也分为以下两种相应的方式。

2.1  获取文件附件

获取文件附件时,我们还可以获取附件的信息如文件名,MimeType,描述,创建日期和修改日期等。

//加载PDF文档
PdfDocument pdf = new PdfDocument("Attachment1.pdf"); //获取文档的第一个文件附件
PdfAttachment attachment = pdf.Attachments[]; //获取该附件的信息
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();

2.2  获取注释附件

//加载PDF文档
PdfDocument pdf = new PdfDocument("Attachment2.pdf"); //实例化一个list并将文档内所有页面的Attachment annotations添加到该list
List<PdfAttachmentAnnotationWidget> attaches = new List<PdfAttachmentAnnotationWidget>();
foreach (PdfPageBase page in pdf.Pages)
{
foreach (PdfAnnotation annotation in page.AnnotationsWidget)
{
attaches.Add(annotation as PdfAttachmentAnnotationWidget);
}
} //遍历list,将附件数据写入到新文档
for (int i = ; i < attaches.Count; i++)
{
File.WriteAllBytes(attaches[i].FileName, attaches[i].Data);
}

3.  删除附件

3.1  删除文件附件

//加载PDF文档
PdfDocument pdf = new PdfDocument("Attachment1.pdf"); //删除文档的所有文件附件
for (int i = ; i < pdf.Attachments.Count; i++)
{
pdf.Attachments.RemoveAt(i);
} //保存文档
pdf.SaveToFile("Remove.pdf");

3.2  删除注释附件

//加载PDF文档
PdfDocument pdf = new PdfDocument("Attachment2.pdf"); //删除文档的所有注释附件
foreach (PdfPageBase page in pdf.Pages)
{
for (int i = ; i < page.AnnotationsWidget.Count; i++)
{
PdfAnnotation annotation = page.AnnotationsWidget[i] as PdfAttachmentAnnotationWidget; page.AnnotationsWidget.Remove(annotation);
}
} //保存文档
pdf.SaveToFile("Result.pdf");

总结:

本文只对该dll的部分功能做了简单的介绍,如果需要了解更多内容,可以去官网NuGet下载dll进行测试。

C# 添加、获取及删除PDF附件的更多相关文章

  1. C# 添加、修改和删除PDF书签

    C# 添加.修改和删除PDF书签 有时候我们在阅读PDF文档时会遇到这样一种情况:PDF文档页数比较多,但是又没有书签,所以我们不能根据书签快速了解文档所讲解的内容,也不能点击书签快速跳转到相应的位置 ...

  2. Java添加、提取、替换和删除PDF图片

    (一)简介 这篇文章将介绍在Java中添加.提取.删除和替换PDF文档中的图片. 工具使用: Free Spire.PDF for JAVA 2.4.4(免费版) Intellij IDEA Jar包 ...

  3. JAVA 添加、修改和删除PDF书签

    当阅读篇幅较长的PDF文档时,为方便我们再次阅读时快速定位到上一次的阅读位置,可以插入一个书签进行标记:此外,对于文档中已有的书签,我们也可以根据需要进行修改或者删除等操作.本篇文章将通过Java编程 ...

  4. Java 添加、替换、删除PDF中的图片

    概述 本文介绍通过java程序向PDF文档添加图片,以及替换和删除PDF中已有的图片.另外,关于图片的操作还可参考设置PDF 图片背景.设置PDF图片水印.读取PDF中的图片.将PDF保存为图片等文章 ...

  5. 工具函数:cookie的添加、获取、删除

    cookie是浏览器存储的命名数据,作用是保存用户的信息,这样我们就可以用这些信息来做一些事了,但是cookie容量很小,只有4kb. 下面是我总结的cookie的添加.获取.删除的函数: cooki ...

  6. Java 添加、隐藏/显示、删除PDF图层

    本文介绍操作PDF图层的方法.可分为添加图层(包括添加线条.形状.字符串.图片等图层).隐藏或显示图层.删除图层等.具体可参考如下Java代码示例. 工具:Free Spire.PDF for Jav ...

  7. 利用jquery给指定的table动态添加一行、删除一行

    转自:http://www.cnblogs.com/linjiqin/p/3148181.html $("#mytable tr").find("td:nth-child ...

  8. 利用jquery给指定的table动态添加一行、删除一行,复制,值不重复等操作

    $("#mytable tr").find("td:nth-child(1)") 1表示获取每行的第一列$("#mytable tr").f ...

  9. iOS UIWebView 和 WKWebView 的 cookie 获取,设置,删除

    Cookie简介说到Cookie,或许有些小伙伴会比较陌生,有些小伙伴会比较熟悉.如果项目中,所有页面都是纯原生来实现的话,一般Cookie这个东西或许我们永远也不会接触到.但是,这里还是要说一下Co ...

随机推荐

  1. js arguments.callee & caller的用法及区别

    在函数内部,arguments.callee该属性是一个指针,指向拥有这个arguments对象的函数; 而函数对象的另一个属性:caller,这个属性保存着调用当前函数的函数的引用,如果是在全局作用 ...

  2. 51驱动LCD1602

    1602 采用标准的 16 脚接口,其中: 第 1 脚:VSS 为地电源 第 2 脚:VDD 接 5V 正电源 第 3 脚:V0 为液晶显示器对比度调整端,接正电源时对比度最弱,接地 电源时对比度最高 ...

  3. Java的内存泄漏

    内存泄漏是指,无用对象(不再使用的对象)持续占用内存或者无用对象的内存得不到及时释放,从而造成的内存浪费 就说是有一块内存你不需要再用了,但是呢你还保留着它的指针,那么这块内存就不会被回收 举个例子 ...

  4. 012-ViewState状态保持

    客户端的状态保持方案:ViewState.隐藏域.Cookies.控件状态.URL查询参数服务端的状态保持方案:Session(会话).Application.Caching(缓存).DataBase ...

  5. TFS2013 升级至TFS2015及项目的创建

    TFS2015已发布想体验下新特性 由于现有数据库已经是SQLSERVER2012 SP1 开发工具VS2013 都符合升级要求 现在体验下吧 1.先下载TFS2015 运行安装向导一路NEXT 直至 ...

  6. MySQL-基本sql命令

    关于环境的搭建和数据库的连接,我直接跳过,假设电脑上已经安装好了MySQL的环境,接下来直接进行数据库的操作:(虽然数据库不区分大小写,但是本文约定:命令用大写,用户变量和字段用小写) 1.创建数据库 ...

  7. redis 配置(1)

    redis配置密码 1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到 #requirepass foobared 去掉行前的注释,并修 ...

  8. EALayout 实践

    步骤: 1. 导入framework 1.0. 下载网址 1.1. 修改Build Setting -> other linker flags,添加 “-ObjC”(连接实现文件)和" ...

  9. PHP导出MYSQL数据库并压缩

    PHP可以一键导出MYSQL备份文件,并压缩存放,尽管phpMyAdmin有这功能,不过若你自己开发网站或者是为别人写CMS,你不应该要求别人用你程序的时候再去另外用phpMyAdmin备份MYSQL ...

  10. jQuery仿淘宝图片无缝滚动轮播

    自己前天,也就是1月8日的时候早上自己写了一个图片滚动轮播(基于jQuery). 其实几个月以前就有朋友问过我怎么做出和淘宝上面一样的滚动轮播,一直到现在也没有真正的写好,这次写得差不多了. 但是还有 ...