91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-07-29 05:24:04 來源:網絡 閱讀:5434 作者:E_iceblue 欄目:編程語言

概述

附件,指隨同文件發出的有關文件或物品。在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 以普通文檔形式添加附件

Csharp

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();
        }
    }
}

測試結果:
C#/VB.NET 如何添加、獲取、刪除PDF附件

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Attachments
Imports System
Imports System.IO

Namespace GetAttachment_PDF

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim pdf As PdfDocument = New PdfDocument()
            pdf.LoadFromFile("Attachment1.pdf")
            Dim attachment As PdfAttachment = 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()
        End Sub
    End Class
End Namespace

2.2 獲取注釋附件

Csharp

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);
            }

        }
    }
}

測試結果:
C#/VB.NET 如何添加、獲取、刪除PDF附件

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports System.Collections.Generic
Imports System.IO

Namespace GetAttachment2

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim pdf As PdfDocument = New PdfDocument()
            pdf.LoadFromFile("Attachment2.pdf")
            Dim attaches As List(Of PdfAttachmentAnnotationWidget) = New List(Of PdfAttachmentAnnotationWidget)()
            For Each page As PdfPageBase In pdf.Pages
                For Each annotation As PdfAnnotation In page.AnnotationsWidget
                    attaches.Add(TryCast(annotation, PdfAttachmentAnnotationWidget))
                Next
            Next

            For i As Integer = 0 To attaches.Count - 1
                File.WriteAllBytes(attaches(i).FileName, attaches(i).Data)
            Next
        End Sub
    End Class
End Namespace

3.刪除PDF附件

3.1 刪除文件附件

Csharp

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");

        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace DeleteAttachment_PDF

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim pdf As PdfDocument = New PdfDocument()
            pdf.LoadFromFile("Attachment1.pdf")
            For i As Integer = 0 To pdf.Attachments.Count - 1
                pdf.Attachments.RemoveAt(i)
            Next

            pdf.SaveToFile("Remove.pdf")
            System.Diagnostics.Process.Start("Remove.pdf")
        End Sub
    End Class
End Namespace

3.2 刪除注釋附件

Csharp

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");
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Annotations

Namespace DeleteAttachment2

    Class Program

        Private Shared Sub Main(ByVal args As String())
            Dim pdf As PdfDocument = New PdfDocument()
            pdf.LoadFromFile("Attachment2.pdf")
            For Each page As PdfPageBase In pdf.Pages
                For i As Integer = 0 To page.AnnotationsWidget.Count - 1
                    Dim annotation As PdfAnnotation = TryCast(page.AnnotationsWidget(i), PdfAttachmentAnnotationWidget)
                    page.AnnotationsWidget.Remove(annotation)
                Next
            Next

            pdf.SaveToFile("Result.pdf")
            System.Diagnostics.Process.Start("Result.pdf")
        End Sub
    End Class
End Namespace

調試程序后,生成的文檔就沒有附件了。

以上全部內容為本次關于“C#/VB.NT操作PDF附件”的方法講述。代碼供參考,希望能給各位提供一定解決問題的思路。

( 如需轉載本文,請注明出處。)

感謝閱讀!

《本文完》

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

pdf
AI

阿瓦提县| 拉孜县| 营口市| 岢岚县| 莲花县| 罗定市| 政和县| 光泽县| 化德县| 昌宁县| 和林格尔县| 九龙县| 莆田市| 涿州市| 大港区| 青州市| 西城区| 太湖县| 台湾省| 南丰县| 吉木乃县| 色达县| 鲜城| 中西区| 山丹县| 曲水县| 桃园市| 吉隆县| 福贡县| 潼南县| 麻城市| 灌云县| 库伦旗| 花莲市| 铜梁县| 平陆县| 阿克陶县| 秭归县| 新巴尔虎左旗| 鄯善县| 文昌市|