C#添加、获取、删除PDF附件实例代码
作者:EiceblueSpire 发布时间:2023-06-23 11:41:46
概述
附件,指随同文件发出的有关文件或物品。在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");
}
}
}
调试程序后,生成的文档就没有附件了。
来源:https://www.imooc.com/article/288763


猜你喜欢
- 本文是作者结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化。不多说,上代码:package deal;import
- 本文实例讲述了Java比较两个List的值是否相等的方法。分享给大家供大家参考。具体如下:假设两个队列 {1,2,3,4} 和 {4,3,2
- public static string EncryptWithMD5(string source) &n
- 1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC。 2.定制WORD文档的模板文件,在C#中操作WORD模板,
- 前言纸上得来终觉浅,绝知此事要躬行一.会话引入什么是会话?会话用来识别不同的客户端,客户端和服务器之间发生的一系列连续的请求和响应的过程,当
- 目录Shiro简介Shiro快速入门SpringBoot-Shiro整合(最后会附上完整代码)附上最后的完整代码Shiro整合mybatis
- 前言在进行lua方法注册的时候, 大多数解决方案直接否定了泛型方法, 因为在lua侧难以表达出泛型, 以及lua的函数重载问题,函数重载问题
- 一、Sharding-JDBC简介Sharding-JDBC是Sharding-Sphere的一个产品,它有三个产品,分别是Sharding
- 工程加入依赖:<dependency><groupId>org.apache.pdfbox</groupId&
- 教程展示了如何在Spring应用程序中使用GenericApplicationContext 。在该示例中,我们创建了一个Spring Bo
- 废话不多说了,直接给大家贴java代码了。 import java.io.IOException;import sun.net.Telnet
- 字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。深刻认识Stri
- Java中,for-each循环简化了任何Collection或array的遍历过程,但并不是每个Java程序员都了解本文将要描述的for-
- 1. 简单工厂模式简介简单工厂模式(Simple Factory),又被称为"静态工厂方法模式"。它属于"创建
- 背景:在平时的开发中,我们时常会遇到下列场景公司的组织架构的数据存储与展示文件夹层级的数据存储与展示评论系统中,父评论与诸多子评论的数据存储
- 前言小小知识,不值一提,了解了也不能让你提高身价,但是不了解你就是比别人少知道点!事儿就是这么个事儿,直接正题吧! 直接看代码演示优先级当方
- 本文实例讲述了C++联合体union用法。分享给大家供大家参考。具体如下:我们应该按照C中的convention去使用union,这是我这篇
- // Path类 IO命名空间 静态类 不能创建对象类名.string str =@"E:\C#程序设计基础入门教程\(第十一天)
- 前言作为大数据家族中的重要一员,在大数据以及海量数据存储方面,hbase具有重要的地方,本篇将从java对hbase的操作上,进行详细的说明
- 在日常开发中经常遇到控件不能随着父容器大小的改变而且自动改变控件的所在位置和大小。以下是实现的代码 /// <summary>