PDF管理控件Aspose.PDF for .Net使用教程(四十八):在現(xiàn)有的PDF文件中添加文本
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來(lái)的系列教程中,將為開(kāi)發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何管理PDF文件的頁(yè)眉和頁(yè)腳。
>>Aspose.PDF for .NET更新至最新版v20.11,歡迎下載體驗(yàn)。
在現(xiàn)有的PDF文件中添加文本
要將文本添加到現(xiàn)有的PDF文件中,需要以下步驟:
- 使用文檔對(duì)象打開(kāi)輸入的PDF。
- 獲取要向其添加文本的特定頁(yè)面。
- 使用輸入文本以及其他文本屬性創(chuàng)建一個(gè)TextFragment對(duì)象。從特定頁(yè)面創(chuàng)建的TextBuilder對(duì)象(要向其添加文本)允許您使用AppendText方法將TextFragment對(duì)象添加到頁(yè)面。
- 調(diào)用Document對(duì)象的Save方法并保存輸出的PDF文件。
添加文字
以下代碼段顯示了如何在現(xiàn)有的PDF文件中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Open document Document pdfDocument = new Document(dataDir + "input.pdf"); // Get particular page Page pdfPage = (Page)pdfDocument.Pages[1]; // Create text fragment TextFragment textFragment = new TextFragment("main text"); textFragment.Position = new Position(100, 600); // Set text properties 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); // Create TextBuilder object TextBuilder textBuilder = new TextBuilder(pdfPage); // Append the text fragment to the PDF page textBuilder.AppendText(textFragment); dataDir = dataDir + "AddText_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
從流中加載字體
以下代碼片段顯示了在將文本添加到PDF文檔時(shí)如何從Stream對(duì)象加載Font。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); string fontFile = ""; // Load input PDF file Document doc = new Document( dataDir + "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); } dataDir = dataDir + "LoadingFontFromStream_out.pdf"; // Save resulting PDF document. doc.Save(dataDir); }
使用TextParagraph添加文本
以下代碼段顯示了如何使用TextParagraph類(lèi)在PDF文檔中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Open document Document doc = new Document(); // Add page to pages collection of Document object Page page = doc.Pages.Add(); TextBuilder builder = new TextBuilder(page); // Create text paragraph TextParagraph paragraph = new TextParagraph(); // Set subsequent lines indent paragraph.SubsequentLinesIndent = 20; // Specify the location to add TextParagraph paragraph.Rectangle = new Aspose.Pdf.Rectangle(100, 300, 200, 700); // Specify word wraping mode paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords; // Create text fragment TextFragment fragment1 = new TextFragment("the quick brown fox jumps over the lazy dog"); fragment1.TextState.Font = FontRepository.FindFont("Times New Roman"); fragment1.TextState.FontSize = 12; // Add fragment to paragraph paragraph.AppendLine(fragment1); // Add paragraph builder.AppendParagraph(paragraph); dataDir = dataDir + "AddTextUsingTextParagraph_out.pdf"; // Save resulting PDF document. doc.Save(dataDir);
將超鏈接添加到TextSegment
PDF頁(yè)面可以包含一個(gè)或多個(gè)TextFragment對(duì)象,其中每個(gè)TextFragment對(duì)象可以具有一個(gè)或多個(gè)TextSegment實(shí)例。為了設(shè)置TextSegment的超鏈接,可以在提供Aspose.Pdf.WebHyperlink實(shí)例的對(duì)象時(shí)使用TextSegment類(lèi)的Hyperlink屬性。請(qǐng)嘗試使用以下代碼段來(lái)滿足此要求。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create document instance Document doc = new Document(); // Add page to pages collection of PDF file Page page1 = doc.Pages.Add(); // Create TextFragment instance TextFragment tf = new TextFragment("Sample Text Fragment"); // Set horizontal alignment for TextFragment tf.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; // Create a textsegment with sample text TextSegment segment = new TextSegment(" ... Text Segment 1..."); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Create a new TextSegment segment = new TextSegment("Link to Google"); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Set hyperlink for TextSegment segment.Hyperlink = new Aspose.Pdf.WebHyperlink("www.google.com"); // Set forground color for text segment segment.TextState.ForegroundColor = Aspose.Pdf.Color.Blue; // Set text formatting as italic segment.TextState.FontStyle = FontStyles.Italic; // Create another TextSegment object segment = new TextSegment("TextSegment without hyperlink"); // Add segment to segments collection of TextFragment tf.Segments.Add(segment); // Add TextFragment to paragraphs collection of page object page1.Paragraphs.Add(tf); dataDir = dataDir + "AddHyperlinkToTextSegment_out.pdf"; // Save resulting PDF document. doc.Save(dataDir);
使用OTF字體
用于.NET的Aspose.PDF提供了在創(chuàng)建/處理PDF文件內(nèi)容時(shí)使用Custom / TrueType字體的功能,以便使用默認(rèn)系統(tǒng)字體以外的內(nèi)容顯示文件內(nèi)容。從.NET 10.3.0的Aspose.PDF版本開(kāi)始,我們提供了對(duì)Open Type Fonts的支持。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Text(); // Create new document instance Document pdfDocument = new Document(); // Add page to pages collection of PDF file Aspose.Pdf.Page page = pdfDocument.Pages.Add(); // Create TextFragment instnace with sample text TextFragment fragment = new TextFragment("Sample Text in OTF font"); // Find font inside system font directory // Fragment.TextState.Font = FontRepository.FindFont("HelveticaNeueLT Pro 45 Lt"); // Or you can even specify the path of OTF font in system directory fragment.TextState.Font = FontRepository.OpenFont(dataDir + "space age.otf"); // Specify to emend font inside PDF file, so that its displayed properly, // Even if specific font is not installed/present over target machine fragment.TextState.Font.IsEmbedded = true; // Add TextFragment to paragraphs collection of Page instance page.Paragraphs.Add(fragment); dataDir = dataDir + "OTFFont_out.pdf"; // Save resulting PDF document. pdfDocument.Save(dataDir);
還想要更多嗎?您可以點(diǎn)擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢(xún)和咨詢(xún)。