翻譯|使用教程|編輯:李顯亮|2021-08-30 10:59:01.727|閱讀 407 次
概述:在某些情況下,可能需要以編程方式向現(xiàn)有 PDF 文件添加文本。據(jù)此,本文展示了如何使用 C# 向 PDF 文檔添加文本。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PDF 文檔格式廣泛,例如生成各種報(bào)表、財(cái)務(wù)報(bào)表、履歷。在自動(dòng)化領(lǐng)域,PDF 文檔是從 Web 或桌面應(yīng)用程序中生成和操作的。因此,在這種情況下,可能需要生成各種報(bào)表。以編程方式向現(xiàn)有的 PDF 文件添加文本。據(jù)此,本文展示了如何使用 C# 向 PDF 文件添加文本。
為了動(dòng)態(tài)地向 PDF 文件添加文本,我們將使用Aspose.PDF for .NET。它是一個(gè)功能豐富的 API,可從 .NET 應(yīng)用程序中創(chuàng)建和 PDF 文件。
點(diǎn)擊下載最新版Aspose.PDF for .NET
下面是使用 C# 向 PDF 文件添加文本的步驟。
下面的示例代碼展示了如何向 PDF 文件添加文本。
// 完整的例子和數(shù)據(jù)文件請(qǐng)到 //github.com/aspose-pdf/Aspose.PDF-for-.NET // 打開(kāi)文檔 文檔 pdfDocument = new Document("input.pdf"); // 獲取特定頁(yè)面 Page pdfPage = (Page)pdfDocument.Pages[1]; // 創(chuàng)建文本片段 TextFragment textFragment = new TextFragment("正文"); textFragment.Position = new Position(100, 600); // 設(shè)置文本屬性 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); // 創(chuàng)建 TextBuilder 對(duì)象 TextBuilder textBuilder = new TextBuilder(pdfPage); // 將文本片段附加到 PDF 頁(yè)面 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的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn