翻譯|使用教程|編輯:李顯亮|2020-12-23 10:36:24.863|閱讀 246 次
概述:PDF附件可以是TXT,DOCX,XLSX或任何其他文檔格式。在本文中,您將學習如何在.NET應用程序中實現一些基本的PDF附件操縱功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
PDF格式支持添加附件,以類似電子郵件附件的PDF文件。PDF附件可以是TXT,DOCX,XLSX或任何其他文檔格式。在本文中,您將學習如何在.NET應用程序中實現一些基本的PDF附件操縱功能,即使用C#以編程方式提取,添加或刪除PDF中的附件。
.NET的Aspose.PDF是著名的PDF操作API,可讓您無縫處理PDF文件。您可以在幾個步驟中閱讀,創建,編輯和轉換PDF文件以及操作PDF附件。
使用C#提取PDF附件
首先,讓我們看看如何從PDF文檔中檢索附件。為此,請按照以下步驟操作:
下面的代碼示例演示如何使用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類將文件添加到Document.EmbeddedFiles集合中。以下是將附件添加到PDF文檔的步驟。
下面的代碼示例演示如何使用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提供了以下方法:
以下是從PDF刪除附件的步驟。
以下代碼示例顯示了如何從C#中的PDF文件中刪除附件。
// Open document Document pdfDocument = new Document("document.pdf"); // Delete all attachments pdfDocument.EmbeddedFiles.Delete(); // Save updated file pdfDocument.Save("output.pdf");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn