C#添加、获取、删除PDF附件实例代码

所属分类: 软件编程 / C#教程 阅读数: 93
收藏 0 赞 0 分享

概述

附件,指随同文件发出的有关文件或物品。在PDF文档中,我们可以添加同类型的或其他类型的文档作为附件内容,而PDF中附件也可以分为两种存在形式,一种是附件以普通文件形式存在,另一种是以注释的形式存在。在下面的示例中介绍了如何分别添加以上两种形式的PDF附件。此外,根据PDF附件的不同添加方式,我们在获取PDF附件信息或删除PDF附件时,也可以分情况来执行操作。

工具使用

pire.PDF for .NET 4.0

代码示例(供参考)

 1.添加PDF附件

   1.1 以普通文档形式添加附件

using Spire.Pdf;
using Spire.Pdf.Attachments; 
namespace AddAttachment_PDF
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //创建一个PdfDocument类对象,加载测试文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("sample.pdf"); 
 
 //初始化PdfAttachment类实例,加载需要附加的文档 
 PdfAttachment attachment = new PdfAttachment("New.pdf"); 
 
 //将文档添加到原PDF文档的附件集合中 
 pdf.Attachments.Add(attachment); 
 
 //保存并打开文档 
 pdf.SaveToFile("Attachment1.pdf"); 
 System.Diagnostics.Process.Start("Attachment1.pdf"); 
 } 
 }
}

测试结果:

1.2 以文档注释形式添加附件

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.IO; 
namespace AddAttachment2
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //创建一个PdfDocument类对象,加载测试文档 
 PdfDocument doc = new PdfDocument("sample.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 = "sample.docx"; 
 byte[] data = File.ReadAllBytes("sample.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, "sample.docx", data); 
 annotation1.Color = Color.Purple; 
 annotation1.Flags = PdfAnnotationFlags.NoZoom; 
 annotation1.Icon = PdfAttachmentIcon.Graph; 
 annotation1.Text = "sample.docx"; 
 (page as PdfNewPage).Annotations.Add(annotation1); 
 
 //保存并打开文档 
 doc.SaveToFile("Attachment2.pdf"); 
 System.Diagnostics.Process.Start("Attachment2.pdf"); 
 } 
 }
 }

2.获取PDF附件

   2.1 获取文件附件

using Spire.Pdf;
using Spire.Pdf.Attachments;
using System;
using System.IO; 
namespace GetAttachment_PDF
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //创建PDF文档,加载测试文件 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("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(); 
 } 
 }
 }

测试结果:

2.2 获取注释附件

using Spire.Pdf;
using Spire.Pdf.Annotations;
using System.Collections.Generic;
using System.IO; 
namespace GetAttachment2
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //加载PDF文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("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 = 0; i < attaches.Count; i++) 
 { 
 File.WriteAllBytes(attaches[i].FileName, attaches[i].Data); 
 } 
 } 
 }
 }

注释附件读取结果:

3.删除PDF附件

   3.1 删除文件附件

using Spire.Pdf; 
namespace DeleteAttachment_PDF
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //加载PDF文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("Attachment1.pdf"); 
 
 //删除文档的所有文件附件 
 for (int i = 0; i < pdf.Attachments.Count; i++) 
 { 
 pdf.Attachments.RemoveAt(i); 
 } 
 
 //保存并打开文档 
 pdf.SaveToFile("Remove.pdf"); 
 System.Diagnostics.Process.Start("Remove.pdf"); 
 } 
 }
 }

   3.2 删除注释附件

using Spire.Pdf; 
namespace DeleteAttachment_PDF
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //加载PDF文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("Attachment1.pdf"); 
 
 //删除文档的所有文件附件 
 for (int i = 0; i < pdf.Attachments.Count; i++) 
 { 
 pdf.Attachments.RemoveAt(i); 
 } 
 
 //保存并打开文档 
 pdf.SaveToFile("Remove.pdf"); 
 System.Diagnostics.Process.Start("Remove.pdf"); 
 } 
 }
 }
 3.2 删除注释附件

using Spire.Pdf;
using Spire.Pdf.Annotations; 
namespace DeleteAttachment2
 { 
 class Program 
 { 
 static void Main(string[] args) 
 { 
 //加载PDF文档 
 PdfDocument pdf = new PdfDocument(); 
 pdf.LoadFromFile("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"); 
 System.Diagnostics.Process.Start("Result.pdf"); 
 } 
 }
 }

调试程序后,生成的文档就没有附件了。

以上就是C#添加、获取、删除PDF附件实例代码的详细内容,更多关于C#添加、获取、删除PDF附件的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

C#获取并修改文件扩展名的方法

这篇文章主要介绍了C#获取并修改文件扩展名的方法,实例分析了C#编程方式修改文件扩展名的技巧,涉及Path类的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C#遍历操作系统下所有驱动器的方法

这篇文章主要介绍了C#遍历操作系统下所有驱动器的方法,涉及C#中DriveInfo类GetDrivers方法的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#遍历指定目录下所有文件的方法

这篇文章主要介绍了C#遍历指定目录下所有文件的方法,实例分析了C#中GetFiles()方法遍历文件的使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#拷贝文件简单实现方法

这篇文章主要介绍了C#拷贝文件简单实现方法,主要分析了FileInfo类中CopyTo方法针对文件复制的操作技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

C#获取文件相关信息的方法

这篇文章主要介绍了C#获取文件相关信息的方法,涉及C#中FileInfo类的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#交错数组用法实例

这篇文章主要介绍了C#交错数组用法,较为详细的分析了交错数组的概念、用法并实例分析了交错数组的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现在前端网页弹出警告对话框(alert)的方法

这篇文章主要介绍了C#实现在前端网页弹出警告对话框(alert)的方法,涉及C#通过自定义函数调用window.alert方法弹出对话框的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现控制Windows系统关机、重启和注销的方法

这篇文章主要介绍了C#实现控制Windows系统关机、重启和注销的方法,涉及C#调用windows系统命令实现控制开机、关机等操作的技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取Excel并转化成XML的方法

这篇文章主要介绍了C#读取Excel并转化成XML的方法,实例分析了C#操作Excel及XML文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#创建一个Word并打开的方法

这篇文章主要介绍了C#创建一个Word并打开的方法,实例分析了C#操作word的常用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多