翻译|使用教程|编辑:李显亮|2020-11-20 09:46:08.257|阅读 597 次
概述:在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何在设置文本格式,如行缩进、下划线、换行等等。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关链接:
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何在设置文本格式,如行缩进、下划线、换行等等。
>>Aspose.PDF for .NET更新至最新版v20.11,欢迎下载体验。
TextState类提供了设置放置在PDF文档中的TextFragments格式的功能。您可以使用此类将文本格式设置为粗体,斜体,下划线,并在此版本开始时,API提供了将文本格式标记为Strikeout的功能。请尝试使用以下代码片段添加具有Strikeout格式的TextFragment。请使用完整的代码段:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Open document Document pdfDocument = new Document(); // Get particular page Page pdfPage = (Page)pdfDocument.Pages.Add(); // 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.LightGray; textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; // Set StrikeOut property textFragment.TextState.StrikeOut = true; // Mark text as Bold textFragment.TextState.FontStyle = FontStyles.Bold; // Create TextBuilder object TextBuilder textBuilder = new TextBuilder(pdfPage); // Append the text fragment to the PDF page textBuilder.AppendText(textFragment); dataDir = dataDir + "AddStrikeOutText_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
在用于文本编辑方案的API中,文本格式得到了进一步增强,现在您可以在PDF文档中添加带有图案色彩空间的文本。通过引入PatternColorSpace的新属性进一步增强了Aspose.Pdf.Color类,该属性可用于指定文本的底纹颜色。此新属性在文本中添加了不同的渐变底纹,例如“轴向底纹”,“径向(类型3)”底纹,如以下代码片段所示:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); using (Document pdfDocument = new Document(dataDir + "text_sample4.pdf")) { TextFragmentAbsorber absorber = new TextFragmentAbsorber("Lorem ipsum"); pdfDocument.Pages.Accept(absorber); TextFragment textFragment = absorber.TextFragments[1]; // Create new color with pattern colorspace textFragment.TextState.ForegroundColor = new Aspose.Pdf.Color() { PatternColorSpace = new Aspose.Pdf.Drawing.GradientAxialShading(Color.Red, Color.Blue) }; textFragment.TextState.Underline = true; pdfDocument.Save(dataDir + "text_out.pdf"); }
用于.NET的Aspose.PDF将SubsequentLinesIndent属性提供给TextFormattingOptions类。在具有TextFragment和Paragraphs集合的PDF生成方案中,可用于指定行缩进。请使用以下代码段使用该属性:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document object Aspose.Pdf.Document document = new Aspose.Pdf.Document(); Aspose.Pdf.Page page = document.Pages.Add(); Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog. A quick brown fox jumped over the lazy dog."); // Initilize TextFormattingOptions for the text fragment and specify SubsequentLinesIndent value text.TextState.FormattingOptions = new Aspose.Pdf.Text.TextFormattingOptions() { SubsequentLinesIndent = 20 }; page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line2"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line3"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line4"); page.Paragraphs.Add(text); text = new Aspose.Pdf.Text.TextFragment("Line5"); page.Paragraphs.Add(text); document.Save(dataDir + "SubsequentIndent_out.pdf", Aspose.Pdf.SaveFormat.Pdf);
以下代码段显示了如何使用TextBuilder和设置TextState的DrawTextRectangleBorder属性为文本添加边框:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document object Document pdfDocument = new Document(); // Get particular page Page pdfPage = (Page)pdfDocument.Pages.Add(); // 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.LightGray; textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; // Set StrokingColor property for drawing border (stroking) around text rectangle textFragment.TextState.StrokingColor = Aspose.Pdf.Color.DarkRed; // Set DrawTextRectangleBorder property value to true textFragment.TextState.DrawTextRectangleBorder = true; TextBuilder tb = new TextBuilder(pdfPage); tb.AppendText(textFragment); // Save the document pdfDocument.Save(dataDir + "PDFWithTextBorder_out.pdf");
以下代码段显示了如何在创建新的PDF文件时添加下划线文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // 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); dataDir = dataDir + "AddUnderlineText_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
可以控制所添加文本的外观。下例显示了如何在您添加的文本周围绘制边框,方法是在该文本周围绘制一个矩形。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); PdfContentEditor editor = new PdfContentEditor(); editor.BindPdf(dataDir + "input.pdf"); LineInfo lineInfo = new LineInfo(); lineInfo.LineWidth = 2; lineInfo.VerticeCoordinate = new float[] { 0, 0, 100, 100, 50, 100 }; lineInfo.Visibility = true; editor.CreatePolygon(lineInfo, 1, new System.Drawing.Rectangle(0, 0, 0, 0), ""); dataDir = dataDir + "AddingBorderAroundAddedText_out.pdf"; // Save resulting PDF document. editor.Save(dataDir);
TextFragment不支持文本内的换行。但是,为了通过换行符添加文本,请使用TextFragment和TextParagraph:
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document(); Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add(); // Initialize new TextFragment with text containing required newline markers Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment("Applicant Name: " + Environment.NewLine + " Joe Smoe"); // Set text fragment properties if necessary textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"); textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray; textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red; // Create TextParagraph object TextParagraph par = new TextParagraph(); // Add new TextFragment to paragraph par.AppendLine(textFragment); // Set paragraph position par.Position = new Aspose.Pdf.Text.Position(100, 600); // Create TextBuilder object TextBuilder textBuilder = new TextBuilder(applicationFirstPage); // Add the TextParagraph using TextBuilder textBuilder.AppendParagraph(par); dataDir = dataDir + "AddNewLineFeed_out.pdf"; // Save resulting PDF document. pdfApplicationDoc.Save(dataDir);
Aspose.PDF支持为Floating Box元素内的内容设置文本对齐方式。如下面的代码示例所示,可以使用Aspose.Pdf.FloatingBox实例的对齐属性来实现此目的。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); Aspose.Pdf.Document doc = new Document(); doc.Pages.Add(); Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox(100, 100); floatBox.VerticalAlignment = VerticalAlignment.Bottom; floatBox.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; floatBox.Paragraphs.Add(new TextFragment("FloatingBox_bottom")); floatBox.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Blue); doc.Pages[1].Paragraphs.Add(floatBox); Aspose.Pdf.FloatingBox floatBox1 = new Aspose.Pdf.FloatingBox(100, 100); floatBox1.VerticalAlignment = VerticalAlignment.Center; floatBox1.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; floatBox1.Paragraphs.Add(new TextFragment("FloatingBox_center")); floatBox1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Blue); doc.Pages[1].Paragraphs.Add(floatBox1); Aspose.Pdf.FloatingBox floatBox2 = new Aspose.Pdf.FloatingBox(100, 100); floatBox2.VerticalAlignment = VerticalAlignment.Top; floatBox2.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; floatBox2.Paragraphs.Add(new TextFragment("FloatingBox_top")); floatBox2.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Blue); doc.Pages[1].Paragraphs.Add(floatBox2); doc.Save(dataDir + "FloatingBox_alignment_review_out.pdf");
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn