没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2021-08-30 10:59:01.727|阅读 346 次
概述:在某些情况下,可能需要以编程方式向现有 PDF 文件添加文本。据此,本文展示了如何使用 C# 向 PDF 文档添加文本。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
PDF 文档格式广泛,例如生成各种报表、财务报表、履历。在自动化领域,PDF 文档是从 Web 或桌面应用程序中生成和操作的。因此,在这种情况下,可能需要生成各种报表。以编程方式向现有的 PDF 文件添加文本。据此,本文展示了如何使用 C# 向 PDF 文件添加文本。
为了动态地向 PDF 文件添加文本,我们将使用Aspose.PDF for .NET。它是一个功能丰富的 API,可从 .NET 应用程序中创建和 PDF 文件。
下面是使用 C# 向 PDF 文件添加文本的步骤。
下面的示例代码展示了如何向 PDF 文件添加文本。
// 完整的例子和数据文件请到 //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 //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 //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@wqylolg.cn
CADEditorX是一个ActiveX组件,用于在支持ActiveX和COM技术的任何开发环境中,将CAD功能添加到网页或正在开发的应用程序中。同时可以查看、编辑、转换、打印和测量DWG、DXF、SVG、HPGL、PDF、STEP、IGES、STL和其他CAD文件。
DotNetBrowser 是 TeamDev 开发的商业跨平台.NET 库,基于 Chromium,可将浏览器控件嵌入 Windows Forms 等桌面应用,支持多系统运行。能实现网页展示、DOM 操作、JS 运行等功能,还适用于无头服务器任务,是开发中实现浏览器相关功能的有力工具。
本教程将为大家介绍如何使用MyEclipse的可视化JSF编辑器设计JSP,欢迎下载最新版IDE体验!
在处理 Excel 文件时,除了常见的数据读写操作,有时还需要进一步控制 Excel 文件在打开时的显示状态。本教程将介绍如何使用 Java 和 Spire.XLS for Java 库设置 Excel 文件中的活动工作表和活动单元格。
PDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@wqylolg.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢