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