PDF处理控件Aspose.PDF功能演示:使用 C# 将文本添加到 PDF 文档

  • 使用 C# 将文本添加到 PDF
  • 使用 TextParagraph 将文本添加到 PDF
  • 在 PDF 中插入透明文本

为了动态地向 PDF 文件添加文本,我们将使用

使用 C# 将文本添加到 PDF

下面是使用 C# 向 PDF 文件添加文本的步骤。

  • 使用文档类加载 PDF 文件。
  • 将需要的 PDF 访问页面中。
  • 创建一个TextFragment对象并设置它的文本、位置、字体、背景颜色等。
  • 创建一个TextBuilder对象并用Page对象初始化它。
  • 使用TextBuilder.AppendText(TextFragment)方法向PDF添加页面文本。
  • 使用Document.Save(String)方法保存更新的PDF文件。

下面的示例代码展示了如何向 PDF 文件添加文本。

// 完整的例子和数据文件请到 https://github.com/aspose-pdf/Aspose.PDF-for-.NET // 打开文档 文档 pdfDocument = new Document("input.pdf");  // 获取特定页面 Page pdfPage = (Page)pdfDocument.Pages[1];  // 创建文本片段 TextFragment textFragment = new TextFragment("正文"); textFragment.Position = new Position(100, 600);  // 设置文本属性 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);  // 创建 TextBuilder 对象 TextBuilder textBuilder = new TextBuilder(pdfPage);  // 将文本片段附加到 PDF 页面 textBuilder.AppendText(textFragment);  // 保存生成的 PDF 文档。 pdfDocument.Save("output.pdf");

使用 TextParagraph 将文本添加到 PDF

下面是使用TextParagraph将文本添加到PDF文件的步骤。

  • 使用文档类加载 PDF 文件。
  • 将需要的 PDF 展示页面对象或添加一个新页面。
  • 创建一个TextBuilder对象并用Page对象初始化它。
  • 创建一个TextParagraph类的对象。
  • 使用TextParagraph.Rectangle属性指定段落的位置。
  • 设置TextParagraph 的格式选项。
  • 创建一个TextFragment对象并设置它的文本、位置、字体、背景颜色等。
  • 使用TextParagraph.AppendLine(TextFragment)方法将TextFragment添加到段落。
  • 使用TextBuilder.AppendParagraph(TextParagraph)方法向添加段落段落。
  • 使用Document.Save(String)方法保存更新的PDF文件。

下面示例代码展示了如何使用 TextParagraph 类向 PDF 添加文本。

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET string fontFile = "font.ttf";  // Load input PDF file Document doc = new Document("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); }  // Save resulting PDF document doc.Save("output.pdf"); }

在PDF中添加透明文本

Aspose.PDF for .NET 还允许向 PDF 文档添加透明文本,如以下步骤所示。

  • 使用Document类加载 PDF 文件。
  • 将所需的 PDF 页面放入一个Page对象或添加一个新页面。
  • 创建并初始化一个Graph对象。
  • 创建一个Rectangle对象并初始化它。
  • 设置Rectangle.GraphInfo.FillColor属性。
  • 使用Graph.Shapes.Add(Rectangle)方法向Graph添加矩形。
  • 使用Page.Paragraphs.Add(Graph)方法将Graph添加到页面的段落集合中。
  • 创建一个TextFragment对象并设置其TextState.ForegroundColor属性。
  • 使用Page.Paragraphs.Add(TextFragment)方法将TextFragment添加到页面。
  • 使用Document.Save(String)方法保存更新的 PDF 文件。

以下代码示例展示了如何向 PDF 文件添加透明文本。

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET // Create Document instance Document doc = new Document("input.pdf");  // Create page to pages collection of PDF file Aspose.Pdf.Page page = doc.Pages.Add();  // Create Graph object Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);  // Create rectangle instance with certain dimensions Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400);  // Create color object from Alpha color channel rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));  // Add rectanlge to shapes collection of Graph object canvas.Shapes.Add(rect);  // Add graph object to paragraphs collection of page object page.Paragraphs.Add(canvas);  // Set value to not change position for graph object canvas.IsChangePosition = false;  // Create TextFragment instance with sample value TextFragment text = new TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text ");  // Create color object from Alpha channel Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0);  // Set color information for text instance text.TextState.ForegroundColor = color;  // Add text to paragraphs collection of page instance page.Paragraphs.Add(text);  // Save the updated PDF file doc.Save("output.pdf");

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要加入的教程资源。如果您有任何疑问或需求,请随时Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年7月27日
下一篇 2021年7月27日

相关推荐

发表回复

登录后才能评论
iruanshi.com/software/author/jinwe2022"} }