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

C# 添加、获取及删除PDF附件,第1张

概述前言

附件在PDF文档中很常见,这些附件可以是PDF或其他类型的文件。在PDF中,附件有两种存在方式,一种是普通的文件附件(document-level file attachment),另一种

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);             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 = 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.  获取附件

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

2.1  获取文件附件

获取文件附件时,我们还可以获取附件的信息如文件名,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添加到该ListList<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.1  删除文件附件

删除文档的所有文件附件0; i < pdf.Attachments.Count; i++){    pdf.Attachments.RemoveAt(i);} Remove.pdf");

 

3.2  删除注释附件

删除文档的所有注释附件0; i < page.AnnotationsWidget.Count; i++)    {        pdfAnnotation annotation = page.AnnotationsWidget[i]  pdfAttachmentAnnotationWidget;        page.AnnotationsWidget.Remove(annotation);                       }}Result.pdf");

 

总结:

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

总结

以上是内存溢出为你收集整理的C# 添加、获取及删除PDF附件全部内容,希望文章能够帮你解决C# 添加、获取及删除PDF附件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1213584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存