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

Free Support Forum - aspose.com

Aspose PDF Document - FreeTextAnnotation does not show text

Hello,

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";
page.Annotations.Add(annotation);
_pdfDocument.Save(destFilePath);

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?

@nir-1

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)

@nir-1

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);
tb.AppendText(textFragment);
// Save the document
pdfDocument.Save(MyDir + "22.2.pdf");

I have found the problem.

After several tries using the code here:
https://docs.aspose.com/pdf/net/text-annotation/#set-formatting-of-freetextannotation

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?

@nir-1

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.

@nir-1

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);
    page.Paragraphs.Add(canvas);

    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;
    //canvas.Shapes.Add(rect);
    rect.Text = new TextFragment("test address");
    rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
    canvas.Shapes.Add(rect);
    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?

@nir-1

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);
page.Paragraphs.Add(canvas);

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);

canvas.Shapes.Add(rect);
doc.Save(dataDir + "22.2.pdf");