前言
附件在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的页面中。
该方法是通过调用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); 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. string fileName,1)">byte[] data); The rectangle. stream: The stream specifying the content of the annotation's embedded file. fileName,Stream stream); override string FileName { get; set; } Summary: Gets or Sets attachment's icon. public PdfAttachmentIcon Icon { ; } protected void Initialize(); Save(); } }
代码段:
加载PDF文档 PdfDocument doc = 给文档添加一个新页面 pdfpageBase page = doc.Pages.Add(); 添加文本到页面 PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font(Arial,16f,System.Drawing.FontStyle.Bold)); page.Canvas.DrawString(Attachments:",font1,PdfBrushes.CornflowerBlue,1)">new Point(50,50)); 将文档作为注释添加到页面 PdfTrueTypeFont font2 = stem.Drawing.FontStyle.Bold)); PointF location = new PointF(52,1)">80); String label = Report.docx; byte[] data = File.ReadAllBytes(); 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 / 2new PdfAttachmentAnnotation(bounds,data); annotation1.Color = Color.Purple; annotation1.Flags = PdfAnnotationFlags.NoZoom; annotation1.Icon = PdfAttachmentIcon.Graph; annotation1.Text = ; (page as PdfNewPage).Annotations.Add(annotation1); 保存文档 doc.SavetoFile(Attachment2.pdf");
2. 获取附件
获取文件附件时,我们还可以获取附件的信息如文件名,MimeType,描述,创建日期和修改日期等。
获取文档的第一个文件附件 PdfAttachment attachment = pdf.Attachments[0]; 获取该附件的信息 Console.WriteLine(Name: {0}MimeType: {0}Description: {0}Creation Date: {0}Modification Date: {0}将附件的数据写入到新文档 File.WriteallBytes(attachment.FileName,attachment.Data); Console.ReadKey();
2.2 获取注释附件
实例化一个list并将文档内所有页面的Attachment annotations添加到该list List<PdfAttachmentAnnotationWidget> attaches = new List<PdfAttachmentAnnotationWidget>(); foreach (pdfpageBase page in pdf.Pages) { foreach (PdfAnnotation annotation page.AnnotationsWidget) { attaches.Add(annotation PdfAttachmentAnnotationWidget); } } 遍历list,将附件数据写入到新文档 for (int i = 0; i < attaches.Count; i++) { File.WriteallBytes(attaches[i].FileName,attaches[i].Data); }
3. 删除附件
3.2 删除注释附件
删除文档的所有注释附件 0; i < page.AnnotationsWidget.Count; i++) { PdfAnnotation annotation = page.AnnotationsWidget[i] PdfAttachmentAnnotationWidget; page.AnnotationsWidget.Remove(annotation); } } Result.pdf");
总结:
本文只对该dll的部分功能做了简单的介绍,如果需要了解更多内容,可以去官网或NuGet下载dll进行测试。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。