Question: How do I determine the potential height of my text annotation?
Answer: You can use the PDFNet SDK and create a new PDF with a “dummy” page in it, without transparency, and add a free text annotation with your text. You can then get the visible bounding box for the page. Please note that this is for getting the height of the annotation, the width must be a known value.
PDFNet.Initialize("[YOUR LICENSE HERE]");
using (PDFDoc pdf_doc = new PDFDoc())
{
double page_height = 792;
double free_text_border_thickness = 2;
double free_text_width = 250;
// Create the new PDF page of the annotation width.
Page pdf_page = pdf_doc.PageCreate(new Rect(0, 0,
free_text_width - 2 * free_text_border_thickness, page_height));
pdf_doc.PagePushBack(pdf_page);
// Add a new free text annotation to the page of the same width.
pdftron.PDF.Annots.FreeText text_annot =
pdftron.PDF.Annots.FreeText.Create(pdf_doc, new Rect(0, 0,
free_text_width - 2 * free_text_border_thickness, page_height));
text_annot.SetContents("This is some text for\nthe free text annotation\n as an example to\nget the bounding box for a\nstring of text.");
Annot.BorderStyle border_style = new Annot.BorderStyle(Annot.BorderStyle.Style.e_solid, 0, 0, 0);
text_annot.SetBorderStyle(border_style);
pdf_page.AnnotPushBack(text_annot);
// Refresh the appearance and flatten.
text_annot.RefreshAppearance();
text_annot.Flatten(pdf_page);
// The resulting bounding box and it's height.
Rect bounding_box = pdf_page.GetVisibleContentBox();
Console.WriteLine($"Visible Bounding Box: ({bounding_box.x1}, {bounding_box.y1}) ({bounding_box.x2}, {bounding_box.y2})");
// Get the annotation height by subtracting the border width.
double final_free_text_height = bounding_box.Height() + 2 * free_text_border_thickness;
Console.WriteLine($"Final Text Height: {final_free_text_height}");
}
You can find descriptions of the API calls used in this sample in our online documentation here:
https://sdk.apryse.com/api/PDFTronSDK/dotnet/index.html
- This example was done using our .NET API but we support a wide range of languages and platforms.