C#/VB.NET 如何添加、获取、删除PDF附件

时间:2022-05-16 01:44:24

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

要点索引

1.添加PDF附件
1.1 以普通文档形式添加附件
1.2 以文档注释形式添加附件
2.获取PDF附件
2.1 获取文件附件
2.2 获取注释附件
3.删除PDF附件
3.1 删除文件附件
3.2 删除注释附件

工具使用

Spire.PDF for .NET 4.0

示例操纵 1.添加PDF附件 1.1 以普通文档形式添加附件 C 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"); } } }

测试功效:

C#/VB.NET 如何添加、获取、删除PDF附件

VB.NET Imports Spire.Pdf Imports Spire.Pdf.Attachments Namespace AddAttachment_PDF Class Program Private Shared Sub Main(ByVal args As String()) Dim pdf As PdfDocument = New PdfDocument() pdf.LoadFromFile("sample.pdf") Dim attachment As PdfAttachment = New PdfAttachment("New.pdf") pdf.Attachments.Add(attachment) pdf.SaveToFile("Attachment1.pdf") System.Diagnostics.Process.Start("Attachment1.pdf") End Sub End Class End Namespace 1.2 以文档注释形式添加附件 Csharp 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"); } } }

测试功效:

C#/VB.NET 如何添加、获取、删除PDF附件

VB.NET Imports Spire.Pdf Imports Spire.Pdf.Annotations Imports Spire.Pdf.Graphics Imports System Imports System.Drawing Imports System.IO Namespace AddAttachment2 Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As PdfDocument = New PdfDocument("sample.pdf") Dim page As PdfPageBase = doc.Pages.Add() Dim font1 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 16F, System.Drawing.FontStyle.Bold)) page.Canvas.DrawString("Attachments:", font1, PdfBrushes.CornflowerBlue, New Point(50, 50)) Dim font2 As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 12F, System.Drawing.FontStyle.Bold)) Dim location As PointF = New PointF(52, 80) Dim label As String = "sample.docx" Dim data As Byte() = File.ReadAllBytes("sample.docx") Dim size As SizeF = font2.MeasureString(label) Dim bounds As RectangleF = 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) Dim annotation1 As PdfAttachmentAnnotation = New PdfAttachmentAnnotation(bounds, "sample.docx", data) annotation1.Color = Color.Purple annotation1.Flags = PdfAnnotationFlags.NoZoom annotation1.Icon = PdfAttachmentIcon.Graph annotation1.Text = "sample.docx" (TryCast(page, PdfNewPage)).Annotations.Add(annotation1) doc.SaveToFile("Attachment2.pdf") System.Diagnostics.Process.Start("Attachment2.pdf") End Sub End Class End Namespace 2.获取PDF附件 2.1 获取文件附件 Csharp 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(); } } }