0

I was creating a C# Console Application that fills in the details in a certificate template (pdf) file using ITextSharp. The text that needs to be filled is dynamically generated.

If the dynamically generated text exceeds a certain size the field name gets truncated. I have already thought of an algorithm to solve this problem for which I require the maximum number of characters that can be accommodated in a page with the given font and font-size. The width of the page where the text has to be accommodated is given to us already.

I used the following code:

string text = "This is the test string that needs to be accommodated";

// Registering a font
FontFactory.Register("somefont.ttf", "script");
var script = FontFactory.GetFont("script", 20f); // registers the font with the given type and size. 
Phrase phrase_text = new Phrase();
phrase_text.Add(new Chunk(text, script));
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(phrase_text), PageSize.A4.Height/2, 140, 0);

This code seems to work fine, but when I am adding a bigger text in the screen, the text gets truncated with the alignment still in the center (in the horizontal sense).

I was reading something on using the Graphics library, but could not resolve the issue.

Aditya
  • 1,172
  • 11
  • 32

1 Answers1

1

You say that you are filling out a template. That assumes that you have at least one AcroForm field in your document. Filling out fields is explained in the answer to this question: itextsharp setting the stamper FormFlatttening=true results in no output

Usually, the field has a property that defines the font size that needs to be used, but you are right: if the text doesn't fit the rectangle defined by the form field, the text gets truncated. You can change this by setting the font size to "auto". This way, the font size will decrease if the text doesn't fit. This is explained here: Set AcroField Text Size to Auto

Or you could use a multiline field. See How to get the row count of a multiline field?

If you don't have a form field (which would be strange, because that means that you have to program the coordinates, e.g. PageSize.A4.Height/2, 140), you shouldn't use the showTextAligned() method. If there is no way you can define the location using a form field (but I can't think of any argument why you couldn't), you should take a look at the MovieAds example (taken from my book iText in Action).

I have a method AddParagraph that looks like this:

public bool AddParagraph(Paragraph p, PdfContentByte canvas, 
    AcroFields.FieldPosition f, bool simulate) 
{
  ColumnText ct = new ColumnText(canvas);
  ct.SetSimpleColumn(
    f.position.Left, f.position.GetBottom(2),
    f.position.GetRight(2), f.position.Top
  );
  ct.AddElement(p);
  return ColumnText.HasMoreText(ct.Go(simulate));
}

In this example, I create a ColumnText object for which I define a column. I add a paragraph, and I render that paragraph. If simulate is true, I don't add it for real, I add it virtually to see if the paragraph fits. If it fits, I call the method anew for real (with simulate = false).

I repeat this as many time as needed, decreasing the font with 0.2 user units at every try. Only if the font gets smaller than 6pt, I allow iText to truncate the content.

while (AddParagraph(CreateMovieParagraph(movie, size),
          canvas, f, true) && size > 6) 
{
    size -= 0.2f;
}
Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165