C# and .NET Code for Adding Text in Existing PDF file and Formatting the Added Text

.NET Developers can now add text in existing PDF File and can perform other formatting operation on the added text.

Online PR News – 28-November-2013 – Lane Cove, NSW – This technical tip shows how to .NET Developers canadd text in an existing PDF file using Aspose.Pdf for .NET Library. 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.


//Open document
Document pdfDocument = new Document("input.pdf");
//Get particular page
Page pdfPage = (Page)pdfDocument.Pages[1];

//Create text fragment
TextFragmenttextFragment = 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
TextBuildertextBuilder = new TextBuilder(pdfPage);

//Append the text fragment to the PDF page

//Save document


'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

'Save document

Adding Underline Text

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


// create documentation object
Document pdfDocument = new Document();
// add age page to PDF document
// create TextBuilder for first page
TextBuildertb = 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
// save the PDF file


' create documentation object
Dim pdfDocument As Document = New Document()
' add age page to PDF document
' createTextBuilder 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)
' appendTextFragment to PDF file
' save the PDF file

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.


PdfContentEditor editor = new PdfContentEditor();
LineInfolineInfo = 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),"");

Loading Font from Stream

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


// load input PDF file
Document doc = new Document("input.pdf");
// creat text builder object for first page of document
TextBuildertextBuilder = new TextBuilder(doc.Pages[1]);
// create text fragment with sample string
TextFragmenttextFragment = new TextFragment("Hello world");
// load the TrueType font into stream object
using (FileStreamfontStream = 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
// save the updated PDF file

More about Aspose.Pdf for .NET

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Download Aspose.Pdf for .NET at: http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/default.aspx

- Read More about Working with Document Conversion: http://www.aspose.com/docs/display/pdfnet/Working+with+Document+Conversion