1

I'm trying to center align a block of text, however getting inconsistent results. Here is a rough idea of my code:

baseCanvas.ShowTextAligned("Header 1", 555, 839, TextAlignment.CENTER, 0);
baseCanvas.ShowTextAligned("Test test test ...", 240, 809, TextAlignment.CENTER, 0);

Here is the PDF Output:

Output

However I'm trying to achieve the following: Design

I've checked the iText documentation, but is there a way to do this without having to create tables and cells?

Davy-F
  • 123
  • 1
  • 2
  • 12
  • Do any of these work for you? https://stackoverflow.com/questions/14373269/align-paragraph-at-the-center-of-the-page – KSib Sep 10 '18 at 14:54
  • @KSib No, that doesn't work because those answers are for iText 5 and the question says that iText 7 is used. – Bruno Lowagie Sep 10 '18 at 16:35

3 Answers3

7

When you do:

baseCanvas.ShowTextAligned("Some text", x, y, TextAlignment.CENTER, 0);

Then you want the coordinate (x, y) to coincide with the middle of the text "some text".

In your code snippet, you are centering some text around the coordinate (555, 839) and some text around the coordinate (40, 809) which explains the difference.

Since you are using iText 7, why don't you take advantage of the fact that you can now easily position Paragraph objects at absolute positions? The iText 7 jump-start tutorial for .NET already introduces some of the basic building blocks, but the Building blocks tutorial goes into more depth.

Take a look at the first example of chapter 2 and adapt it like this:

PdfPage page = pdf.AddNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
Text title =
    new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
Text author = new Text("Robert Louis Stevenson").SetFont(font);
Paragraph p = new Paragraph().Add(title).Add(" by ").Add(author);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
canvas.Add(p);
canvas.Close();

This should add the text inside the rectangle (36, 650, 100, 100) and center all content.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • This is doing the trick. Thank you. How would I then rotate a block of text, as I'm not specifying an angle? – Davy-F Sep 11 '18 at 10:02
  • Any info on the above, @bruno? – Davy-F Sep 17 '18 at 10:24
  • That's a different question. Some objects can be rotated; sometimes creating a `PdfXObject` is required (and you can rotate those). – Bruno Lowagie Sep 17 '18 at 10:51
  • Thanks for the reply. Appreciate it's a new question, however my original method "baseCanvas.ShowTextAligned..." allowed a radius to be passed as a parameter. Would I have to convert the rectangle or the paragraph to a PdfXObject? – Davy-F Sep 17 '18 at 12:18
  • The `ShowTextAligned()` method in `Canvas` also allowed you to pass a radius. See http://itextsupport.com/apidocs/iText7/latest/com/itextpdf/layout/RootElement.html#showTextAligned-com.itextpdf.layout.element.Paragraph-float-float-int-com.itextpdf.layout.property.TextAlignment-com.itextpdf.layout.property.VerticalAlignment-float- The main difference is that the radius in iText 5 is expressed in degrees, whereas the radius in iText 7 is expressed in radians. Your initial comment wasn't about `ShowTextAligned()`; it was about rotating a *block* of text. Your comments confuse me. – Bruno Lowagie Sep 17 '18 at 14:13
  • Hi Bruno. Previously I was able to rotate text easily by passing a degree. How would I be able to rotate the text with the chosen answer example above? Thank you. – Davy-F Sep 19 '18 at 08:20
  • If you use `ShowTextAligned()`, convert degrees into radians as explained [here](https://stackoverflow.com/questions/19677052/) and use that value for the `radAngle` parameter. If you use `canvas.Add(p)`, check which properties you can set on the `p` object [here](https://developers.itextpdf.com/content/itext-7-building-blocks/appendix/b-blockelement). As you can see, there's a method `SetRotationAngle()`. This method is demonstrated [here](https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-5-adding-abstractelement-objects-part-2) (see figure 5.9). Read the documentation! – Bruno Lowagie Sep 19 '18 at 08:32
  • since march 2020 those links are broken :( – Don Diego Mar 19 '20 at 11:27
0

I do it like this. Where the document is created, get the width of the document.

var document = new Document(pdfDoc);
var pageSize = pdfDoc.GetDefaultPageSize();
var width = pageSize.GetWidth() - document.GetLeftMargin() - document.GetRightMargin();

Then create the paragraph with this function.

private Paragraph CenteredParagraph(Text text, float width)
{
    var tabStops = new List<TabStop> { new TabStop(width / 2, TabAlignment.CENTER) };
    var output = new Paragraph().AddTabStops(tabStops);
    output.Add(new Tab())
            .Add(text);
    return output;
}

After that just add the paragraph to the document.

document.Add(CenteredParagraph("All the text to add that is centered.");
Red_Phoenix
  • 482
  • 6
  • 22
0
    Div div2 = new Div();
    div2.setPaddingLeft(35);
    div2.setPaddingRight(35);
    div2.setPaddingBottom(5);
    div2.add(new Paragraph().add(new Paragraph("Hola Mundo")
                    .setFontSize(12)
                    .setTextAlignment(TextAlignment.JUSTIFIED)
                    .setPaddingLeft(10)
            )
            .setPaddingBottom(4));
    document.add(div2);
  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '22 at 23:59