原創(chuàng)|使用教程|編輯:郝浩|2013-03-15 13:49:38.000|閱讀 3838 次
概述:本次的教程將會為廣大的用戶提供一系列PDF編輯與轉(zhuǎn)換控件Aspose.Pdf的使用方法與技巧的總結(jié),幫助大家更好地使用Aspose.Pdf。其中分別對多個使用技巧進(jìn)行了詳細(xì)的描述,并附上使用代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
本次的教程將會為廣大的用戶提供一系列PDF文檔處理控件Aspose.Pdf的使用方法與技巧的總結(jié),幫助大家更好地使用Aspose.Pdf。
Aspose.Pdf不支持多線程訪問一個單獨(dú)的文件,一個文件只能有一個線程進(jìn)行訪問。
頁面的高度和寬度屬性都使用點(diǎn)作為基本單位,比如1 inch = 72 points ,1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points。
下面的代碼允許使用者將 PdfXML, Xsl-FO, HTML 和 SVG 格式文件快速的轉(zhuǎn)換為PDF格式。
這個方法將自動發(fā)現(xiàn)輸入文件的格式,并自動渲染導(dǎo)出為PDF格式。
C#
// instantiate PDF object Pdf pdf = new Pdf(); // bind the source HTML file for conversion pdf.ParseToPdf("c:/pdftest/sample.html"); // save the output PDF document pdf.Save("c:/pdftest/HTMLoutput.pdf");
如果你想打印包含文本內(nèi)容的PDF文件,并且你行要內(nèi)容顯示為文本而不是向量圖形,請使用以下代碼,如果文檔不包含嵌入字體,可以將系統(tǒng)字體嵌入到被打印的文檔中。
C#
PdfViewer pdfViewer = new PdfViewer(); // Render all system fonts with native system approach (embed the fonts to output documents) pdfViewer.RenderingOptions.SystemFontsNativeRendering = true; ... pdfViewer.PrintDocumentWithSettings(...);
對于那些已經(jīng)有嵌入字體的文檔,質(zhì)量也可以得到提高,并且字體也可以嵌入到文檔中。Aspose.Pdf已經(jīng)推出了一個新功能,可以使用系統(tǒng)字體替代嵌入字體,代替相同的命名的字體請用下面的代碼片段:
C#
// Substitute all embedded fonts with system fonts that names are equal FontRepository.Substitutions.Add(new SystemFontsSubstitution(SubstitutionFontCategories.TheSameNamedEmbeddedFonts)); PdfViewer pdfViewer = new PdfViewer(); // Render all system fonts with native system approach (embed the fonts to output documents) pdfViewer.RenderingOptions.SystemFontsNativeRendering = true; ... pdfViewer.PrintDocumentWithSettings(...);
如果源PDF文件包含XFA表單字段,下面的代碼片段可以用來閱讀他們,因?yàn)槲覀儾荒苁褂肁spose.Pdf.InteractiveFeatures.Forms.Field進(jìn)行閱讀。
C#
Aspose.Pdf.Document d = new Aspose.Pdf.Document("c:/pdftest/input.pdf"); { foreach (string field in d.Form.XFA.FieldNames) { Console.WriteLine(field); }
如果一個源PDF文件包含書簽,你需要得到頁面號碼信息,嘗試使用Aspose.Pdf.Facades.Bookmark class類的PageNumber屬性。
C#
string strDocDir = @"C:\pdftest\"; string strDocName = "Input_Bookmarked.pdf"; string strFilePath = strDocDir + strDocName; //Create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //Ppen PDF file bookmarkEditor.BindPdf(strFilePath); //Extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks) { string strLevelSeprator = string.Empty; for (int i = 1; i < bookmark.Level; i++) { strLevelSeprator += "----"; } Console.WriteLine("{0}Title: {1}", strLevelSeprator, bookmark.Title); Console.WriteLine("{0}Page Number: {1}", strLevelSeprator, bookmark.PageNumber); Console.WriteLine("{0}Page Action: {1}", strLevelSeprator, bookmark.Action); }
下面是在PDF文件中使用適當(dāng)?shù)姆椒ㄌ砑訒灥拇a片段。
C#
Document doc = new Document("input.pdf"); OutlineItemCollection bookmark = new OutlineItemCollection(doc.Outlines); bookmark.Title = "bookmark1"; // bookmark.Action = new GoToAction(1); <-- use next line instead of this one. bookmark.Action = new GoToAction(doc.Pages[1]); doc.Outlines.Add(bookmark); doc.Save("Output_Bookmarked.pdf");
如果源HTML文件包含TrueType字體,而你又想在導(dǎo)出PDF文件時顯示該字體,那么就可以在保存PDF文件前使用下面的代碼。
C#
// Use the style from TextInfo text1.UseTextInfoStyle = true;
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)