翻譯|使用教程|編輯:李顯亮|2019-06-27 10:01:53.637|閱讀 1556 次
概述:在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
附件可以包含各種各樣的信息,并且可以是各種文件類型。想要向PDF文件添加附件只需兩步:
該EmbeddedFiles集合包含PDF文件中的所有附件。以下代碼段顯示了如何在PDF文檔中添加附件:
//文檔目錄的路徑。 string dataDir = RunExamples。GetDataDir_AsposePdf_Attachments(); //打開文檔 Document pdfDocument = new Document(dataDir + "AddAttachment.pdf"); //設置要添加為附件的新文件 FileSpecification fileSpecification = new FileSpecification(dataDir + “ test.txt ”,“ Sample text file ”); //添加附件到文檔的附件集合 pdfDocument.EmbeddedFiles.Add(fileSpecification); dataDir = dataDir + "AddAttachment_out.pdf"; //保存新輸出 pdfDocument.Save(dataDir);
使用Aspose.PDF,可以從PDF文檔中獲取所有附件。當想要將文檔與PDF分開保存,或者需要剝離PDF附件時,這非常有用。
要從PDF文件中獲取所有附件只需兩步:
以下代碼段顯示如何從PDF文檔中獲取所有附件:
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); //打開文檔 Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf"); //獲取嵌入式文件集合 EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles; //獲取嵌入文件的數量 Console.WriteLine("Total files : {0}", embeddedFiles.Count); int count = 1; //遍歷集合以獲取所有附件 foreach (FileSpecification fileSpecification in embeddedFiles) { Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); //檢查參數對象是否包含參數 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); } //獲取附件并寫入文件或流 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;
為了獲得單獨的附件,我們可以在文檔實例的EmbeddedFiles對象中指定附件索引。請嘗試使用以下代碼片段。
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); //打開文檔 Document pdfDocument = new Document(dataDir + "GetIndividualAttachment.pdf"); //獲取特定的嵌入文件 FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1]; //獲取文件屬性 Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); //檢查參數對象是否包含參數 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); } //獲取附件并寫入文件或流 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對象的EmbeddedFiles集合中。
要刪除與PDF文件關聯的所有附件需兩步:
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); //打開文檔 Document pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf"); //刪除所有附件 pdfDocument.EmbeddedFiles.Delete(); dataDir = dataDir + "DeleteAllAttachments_out.pdf"; //保存更新的文件 pdfDocument.Save(dataDir);
附件信息保存在filspeciification對象中,與文檔對象的EmbeddedFiles集合中的其他附件一起收集。文件化對象提供了獲取hteattchment信息的方法,例如:
要獲取這些參數,請首先確保該Params屬性不為null。EmbeddedFiles使用foreach循環遍歷集合中的所有附件,或通過指定其索引值獲取單個附件。以下代碼段顯示了如何獲取有關特定附件的信息:
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); //打開文檔 Document pdfDocument = new Document(dataDir + "GetAttachmentInfo.pdf"); //獲取特定的嵌入文件 FileSpecification fileSpecification = pdfDocument.EmbeddedFiles[1]; //獲取文件屬性 Console.WriteLine("Name: {0}", fileSpecification.Name); Console.WriteLine("Description: {0}", fileSpecification.Description); Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType); //檢查參數對象是否包含參數 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); }
-- 未完待續 --
慧都20萬+用戶答謝惠倒計時,ASPOSE系列產品火熱促銷中,最高直降8萬元!欲購從速!!>>立即進入優惠專場
ASPOSE技術交流QQ群現已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: