使用C#代碼在PDF中添加或刪除附件
除文本和圖形外,PDF 文件還可以包含作為附件嵌入的完整文件。這使得成套文檔的交換更加方便和可靠。Spire.PDF 提供兩種方式來添加附件:
- 文檔級附件(Document Level Attachment):附加在 PDF 文檔級別的文件不會(huì)出現(xiàn)在頁面上,只能在 PDF 閱讀器的“附件”面板中查看。
- 注釋級附件(Annotation Attachment):文件會(huì)被添加到頁面的特定位置。此類附件以回形針圖標(biāo)顯示在頁面上,審閱者可雙擊圖標(biāo)打開文件。
本文演示如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中向 PDF 文檔添加或刪除這兩種類型的附件。
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for .NET 包中的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中。您可以通過此鏈接下載這些 DLL 文件,或通過 NuGet 進(jìn)行安裝。
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中向 PDF 添加附件
通過使用 PdfDocument.Attachments.Add() 方法,可以輕松將附件添加到“附件”面板。以下是具體步驟:
- 創(chuàng)建一個(gè) PdfDocument 對象。
- 使用 PdfDocument.LoadFromFile() 方法加載 PDF 文檔。
- 基于外部文件創(chuàng)建一個(gè) PdfAttachment 對象。
- 使用 PdfDocument.Attachments.Add() 方法將附件添加到 PDF 中。
- 使用 PdfDocument.SaveToFile() 方法將文檔保存為新的 PDF 文件。
示例代碼如下:
using Spire.Pdf;
using Spire.Pdf.Attachments;
namespace AttachFilesToPDF
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載示例 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//基于外部文件創(chuàng)建 PdfAttachment 對象
PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\Data.xlsx");
//將附件添加到 PDF
doc.Attachments.Add(attachment);
//保存文檔
doc.SaveToFile("Attachment.pdf");
}
}
}在 C# 和 VB.NET 中向 PDF 添加注釋級附件
注釋級附件既會(huì)顯示在“附件”面板中,也會(huì)出現(xiàn)在文檔的特定頁面上。
示例代碼如下:
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.IO;
namespace AnnotationAttachment
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載示例 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");
//獲取指定頁
PdfPageBase page = doc.Pages[0];
//在 PDF 上繪制文字標(biāo)簽
String label = "Here is the report:";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Bold), true);
float x = 35;
float y = doc.Pages[0].ActualSize.Height - 220;
page.Canvas.DrawString(label, font, PdfBrushes.Red, x, y);
//基于外部文件創(chuàng)建 PdfAttachmentAnnotation 對象
String filePath = "C:\\Users\\Administrator\\Desktop\\Report.pptx";
byte[] data = File.ReadAllBytes(filePath);
SizeF size = font.MeasureString(label);
RectangleF bounds = new RectangleF((float)(x + size.Width + 5), (float)y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bounds, "Report.docx", data);
annotation.Color = Color.Purple;
annotation.Flags = PdfAnnotationFlags.Default;
annotation.Icon = PdfAttachmentIcon.Graph;
annotation.Text = "Click here to open the file";
//將注釋附件添加到 PDF 頁面
page.AnnotationsWidget.Add(annotation);
//保存文檔
doc.SaveToFile("Annotation.pdf");
}
}
}在 C# 和 VB.NET 中從 PDF 中移除附件
可以通過 PdfDocument.Attachments 屬性訪問 PDF 文檔中的附件,并使用 PdfAttachmentCollection 對象的 RemoveAt() 方法或 Clear() 方法來刪除附件。
示例代碼如下:
using Spire.Pdf;
using Spire.Pdf.Attachments;
namespace RemoveAttachments
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Attachment.pdf");
//獲取附件集合
PdfAttachmentCollection attachments = doc.Attachments;
//刪除指定附件
attachments.RemoveAt(0);
//刪除所有附件
//attachments.Clear();
//保存文件
doc.SaveToFile("DeleteAttachments.pdf");
}
}
}在 C# 和 VB.NET 中從 PDF 中移除注釋級附件
注釋是基于頁面的元素。若要獲取文檔中的所有注釋,需要遍歷每一頁并獲取該頁上的注釋。接著判斷某個(gè)注釋是否為注釋級附件,最后通過注釋集合的 Remove() 方法將其移除。
示例代碼如下:
using Spire.Pdf;
using Spire.Pdf.Annotations;
namespace RemoveAnnotationAttachments
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建 PdfDocument 對象
PdfDocument doc = new PdfDocument();
//加載 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Annotation.pdf");
//遍歷所有頁面
for (int i = 0; i < doc.Pages.Count; i++)
{
//獲取注釋集合
PdfAnnotationCollection annotationCollection = doc.Pages[i].AnnotationsWidget;
//遍歷注釋
for (int j = 0; j < annotationCollection.Count; j++)
{
//判斷當(dāng)前注釋是否為 PdfAttachmentAnnotationWidget 類型
if (annotationCollection[j] is PdfAttachmentAnnotationWidget)
{
//移除注釋附件
annotationCollection.Remove((PdfAnnotation)annotationCollection[j]);
}
}
}
//保存文件
doc.SaveToFile("DeleteAnnotationAttachments.pdf");
}
}
}到此這篇關(guān)于使用C#代碼在PDF中添加或刪除附件的文章就介紹到這了,更多相關(guān)C# PDF添加或刪除附件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組
這篇文章主要介紹了深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組,隱式類型的數(shù)組通常與匿名類型以及對象初始值設(shè)定項(xiàng)和集合初始值設(shè)定項(xiàng)一起使用,需要的朋友可以參考下2016-01-01
Unity的AssetPostprocessor?Model動(dòng)畫函數(shù)使用案例深究
這篇文章主要介紹了Unity的AssetPostprocessor?Model動(dòng)畫函數(shù)使用案例的深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
這篇文章主要介紹了C#實(shí)現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實(shí)例形式分析了Gzip和Zip兩種壓縮操作實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06
DevExpress SplitContainerControl用法總結(jié)
這篇文章主要介紹了DevExpress SplitContainerControl用法,對初學(xué)者有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08

