Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Add Simple/Underline Text or Border Around Added Text in a PDF Using .NET

sheraz786sheraz786 Posts: 19Member

This technical tip shows how to .NET Developers can add text in an existing PDF file using Aspose.Pdf. Developers can add simple or underline text as well as can add border around added text in an existing PDF document. To add text to an existing PDF file Open the input PDF using the Document object and get the particular page to which you want to add the text. After that create a TextFragment object with the input text along with other text properties. The TextBuilder object created from that particular page – to which you want to add the text – allow you to add the TextFragment object to the page using the AppendText method. Now just call the Document object's Save method and save the output PDF file.

The following code snippet shows you how to add text in an existing PDF file.

[C#]

//Open document Document pdfDocument = new Document("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);

//Save document pdfDocument.Save("output.pdf");

[VB.NET]

'Open document Dim pdfDocument As New Document("input.pdf") 'Get particular page Dim pdfPage As Page = CType(pdfDocument.Pages(1), Page)

'Create text fragment Dim textFragment As 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 Dim textBuilder As New TextBuilder(pdfPage)

'Append the text fragment to the PDF page textBuilder.AppendText(textFragment)

'Save document pdfDocument.Save("output.pdf")

Adding Underline Text

The following code snippet shows you how to add Underline text while creating a new PDF file.

[C#]

// create documentation object Document pdfDocument = new Document(); // add age page to PDF document pdfDocument.Pages.Add(); // create TextBuilder for first page TextBuilder tb = new TextBuilder(pdfDocument.Pages[1]); // TextFragment with sample text TextFragment fragment = new TextFragment("Test message"); // set the font for TextFragment fragment.TextState.Font = FontRepository.FindFont("Arial"); fragment.TextState.FontSize = 10; // set the formatting of text as Underline fragment.TextState.Underline = true; // specify the position where TextFragment needs to be placed fragment.Position = new Position(10, 800); // append TextFragment to PDF file tb.AppendText(fragment); // save the PDF file pdfDocument.Save("c:/pdftest/UnderLine.pdf");

[VB.NET]

' create documentation object Dim pdfDocument As Document = New Document() ' add age page to PDF document pdfDocument.Pages.Add() ' create TextBuilder for first page Dim tb As Aspose.Pdf.Text.TextBuilder = New Aspose.Pdf.Text.TextBuilder(pdfDocument.Pages(1)) ' TextFragment with sample text Dim fragment As TextFragment = New TextFragment("Test message") ' set the font for TextFragment fragment.TextState.Font = FontRepository.FindFont("Arial") fragment.TextState.FontSize = 10 ' set the formatting of text as Underline fragment.TextState.Underline = True ' specify the position where TextFragment needs to be placed fragment.Position = New Position(10, 800) ' append TextFragment to PDF file tb.AppendText(fragment) ' save the PDF file pdfDocument.Save("c:/pdftest/UnderLine.pdf")

Adding a Border Around Added Text

You have control over the look and feel of the text you add. The example below shows how to add a border around a piece of text that you have added by drawing a rectangle around it. Find out more about the PdfContentEditor class.

[C#]

PdfContentEditor editor = new PdfContentEditor(); editor.BindPdf("c:/pdftest/UniCode_Characters.pdf"); LineInfo lineInfo = new LineInfo(); lineInfo.LineWidth = 2; lineInfo.VerticeCoordinate = new float[] { 0, 0, 100, 100,50,100}; lineInfo.Visibility = true; editor.CreatePolygon(lineInfo,1, new System.Drawing.Rectangle(0, 0, 0, 0),""); editor.Save("c:/pdftest/UniCode_Characters_example_out.pdf");

Loading Font from Stream

The following code snippet shows how to load Font from Stream object when adding text to PDF document.

[C#]

// load input PDF file Document doc = new Document("input.pdf"); // creat 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"); // load the TrueType font into stream object using (FileStream fontStream = File.OpenRead(@"C:\CustomerFont.ttf")) { // 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 the updated PDF file doc.Save("c:/TTF_FontAdded.pdf");

More about C# PDF API

Comments

Sign In or Register to comment.