博客 / 詳情

返回

如何使用C#代碼在 PDF 中添加或刪除附件

除文本和圖形外,PDF 文件還可以包含作為附件嵌入的完整文件。這使得成套文檔的交換更加方便和可靠。Spire.PDF 提供兩種方式來添加附件:

  • 文檔級附件(Document Level Attachment):附加在 PDF 文檔級別的文件不會出現在頁面上,只能在 PDF 閲讀器的“附件”面板中查看。
  • 註釋級附件(Annotation Attachment):文件會被添加到頁面的特定位置。此類附件以回形針圖標顯示在頁面上,審閲者可雙擊圖標打開文件。

本文演示如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中向 PDF 文檔添加或刪除這兩種類型的附件。

安裝 Spire.PDF for .NET

首先,您需要將 Spire.PDF for .NET 包中的 DLL 文件作為引用添加到您的 .NET 項目中。您可以通過此鏈接下載這些 DLL 文件,或通過 NuGet 進行安裝。

PM> Install-Package Spire.PDF

在 C# 和 VB.NET 中向 PDF 添加附件

通過使用 PdfDocument.Attachments.Add() 方法,可以輕鬆將附件添加到“附件”面板。以下是具體步驟:

  1. 創建一個 PdfDocument 對象。
  2. 使用 PdfDocument.LoadFromFile() 方法加載 PDF 文檔。
  3. 基於外部文件創建一個 PdfAttachment 對象。
  4. 使用 PdfDocument.Attachments.Add() 方法將附件添加到 PDF 中。
  5. 使用 PdfDocument.SaveToFile() 方法將文檔保存為新的 PDF 文件。

示例代碼如下:

using Spire.Pdf;
using Spire.Pdf.Attachments;

namespace AttachFilesToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建 PdfDocument 對象
            PdfDocument doc = new PdfDocument();

            //加載示例 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

            //基於外部文件創建 PdfAttachment 對象
            PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\Data.xlsx");

            //將附件添加到 PDF
            doc.Attachments.Add(attachment);

            //保存文檔
            doc.SaveToFile("Attachment.pdf");
        }
    }
}

在 C# 和 VB.NET 中向 PDF 添加註釋級附件

註釋級附件既會顯示在“附件”面板中,也會出現在文檔的特定頁面上。

示例代碼如下:

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)
        {
            //創建 PdfDocument 對象
            PdfDocument doc = new PdfDocument();

            //加載示例 PDF 文件
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

            //獲取指定頁
            PdfPageBase page = doc.Pages[0];

            //在 PDF 上繪製文字標籤
            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);

            //基於外部文件創建 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)
        {
            //創建 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 中移除註釋級附件

註釋是基於頁面的元素。若要獲取文檔中的所有註釋,需要遍歷每一頁並獲取該頁上的註釋。接着判斷某個註釋是否為註釋級附件,最後通過註釋集合的 Remove() 方法將其移除。

示例代碼如下:

using Spire.Pdf;
using Spire.Pdf.Annotations;

namespace RemoveAnnotationAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建 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++)
                {
                    //判斷當前註釋是否為 PdfAttachmentAnnotationWidget 類型
                    if (annotationCollection[j] is PdfAttachmentAnnotationWidget)
                    {
                        //移除註釋附件
                        annotationCollection.Remove((PdfAnnotation)annotationCollection[j]);
                    }
                }
            }

            //保存文件
            doc.SaveToFile("DeleteAnnotationAttachments.pdf");
        }
    }
}

申請臨時許可證

如果您想去除生成文檔中的評估提示,或解除功能限制,請申請一個 30 天的試用許可證。

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.