翻译|使用教程|编辑:李显亮|2020-11-11 09:47:08.543|阅读 237 次
概述:在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何在现有的PDF文件中添加文本。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何管理PDF文件的页眉和页脚。
>>Aspose.PDF for .NET更新至最新版v20.11,欢迎下载体验。
要将文本添加到现有的PDF文件中,需要以下步骤:
以下代码段显示了如何在现有的PDF文件中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Open document Document pdfDocument = new Document(dataDir + "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); dataDir = dataDir + "AddText_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
以下代码片段显示了在将文本添加到PDF文档时如何从Stream对象加载Font。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); string fontFile = ""; // Load input PDF file Document doc = new Document( dataDir + "input.pdf"); // Create text builder object for first page of document TextBuilder textBuilder = new TextBuilder(doc.Pages[1]); // Create text fragment with sample string TextFragment textFragment = new TextFragment("Hello world"); if (fontFile != "") { // Load the TrueType font into stream object using (FileStream fontStream = File.OpenRead(fontFile)) { // Set the font name for text string textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF); // Specify the position for Text Fragment textFragment.Position = new Position(10, 10); // Add the text to TextBuilder so that it can be placed over the PDF file textBuilder.AppendText(textFragment); } dataDir = dataDir + "LoadingFontFromStream_out.pdf"; // Save resulting PDF document. doc.Save(dataDir); }
以下代码段显示了如何使用TextParagraph类在PDF文档中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Open document Document doc = new Document(); // Add page to pages collection of Document object Page page = doc.Pages.Add(); TextBuilder builder = new TextBuilder(page); // Create text paragraph TextParagraph paragraph = new TextParagraph(); // Set subsequent lines indent paragraph.SubsequentLinesIndent = 20; // Specify the location to add TextParagraph paragraph.Rectangle = new Aspose.Pdf.Rectangle(100, 300, 200, 700); // Specify word wraping mode paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords; // Create text fragment TextFragment fragment1 = new TextFragment("the quick brown fox jumps over the lazy dog"); fragment1.TextState.Font = FontRepository.FindFont("Times New Roman"); fragment1.TextState.FontSize = 12; // Add fragment to paragraph paragraph.AppendLine(fragment1); // Add paragraph builder.AppendParagraph(paragraph); dataDir = dataDir + "AddTextUsingTextParagraph_out.pdf"; // Save resulting PDF document. doc.Save(dataDir);
PDF页面可以包含一个或多个TextFragment对象,其中每个TextFragment对象可以具有一个或多个TextSegment实例。为了设置TextSegment的超链接,可以在提供Aspose.Pdf.WebHyperlink实例的对象时使用TextSegment类的Hyperlink属性。请尝试使用以下代码段来满足此要求。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create document instance Document doc = new Document(); // Add page to pages collection of PDF file Page page1 = doc.Pages.Add(); // Create TextFragment instance TextFragment tf = new TextFragment("Sample Text Fragment"); // Set horizontal alignment for TextFragment tf.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; // Create a textsegment with sample text TextSegment segment = new TextSegment(" ... Text Segment 1..."); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Create a new TextSegment segment = new TextSegment("Link to Google"); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Set hyperlink for TextSegment segment.Hyperlink = new Aspose.Pdf.WebHyperlink("www.google.com"); // Set forground color for text segment segment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue; // Set text formatting as italic segment.TextState.FontStyle = FontStyles.Italic; // Create another TextSegment object segment = new TextSegment("TextSegment without hyperlink"); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Add TextFragment to paragraphs collection of page object page1.Paragraphs.Add(tf); dataDir = dataDir + "AddHyperlinkToTextSegment_out.pdf"; // Save resulting PDF document. doc.Save(dataDir);
用于.NET的Aspose.PDF提供了在创建/处理PDF文件内容时使用Custom / TrueType字体的功能,以便使用默认系统字体以外的内容显示文件内容。从.NET 10.3.0的Aspose.PDF版本开始,我们提供了对Open Type Fonts的支持。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document instance Document pdfDocument = new Document(); // Add page to pages collection of PDF file Aspose.Pdf.Page page = pdfDocument.Pages.Add(); // Create TextFragment instnace with sample text TextFragment fragment = new TextFragment("Sample Text in OTF font"); // Find font inside system font directory // Fragment.TextState.Font = FontRepository.FindFont("HelveticaNeueLT Pro 45 Lt"); // Or you can even specify the path of OTF font in system directory fragment.TextState.Font = FontRepository.OpenFont(dataDir + "space age.otf"); // Specify to emend font inside PDF file, so that its displayed properly, // Even if specific font is not installed/present over target machine fragment.TextState.Font.IsEmbedded = true; // Add TextFragment to paragraphs collection of Page instance page.Paragraphs.Add(fragment); dataDir = dataDir + "OTFFont_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn