没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2021-08-30 10:59:01.727|阅读 356 次
概述:在某些情况下,可能需要以编程方式向现有 PDF 文件添加文本。据此,本文展示了如何使用 C# 向 PDF 文档添加文本。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
PDF 文档格式广泛,例如生成各种报表、财务报表、履历。在自动化领域,PDF 文档是从 Web 或桌面应用程序中生成和操作的。因此,在这种情况下,可能需要生成各种报表。以编程方式向现有的 PDF 文件添加文本。据此,本文展示了如何使用 C# 向 PDF 文件添加文本。
为了动态地向 PDF 文件添加文本,我们将使用Aspose.PDF for .NET。它是一个功能丰富的 API,可从 .NET 应用程序中创建和 PDF 文件。
下面是使用 C# 向 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 添加文本。
// 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"); }
Aspose.PDF for .NET 还允许向 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天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
在许多企业应用程序中,从 Visio 文件读取形状数据是一项常见需求,因为这些应用程序中的图表都包含有意义的元数据。本教程将借助Aspose.Diagram,以清晰实用的方式指导您使用 C# 读取形状数据。
PDF 的页面大小决定了其内容的显示、打印或处理方式。在准备打印、转换格式或标准化布局时,我们通常需要更改PDF文档的纸张大小。在本教程中,您将学习如何使用 Java 编辑任何 PDF 文件的页面大小。我们将通过完整的代码示例,逐步指导您完成操作。
本文主要介绍如何使用DevExpress WPF Grid控件中的节点(Nodes)概述,欢迎下载最新版组件体验!
本教程提供DevExpress WinFormsWinExplorer视图的基本信息,欢迎下载最新版组件体验!
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号