Aspose.PDF for .NET是一種高PDF處理和解析API,用於在跨平臺應用程序中執行文檔管理和操做任務。API能夠輕鬆用於生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格建立和操做,圖形和圖像功能,普遍的超連接功能,印章和水印任務,擴展的安全控制和自定義字體處理。安全
【下載體驗Aspose.PDF for .NET最新版】字體
在接下來的系列教程中,將爲開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。spa
附件能夠包含各類各樣的信息,而且能夠是各類文件類型。想要向PDF文件添加附件只需兩步:code
該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文檔中獲取全部附件:ip
//文檔目錄的路徑
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對象中指定附件索引。請嘗試使用如下代碼片斷。ci
//文檔目錄的路徑
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);
}
|
-- 未完待續 --
更多教程資源可關注ASPOSE技術交流QQ羣(642018183)哦~歡迎交流討論!