這篇文章主要講解了C#如何添加、獲取、刪除PDF附件,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到鉛山網(wǎng)站設(shè)計(jì)與鉛山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鉛山地區(qū)。
概述
附件,指隨同文件發(fā)出的有關(guān)文件或物品。在PDF文檔中,我們可以添加同類型的或其他類型的文檔作為附件內(nèi)容,而PDF中附件也可以分為兩種存在形式,一種是附件以普通文件形式存在,另一種是以注釋的形式存在。在下面的示例中介紹了如何分別添加以上兩種形式的PDF附件。此外,根據(jù)PDF附件的不同添加方式,我們?cè)讷@取PDF附件信息或刪除PDF附件時(shí),也可以分情況來(lái)執(zhí)行操作。
工具使用
pire.PDF for .NET 4.0
代碼示例(供參考)
using Spire.Pdf; using Spire.Pdf.Attachments; namespace AddAttachment_PDF { class Program { static void Main(string[] args) { //創(chuàng)建一個(gè)PdfDocument類對(duì)象,加載測(cè)試文檔 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("sample.pdf"); //初始化PdfAttachment類實(shí)例,加載需要附加的文檔 PdfAttachment attachment = new PdfAttachment("New.pdf"); //將文檔添加到原PDF文檔的附件集合中 pdf.Attachments.Add(attachment); //保存并打開(kāi)文檔 pdf.SaveToFile("Attachment1.pdf"); System.Diagnostics.Process.Start("Attachment1.pdf"); } } }
測(cè)試結(jié)果:
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)建一個(gè)PdfDocument類對(duì)象,加載測(cè)試文檔 PdfDocument doc = new PdfDocument("sample.pdf"); //給添加一個(gè)新頁(yè)面到文檔 PdfPageBase page = doc.Pages.Add(); //添加文本到頁(yè)面,并設(shè)置文本格式(字體、題號(hào)、字體粗細(xì)、顏色、文本位置等) PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, System.Drawing.FontStyle.Bold)); page.Canvas.DrawString("Attachments:", font1, PdfBrushes.CornflowerBlue, new Point(50, 50)); //將文檔作為注釋添加到頁(yè)面 PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, System.Drawing.FontStyle.Bold)); PointF location = new PointF(52, 80); //設(shè)置注釋標(biāo)簽,標(biāo)簽內(nèi)容為作為附件的文檔 String label = "sample.docx"; byte[] data = File.ReadAllBytes("sample.docx"); SizeF size = font2.MeasureString(label); //設(shè)置注釋位置、大小、顏色、標(biāo)簽類型以及顯示文本等 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); //保存并打開(kāi)文檔 doc.SaveToFile("Attachment2.pdf"); System.Diagnostics.Process.Start("Attachment2.pdf"); } } }
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文檔,加載測(cè)試文件 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Attachment1.pdf"); //獲取文檔中的第一個(gè)文件附件 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(); } } }
測(cè)試結(jié)果:
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"); //實(shí)例化一個(gè)list并將文檔內(nèi)所有頁(yè)面的Attachment annotations添加到該list Listattaches = new List (); 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é)果:
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); } //保存并打開(kāi)文檔 pdf.SaveToFile("Remove.pdf"); System.Diagnostics.Process.Start("Remove.pdf"); } } }
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); } //保存并打開(kā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); } } //保存并打開(kāi)文檔 pdf.SaveToFile("Result.pdf"); System.Diagnostics.Process.Start("Result.pdf"); } } }
調(diào)試程序后,生成的文檔就沒(méi)有附件了。
看完上述內(nèi)容,是不是對(duì)C#如何添加、獲取、刪除PDF附件有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。