翻譯|使用教程|編輯:李顯亮|2020-03-04 09:59:18.597|閱讀 254 次
概述:在本系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加、獲取、刪除PDF中的附件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加、獲取、刪除PDF中的附件。
>>Aspose.PDF for .NET更新至最新版v20.2,歡迎下載體驗(yàn)。
PDF文件允許將文件作為內(nèi)容附加,并且是PDF文件格式的主要功能之一。可以將不同的文件格式(如Word DOC / DOCX,Excel XLS / XLSX,EML,JPG,PNG,GLB等)嵌入PDF文件中。.NET API的Aspose.PDF可讓您使用.NET應(yīng)用程序處理PDF文件中的附件。
該API可以在PDF文件中添加/嵌入附件,從PDF獲取附件信息,從PDF文件訪問(wèn)單個(gè)附件,以及使用C#或任何其他.NET編程語(yǔ)言從PDF中刪除附件。
附件可以包含多種信息,并且可以具有多種文件類型。步驟如下:
該EmbeddedFiles集合包含PDF文件中的所有附件。以下代碼段顯示了如何在PDF文檔中添加附件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "AddAttachment.pdf"); // Setup new file to be added as attachment FileSpecification fileSpecification = new FileSpecification(dataDir + "test.txt", "Sample text file"); // Add attachment to document's attachment collection pdfDocument.EmbeddedFiles.Add(fileSpecification); dataDir = dataDir + "AddAttachment_out.pdf"; // Save new output pdfDocument.Save(dataDir);
使用Aspose.PDF,可以從PDF文檔中獲取所有附件。當(dāng)您想要將文檔與PDF分開(kāi)保存時(shí),或者需要?jiǎng)冸x附件PDF時(shí),此功能很有用。要從PDF文件獲取所有附件:
以下代碼段顯示了如何從PDF文檔中獲取所有附件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf"); // Get embedded files collection EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; // Get count of the embedded files Console.WriteLine("Total files : {0}", embeddedFiles.Count); int count = 1; // Loop through the collection to get all the attachments foreach (FileSpecification fileSpecification in embeddedFiles) { Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); // Check if parameter object contains the parameters if (fileSpecification.Params != null) { Console.WriteLine("CheckSum: {0}", fileSpecification.Params.CheckSum); Console.WriteLine("Creation Date: {0}", fileSpecification.Params.CreationDate); Console.WriteLine("Modification Date: {0}", fileSpecification.Params.ModDate); Console.WriteLine("Size: {0}", fileSpecification.Params.Size); } // Get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); FileStream fileStream = new FileStream(dataDir + count + "_out" + ".txt", FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close(); count+=1; }
獲取個(gè)人附件
為了獲得單個(gè)附件,我們可以在Document實(shí)例的EmbeddedFiles對(duì)象中指定附件的索引。請(qǐng)嘗試使用以下代碼段。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "GetIndividualAttachment.pdf"); // Get particular embedded file FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1]; // Get the file properties Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); // Check if parameter object contains the parameters if (fileSpecification.Params != null) { Console.WriteLine("CheckSum: {0}", fileSpecification.Params.CheckSum); Console.WriteLine("Creation Date: {0}", fileSpecification.Params.CreationDate); Console.WriteLine("Modification Date: {0}", fileSpecification.Params.ModDate); Console.WriteLine("Size: {0}", fileSpecification.Params.Size); } // Get the attachment and write to file or stream byte[] fileContent = new byte[fileSpecification.Contents.Length]; fileSpecification.Contents.Read(fileContent, 0, fileContent.Length); FileStream fileStream = new FileStream(dataDir + "test_out" + ".txt", FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close();
Aspose.PDF可以從PDF文件中刪除附件。PDF文檔的附件保存在Document對(duì)象的EmbeddedFiles集合中。要?jiǎng)h除與PDF文件關(guān)聯(lián)的所有附件:
以下代碼段顯示了如何從PDF文檔中刪除附件。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf"); // Delete all attachments pdfDocument.EmbeddedFiles.Delete(); dataDir = dataDir + "DeleteAllAttachments_out.pdf"; // Save updated file pdfDocument.Save(dataDir);
附件信息在舉行FileSpecification的對(duì)象,與其他附件聚集Document對(duì)象的EmbeddedFiles集合。該FileSpecification對(duì)象提供獲取有關(guān)附件信息的方法,例如:
要獲取這些參數(shù),請(qǐng)首先確保該P(yáng)arams屬性不為null。可以EmbeddedFiles使用foreach循環(huán)遍歷集合中的所有附件,也可以通過(guò)指定其索引值來(lái)獲取單個(gè)附件。以下代碼段顯示了如何獲取有關(guān)特定附件的信息。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); // Open document Document pdfDocument = new Document(dataDir + "GetAttachmentInfo.pdf"); // Get particular embedded file FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1]; // Get the file properties Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); // Check if parameter object contains the parameters if (fileSpecification.Params != null) { Console.WriteLine("CheckSum: {0}", fileSpecification.Params.CheckSum); Console.WriteLine("Creation Date: {0}", fileSpecification.Params.CreationDate); Console.WriteLine("Modification Date: {0}", fileSpecification.Params.ModDate); Console.WriteLine("Size: {0}", fileSpecification.Params.Size); }還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn