Aspose.PDF功能演示:使用C#在PDF中添加或刪除附件
PDF格式支持添加附件,以類(lèi)似電子郵件附件的PDF文件。PDF附件可以是TXT,DOCX,XLSX或任何其他文檔格式。在本文中,您將學(xué)習(xí)如何在.NET應(yīng)用程序中實(shí)現(xiàn)一些基本的PDF附件操縱功能,即使用C#以編程方式提取,添加或刪除PDF中的附件。
- 使用C#提取PDF附件
- 使用C#將附件添加到PDF
- 使用C#從PDF刪除附件
.NET的Aspose.PDF是著名的PDF操作API,可讓您無(wú)縫處理PDF文件。您可以在幾個(gè)步驟中閱讀,創(chuàng)建,編輯和轉(zhuǎn)換PDF文件以及操作PDF附件。
使用C#提取PDF附件
首先,讓我們看看如何從PDF文檔中檢索附件。為此,請(qǐng)按照以下步驟操作:
- 創(chuàng)建Document類(lèi)的實(shí)例。
- 使用Document.EmbeddedFiles屬性將附件獲取到EmbeddedFileCollection對(duì)象中。
- 使用FileSpecification對(duì)象遍歷EmbeddedFileCollection中的附件。
- 使用FileSpecification對(duì)象訪(fǎng)問(wèn)每個(gè)附件的屬性。
- 將附件另存為文件(如果需要)。
下面的代碼示例演示如何使用C#提取PDF附件。
// Open document Document pdfDocument = new Document("document.pdf"); // Get particular embedded file foreach(FileSpecification fileSpecification in pdfDocument.EmbeddedFiles) { // 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(fileSpecification.Name, FileMode.Create); fileStream.Write(fileContent, 0, fileContent.Length); fileStream.Close(); }
使用C#將附件添加到PDF
.NET的Aspose.PDF也允許您將附件添加到PDF文件。為此,您只需要使用FileSpecification類(lèi)將文件添加到Document.EmbeddedFiles集合中。以下是將附件添加到PDF文檔的步驟。
- 使用Document類(lèi)創(chuàng)建一個(gè)新的PDF文檔。
- 創(chuàng)建FileSpecification類(lèi)的實(shí)例以加載附件文件。
- 使用Document.EmbeddedFiles.Add(FileSpecification)方法添加附件。
- 使用Document.Save(String)方法保存文檔。
下面的代碼示例演示如何使用C#將附件添加到PDF文檔。
// Open document Document pdfDocument = new Document("document.pdf"); // Setup new file to be added as attachment FileSpecification fileSpecification = new FileSpecification("test.txt", "Sample text file"); // Add attachment to document's attachment collection pdfDocument.EmbeddedFiles.Add(fileSpecification); // Save new output pdfDocument.Save("output.pdf");
使用C#從PDF刪除附件
可以從PDF文件中刪除全部或特定附件。為此,用于.NET的Aspose.PDF提供了以下方法:
- Delete() –刪除所有附件。
- Delete(String fileName) –按名稱(chēng)刪除附件。
- DeleteByKey(String Key) –按集合中的鍵刪除附件。
以下是從PDF刪除附件的步驟。
- 創(chuàng)建Document類(lèi)的實(shí)例以加載PDF文件。
- 使用Document.EmbeddedFiles.Delete()(或其他刪除方法)刪除附件。
- 使用Document.Save(String)方法保存文件。
以下代碼示例顯示了如何從C#中的PDF文件中刪除附件。
// Open document Document pdfDocument = new Document("document.pdf"); // Delete all attachments pdfDocument.EmbeddedFiles.Delete(); // Save updated file pdfDocument.Save("output.pdf");
還想要更多嗎?您可以點(diǎn)擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(761297826),我們很高興為您提供查詢(xún)和咨詢(xún)。