We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose PDF Document - FreeTextAnnotation does not show text


I’m trying to add a simple text box to a PDF document, using this code:

_pdfDocument = new Aspose.Pdf.Document(sourceFilePath);
Aspose.Pdf.Page page = _pdfDocument.Pages[1];
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(10,10,100,50);
DefaultAppearance appearance = new DefaultAppearance();
FreeTextAnnotation annotation = new FreeTextAnnotation(page, rect, appearance);
annotation.Contents = "This is the text";

But the result PDF shows the TextBox frame but without the text. When double-clicking inside I can see the text, then if I add any character or make any change to that text, it will show correctly.

What am I missing?


Could you please attach your input, problematic and expected output PDF files here for testing? We will investigate the issue and provide you more information on it.

Here is the source pdf: source.pdf (30.8 KB)

And the output: output.pdf (30.6 KB)


Please use the following code example to add textbox in PDF. If you want to work with text annotation, please read the following article.
Using Text Annotation for PDF

// Create new document object
Document pdfDocument = new Document();
// Get particular page
Page pdfPage = (Page)pdfDocument.Pages.Add();
// 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.LightGray;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
// Set StrokingColor property for drawing border (stroking) around text rectangle
textFragment.TextState.StrokingColor = Aspose.Pdf.Color.Black;
// Set DrawTextRectangleBorder property value to true
textFragment.TextState.DrawTextRectangleBorder = true;
TextBuilder tb = new TextBuilder(pdfPage);
// Save the document
pdfDocument.Save(MyDir + "22.2.pdf");

I have found the problem.

After several tries using the code here:

I have figure out that the DefaultAppearance must have a font size and color specified. Otherwise the text is set to font size zero…

This is not happening when using the CreateFreeText method from ContentEditor object.

After finding the ‘font size’ problem, I’m still struggling with it.

I need to display the FreeTextAnnotation text in the center (horizontally & vertically) of the rect.
I also need to control the border color.

Is it possible?


You can use TextStyle.HorizontalAlignment property for FreeTextAnnotation as HorizontalAlignment.Center for horizontal alignment. However, there is no property for vertical alignment. Please check the following code snippet.

freetext.TextStyle.HorizontalAlignment = HorizontalAlignment.Center;

Could you please attach your expected output PDF here for our reference? We will then provide you more information on it.


Further to my previous post, you can use following code example achieve your requirement. Hope this helps you.

Document doc = new Document();

    Page page = doc.Pages.Add();
    page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

    var canvas = new Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);//tf.Rectangle.Width, pg.PageInfo.Height
                                                                                            //canvas.Margin = new MarginInfo() { Bottom = 0, Left = 0, Right = 0, Top = 0 };
                                                                                            //canvas.Left = 0;
                                                                                            //canvas.Top = 0;
                                                                                            //canvas.Border = new BorderInfo(BorderSide.All, 1f, Color.Black);

    Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 354, 200, 200); //new Aspose.Pdf.Drawing.Rectangle(0, 700, 100, 750);
    var c = ColorTranslator.FromHtml("#FFFF00");
    //rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
    //rect.GraphInfo.FillColor = alphaColor;
    rect.Text = new TextFragment("test address");
    rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
    doc.Save(dataDir + "22.2.pdf");

This is an interesting solution/workaround, however, I can not set the text size or font.
Or can I?


You can change the font size of text as shown below using TextState class.

Document doc = new Document();
Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

var canvas = new Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);

Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 354, 200, 200); 
var c = ColorTranslator.FromHtml("#FFFF00");
var textFragment = new TextFragment("test address")
    TextState =
                FontSize = 24,
                Font = FontRepository.FindFont("Arial"),
                BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White),
                ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black)

rect.Text = textFragment;
rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);

doc.Save(dataDir + "22.2.pdf");