欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#添加、獲取、刪除PDF附件實例代碼

 更新時間:2020年05月29日 10:56:35   作者:EiceblueSpire  
這篇文章主要介紹了如何在C#添加、獲取、刪除PDF附件,文中代碼非常詳細,快來和小編一起學(xué)習(xí)吧

概述

附件,指隨同文件發(fā)出的有關(guān)文件或物品。在PDF文檔中,我們可以添加同類型的或其他類型的文檔作為附件內(nèi)容,而PDF中附件也可以分為兩種存在形式,一種是附件以普通文件形式存在,另一種是以注釋的形式存在。在下面的示例中介紹了如何分別添加以上兩種形式的PDF附件。此外,根據(jù)PDF附件的不同添加方式,我們在獲取PDF附件信息或刪除PDF附件時,也可以分情況來執(zhí)行操作。

工具使用

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) 
 { 
 //創(chuàng)建一個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"); 
 } 
 }
}

測試結(jié)果:

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) 
 { 
 //創(chuàng)建一個PdfDocument類對象,加載測試文檔 
 PdfDocument doc = new PdfDocument("sample.pdf"); 
 
 //給添加一個新頁面到文檔 
 PdfPageBase page = doc.Pages.Add(); 
 
 //添加文本到頁面,并設(shè)置文本格式(字體、題號、字體粗細、顏色、文本位置等) 
 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); 
 
 //設(shè)置注釋標簽,標簽內(nèi)容為作為附件的文檔 
 String label = "sample.docx"; 
 byte[] data = File.ReadAllBytes("sample.docx"); 
 SizeF size = font2.MeasureString(label); 
 
 //設(shè)置注釋位置、大小、顏色、標簽類型以及顯示文本等 
 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) 
 { 
 //創(chuàng)建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); 
 
 //將附件的數(shù)據(jù)寫入到新文檔 
 File.WriteAllBytes(attachment.FileName, attachment.Data); 
 Console.ReadKey(); 
 } 
 }
 }

測試結(jié)果:

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并將文檔內(nèi)所有頁面的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,將附件數(shù)據(jù)寫入到新文檔 
 for (int i = 0; i < attaches.Count; i++) 
 { 
 File.WriteAllBytes(attaches[i].FileName, attaches[i].Data); 
 } 
 } 
 }
 }

注釋附件讀取結(jié)果:

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

調(diào)試程序后,生成的文檔就沒有附件了。

以上就是C#添加、獲取、刪除PDF附件實例代碼的詳細內(nèi)容,更多關(guān)于C#添加、獲取、刪除PDF附件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法

    C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法

    這篇文章主要介紹了C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法,涉及C#雙向鏈表的定義與排序技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 淺析C#更改令牌ChangeToken

    淺析C#更改令牌ChangeToken

    這篇文章主要介紹了C#更改令牌ChangeToken,文中運用大量代碼講解的非常詳細,感興趣的小伙伴一起來看看這篇文章吧
    2021-09-09
  • C#實現(xiàn)簡單屏幕監(jiān)控的方法

    C#實現(xiàn)簡單屏幕監(jiān)控的方法

    這篇文章主要介紹了C#實現(xiàn)簡單屏幕監(jiān)控的方法,涉及C#的圖標隱藏及屏幕截圖等技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#中把Datatable轉(zhuǎn)換為Json的5個代碼實例

    C#中把Datatable轉(zhuǎn)換為Json的5個代碼實例

    這篇文章主要介紹了C#中把Datatable轉(zhuǎn)換為Json的5個代碼實例,需要的朋友可以參考下
    2014-04-04
  • 基于C#實現(xiàn)一個簡單的FTP操作工具

    基于C#實現(xiàn)一個簡單的FTP操作工具

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)一個簡單的FTP操作工具,可以實現(xiàn)FTP上傳、下載、重命名、刷新、刪除功能,感興趣的可以了解一下
    2022-08-08
  • C#基于Modbus三種CRC16校驗方法的性能對比

    C#基于Modbus三種CRC16校驗方法的性能對比

    這篇文章主要介紹了C#基于Modbus三種CRC16校驗方法的性能對比,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • c# 閉包的相關(guān)知識以及需要注意的地方

    c# 閉包的相關(guān)知識以及需要注意的地方

    這篇文章主要介紹了c# 閉包的相關(guān)知識以及需要注意的地方,文中講解非常細致,代碼幫助大家理解和學(xué)習(xí),感興趣的朋友可以參考下
    2020-06-06
  • C#實現(xiàn)ComboBox自動匹配字符

    C#實現(xiàn)ComboBox自動匹配字符

    本文介紹C#如何實現(xiàn)ComboBox自動匹配字符1.采用CustomSource當(dāng)做提示集合2. 直接使用下拉列表中的項作為匹配的集合,需要了解的朋友可以參考下
    2012-12-12
  • C#實現(xiàn)的海盜分金算法實例

    C#實現(xiàn)的海盜分金算法實例

    這篇文章主要介紹了C#實現(xiàn)的海盜分金算法,結(jié)合具體實例形式分析了海盜分金算法的原理與C#相應(yīng)實現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • C#驗證控件validator的簡單使用

    C#驗證控件validator的簡單使用

    這篇文章主要介紹了C#驗證控件validator的簡單使用方法和示例,十分的簡單實用,有需要的小伙伴可以參考下。
    2015-06-06

最新評論