本次的教程将会为广大的用户提供一系列PDF文档处理控件Aspose.Pdf的使用方法与技巧的总结,帮助大家更好地使用Aspose.Pdf。
》》》下载Aspose.Pdf试用版
线程访问
Aspose.Pdf不支持多线程访问一个单独的文件,一个文件只能有一个线程进行访问。
页面高度和点
页面的高度和宽度属性都使用点作为基本单位,比如1 inch = 72 points ,1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points。
文件转换
下面的代码允许使用者将 PdfXML, Xsl-FO, HTML 和 SVG 格式文件快速的转换为PDF格式。
这个方法将自动发现输入文件的格式,并自动渲染导出为PDF格式。
C#
1
2
3
4
5
6
|
Pdf pdf = new Pdf();
pdf.ParseToPdf( "c:/pdftest/sample.html" );
pdf.Save( "c:/pdftest/HTMLoutput.pdf" );
|
PDF打印
如果你想打印包含文本内容的PDF文件,并且你行要内容显示为文本而不是向量图形,请使用以下代码,如果文档不包含嵌入字体,可以将系统字体嵌入到被打印的文档中。
C#
1
2
3
4
5
6
|
PdfViewer pdfViewer = new PdfViewer();
pdfViewer.RenderingOptions.SystemFontsNativeRendering = true ;
...
pdfViewer.PrintDocumentWithSettings(...);
|
对于那些已经有嵌入字体的文档,质量也可以得到提高,并且字体也可以嵌入到文档中。Aspose.Pdf已经推出了一个新功能,可以使用系统字体替代嵌入字体,代替相同的命名的字体请用下面的代码片段:
C#
1
2
3
4
5
6
7
8
|
FontRepository.Substitutions.Add( new SystemFontsSubstitution(SubstitutionFontCategories.TheSameNamedEmbeddedFonts));
PdfViewer pdfViewer = new PdfViewer();
pdfViewer.RenderingOptions.SystemFontsNativeRendering = true ;
...
pdfViewer.PrintDocumentWithSettings(...);
|
阅读XFA字段
如果源PDF文件包含XFA表单字段,下面的代码片段可以用来阅读他们,因为我们不能使用Aspose.Pdf.InteractiveFeatures.Forms.Field进行阅读。
C#
1
2
3
4
5
|
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#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
string strDocDir = @"C:\pdftest\" ;
string strDocName = "Input_Bookmarked.pdf" ;
string strFilePath = strDocDir + strDocName;
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();
bookmarkEditor.BindPdf(strFilePath);
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文件中使用适当的方法添加书签的代码片段。
C#
1
2
3
4
5
6
7
|
Document doc = new Document( "input.pdf" );
OutlineItemCollection bookmark = new OutlineItemCollection(doc.Outlines);
bookmark.Title = "bookmark1" ;
bookmark.Action = new GoToAction(doc.Pages[1]);
doc.Outlines.Add(bookmark);
doc.Save( "Output_Bookmarked.pdf" );
|
在HTML转换为PDF时使用TrueType字体
如果源HTML文件包含TrueType字体,而你又想在导出PDF文件时显示该字体,那么就可以在保存PDF文件前使用下面的代码。
C#
1
2
|
text1.UseTextInfoStyle = true ;
|