没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2021-08-30 10:59:01.727|阅读 361 次
概述:在某些情况下,可能需要以编程方式向现有 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@obta.cn
创建报表系统时,确保报表工具与各种数据库兼容非常重要。FastReport .NET是 .NET 应用程序中广受欢迎的报表生成库之一。在本文中,我们将介绍如何在 FastReport .NET 中通过报表设计器和代码构建和连接 Firebird 插件。
如果您是开发人员、印刷专业人士或平面设计师,将PPTX/PPT文件转换为EMF 格式将为您带来诸多优势,例如平台独立性、布局处理能力等等。本文将特别介绍如何借助Aspose.Slides for .NET在 C# 中将 PPTX 转换为 EMF,并重点介绍具体步骤和代码示例。
本文将为大家介绍如何用图表控件LightningChart JS实现实时并行坐标图开发,欢迎下载新版组件体验!
本文主要介绍如何使用DevExpress WPF Grid控件检查节点,欢迎下载最新版组件体验!
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@obta.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢