Aspose.PDF For .NET是一款功能强大的PDF文档创建和处理控件,使您的.NET程序可以在不安装Adobe Acrobat的情况下,读取和操作存在的PDF文档,同样支持创建表单和管理PDF嵌入的表单字段,支持PDF压缩,表格创建和处理,支持图形对象和超链接,安全控制,添加和删除书签,添加更新和删除附件和注释,支持PDF转换为多种文件格式和多种文件格式转换为PDF。
下面我们简单介绍,Aspose.PDF为存在的PDF文件添加文本:
C#
//Open document
Document pdfDocument = new Document("input.pdf");
//Get particular page
Page pdfPage = (Page)pdfDocument.Pages[1];
//Create text fragment
TextFragment textFragment = new TextFragment("main text");
textFragment.Position = new Position(100, 600);
//Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
//Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
//Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
//Save document
pdfDocument.Save("output.pdf");
VB.NET
'Open document
Dim pdfDocument As New Document("input.pdf")
'Get particular page
Dim pdfPage As Page = CType(pdfDocument.Pages(1), Page)
'Create text fragment
Dim textFragment As New TextFragment("main text")
textFragment.Position = New Position(100, 600)
'Set text properties
textFragment.TextState.FontSize = 12
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman")
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red)
'Create TextBuilder object
Dim textBuilder As New TextBuilder(pdfPage)
'Append the text fragment to the PDF page
textBuilder.AppendText(textFragment)
'Save document
pdfDocument.Save("output.pdf")
添加带下划线的文本:
C#
// create documentation object
Document pdfDocument = new Document();
// add age page to PDF document
pdfDocument.Pages.Add();
// create TextBuilder for first page
TextBuilder tb = new TextBuilder(pdfDocument.Pages[1]);
// TextFragment with sample text
TextFragment fragment = new TextFragment("Test message");
// set the font for TextFragment
fragment.TextState.Font = FontRepository.FindFont("Arial");
fragment.TextState.FontSize = 10;
// set the formatting of text as Underline
fragment.TextState.Underline = true;
// specify the position where TextFragment needs to be placed
fragment.Position = new Position(10, 800);
// append TextFragment to PDF file
tb.AppendText(fragment);
// save the PDF file
pdfDocument.Save("c:/pdftest/UnderLine.pdf");
VB.NET
' create documentation object
Dim pdfDocument As Document = New Document()
' add age page to PDF document
pdfDocument.Pages.Add()
' create TextBuilder for first page
Dim tb As Aspose.Pdf.Text.TextBuilder = New Aspose.Pdf.Text.TextBuilder(pdfDocument.Pages(1))
' TextFragment with sample text
Dim fragment As TextFragment = New TextFragment("Test message")
' set the font for TextFragment
fragment.TextState.Font = FontRepository.FindFont("Arial")
fragment.TextState.FontSize = 10
' set the formatting of text as Underline
fragment.TextState.Underline = True
' specify the position where TextFragment needs to be placed
fragment.Position = New Position(10, 800)
' append TextFragment to PDF file
tb.AppendText(fragment)
' save the PDF file
pdfDocument.Save("c:/pdftest/UnderLine.pdf")