翻譯|使用教程|編輯:李顯亮|2019-06-27 10:01:53.637|閱讀 1571 次
概述:在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(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ò)展的安全控制和自定義字體處理。
【下載體驗(yàn)Aspose.PDF for .NET最新版】
在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。
附件可以包含各種各樣的信息,并且可以是各種文件類型。想要向PDF文件添加附件只需兩步:
該EmbeddedFiles集合包含PDF文件中的所有附件。以下代碼段顯示了如何在PDF文檔中添加附件:
//文檔目錄的路徑。 string dataDir = RunExamples。GetDataDir_AsposePdf_Attachments(); //打開(kāi)文檔 Document pdfDocument = new Document(dataDir + "AddAttachment.pdf"); //設(shè)置要添加為附件的新文件 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文檔中獲取所有附件。當(dāng)想要將文檔與PDF分開(kāi)保存,或者需要?jiǎng)冸xPDF附件時(shí),這非常有用。
要從PDF文件中獲取所有附件只需兩步:
以下代碼段顯示如何從PDF文檔中獲取所有附件:
//文檔目錄的路徑
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
//打開(kāi)文檔
Document pdfDocument = new Document(dataDir + "GetAlltheAttachments.pdf");
            
//獲取嵌入式文件集合
EmbeddedFileCollection embeddedFiles = pdfDocument.EmbeddedFiles;
            
//獲取嵌入文件的數(shù)量
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);
    
    
     //檢查參數(shù)對(duì)象是否包含參數(shù)
    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);
    }
    
    //獲取附件并寫(xiě)入文件或流
    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;為了獲得單獨(dú)的附件,我們可以在文檔實(shí)例的EmbeddedFiles對(duì)象中指定附件索引。請(qǐng)嘗試使用以下代碼片段。
//文檔目錄的路徑
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
//打開(kāi)文檔
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);
            
//檢查參數(shù)對(duì)象是否包含參數(shù)
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);
}
            
//獲取附件并寫(xiě)入文件或流
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)的所有附件需兩步:
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments(); //打開(kāi)文檔 Document pdfDocument = new Document(dataDir + "DeleteAllAttachments.pdf"); //刪除所有附件 pdfDocument.EmbeddedFiles.Delete(); dataDir = dataDir + "DeleteAllAttachments_out.pdf"; //保存更新的文件 pdfDocument.Save(dataDir);
附件信息保存在filspeciification對(duì)象中,與文檔對(duì)象的EmbeddedFiles集合中的其他附件一起收集。文件化對(duì)象提供了獲取hteattchment信息的方法,例如:
要獲取這些參數(shù),請(qǐng)首先確保該P(yáng)arams屬性不為null。EmbeddedFiles使用foreach循環(huán)遍歷集合中的所有附件,或通過(guò)指定其索引值獲取單個(gè)附件。以下代碼段顯示了如何獲取有關(guān)特定附件的信息:
//文檔目錄的路徑
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
//打開(kāi)文檔
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);
            
//檢查參數(shù)對(duì)象是否包含參數(shù)
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);
}-- 未完待續(xù) --
慧都20萬(wàn)+用戶答謝惠倒計(jì)時(shí),ASPOSE系列產(chǎn)品火熱促銷中,最高直降8萬(wàn)元!欲購(gòu)從速!!>>立即進(jìn)入優(yōu)惠專場(chǎng)
ASPOSE技術(shù)交流QQ群現(xiàn)已開(kāi)通,各類資源及時(shí)分享,歡迎交流討論!(掃描下方二維碼加入群聊)

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: