翻譯|使用教程|編輯:李顯亮|2020-03-04 09:59:18.597|閱讀 262 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加、獲取、刪除PDF中的附件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加、獲取、刪除PDF中的附件。
>>Aspose.PDF for .NET更新至最新版v20.2,歡迎下載體驗。
PDF文件允許將文件作為內容附加,并且是PDF文件格式的主要功能之一。可以將不同的文件格式(如Word DOC / DOCX,Excel XLS / XLSX,EML,JPG,PNG,GLB等)嵌入PDF文件中。.NET API的Aspose.PDF可讓您使用.NET應用程序處理PDF文件中的附件。
該API可以在PDF文件中添加/嵌入附件,從PDF獲取附件信息,從PDF文件訪問單個附件,以及使用C#或任何其他.NET編程語言從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文檔中獲取所有附件。當您想要將文檔與PDF分開保存時,或者需要剝離附件PDF時,此功能很有用。要從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;
}
獲取個人附件
為了獲得單個附件,我們可以在Document實例的EmbeddedFiles對象中指定附件的索引。請嘗試使用以下代碼段。
// 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對象的EmbeddedFiles集合中。要刪除與PDF文件關聯的所有附件:
以下代碼段顯示了如何從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的對象,與其他附件聚集Document對象的EmbeddedFiles集合。該FileSpecification對象提供獲取有關附件信息的方法,例如:
要獲取這些參數,請首先確保該Params屬性不為null。可以EmbeddedFiles使用foreach循環遍歷集合中的所有附件,也可以通過指定其索引值來獲取單個附件。以下代碼段顯示了如何獲取有關特定附件的信息。
// 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);
}
還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。 本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn