翻譯|使用教程|編輯:李顯亮|2021-08-30 10:59:01.727|閱讀 407 次
概述:在某些情況下,可能需要以編程方式向現有 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@fc6vip.cn