Home > Code > C# > Add Simple/Underline Text or Border Around Added Text in a PDF File Using .NET

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

by sher azam   on Nov 26, 2013   Category: C#   |  Views: 2637    |  Points: 25   |  Gold 


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 Aspose.Pdf for .NET

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

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



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts