Reputation: 763
We're using ITextSharp for reasons I do not understand and we dont have the book yet and I have an annoying problem that I'd appreciate help with.
I'm working with a footer and I can not get it to align as I want it.
The function I have takes a list of strings, but it's generally 4 strings I want on a row each. It does not seam like itextsharp can handle strings with linebreaks so that's the reason for the list.
Now this does not position correctly for me, the first string looks ok, but then the second string is a bit longer and it's half outside the document as is the third string and the 4th is not even visible even thou there is 1 cm of space left.
Thanks for help!
public PdfTemplate AddPageText(IList<string> stringList, PdfWriter writer)
{
var cb = writer.DirectContent;
PdfTemplate footerTemplate = cb.CreateTemplate(450, 120); //55);
footerTemplate.BeginText();
BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
footerTemplate.SetFontAndSize(bf2, 9);
footerTemplate.SetColorStroke(BaseColor.DARK_GRAY);
footerTemplate.SetColorFill(BaseColor.GRAY);
footerTemplate.SetLineWidth(3);
footerTemplate.LineTo(50, footerTemplate.YTLM);
int y = 10;
foreach (string text in stringList)
{
float widthoftext = 500.0f - bf2.GetWidthPoint(text, 9);
footerTemplate.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text, widthoftext, 50 - y, 0);
y += y;
}
footerTemplate.EndText();
return footerTemplate;
}
Upvotes: 2
Views: 2865
Reputation: 2265
If you are doing string placement and using DirectContent then you are responsible for the content. In this case you will need to calculate the string rectangles and wrap accordingly.
I would suggest, however, moving to using a table with cells for the text. Tables wrap text and handle some of the issues you are dealing with.
Upvotes: 1