Right Aligning Text in PDFpcell

Right aligning text in PdfPCell

I'm the original developer of iText, and the problem you're experiencing is explained in my book.

You're mixing text mode and composite mode.

In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.

How to solve your problem?

Either work in text mode by adding your Phrase to the cell in a different way.
Or work in composite mode and use a Paragraph for which you define the alignment.

The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.

align cell in itextpdf java

You are mixing text mode with composite mode.

This is text mode:

pcell = new PdfPCell(new Phrase(StrArray[i][j]));
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);

In this case, the alignment of the cell will be used for the alignment of the text.

This is composite mode:

pcell = new PdfPCell();
Paragraph p = new Parapgraph(StrArray[i][j])
p.setAlignment(Element.ALIGN_RIGHT);
pcell.addElement(p);

In this case, the alignment of the cell is ignored, in favor of the alignment of the element.

How to know the difference between text mode and composite mode?

iText automatically switches from text mode to composite mode in a PdfPCell the moment you use the addElement() method. As soon as you do this, some properties defined at the cell level are ignored. This explains why the content you are adding isn't right-aligned.

How to align text to specific position in PDFCell in IText

I managed it by creating a function that creates spaces and append it to the paragraph:

  String addspace(int i, String str) {
StringBuilder str1 = new StringBuilder();
for (int j = 0; j < i; j++) {
str1.append(" ");
}
str1.append(str);
return str1.toString();

}

Aligning text in iText in a single row

You can use a PdfPTable with 2 columns, the first right aligned and the last left aligned. Then set the designer padding on the cells content.
For example:

PdfPTable tbl = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Phrase("1."));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("34."));
cell.disableBorderSide(Rectangle.BOX);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
tbl.addCell(cell);
cell = new PdfPCell(new Phrase("some more random text"));
cell.disableBorderSide(Rectangle.BOX);
tbl.addCell(cell);

You can see that cell border are disabled (disableBorderSide method).
You can also adjust the minimum height of cells using setMinimumHeight method.

itextsharp - Add to a PdfPTable a cell with right-aligned italic text

The problem is that you are using a Phrase. Use a Paragraph where you can set the font and the alignment.

Align Cells inside a pdfTable - ITextSharp

What are you doing wrong?

You create a PdfPCell named cell for which you define an alignment (in an ugly way, you should use Element.ALIGN_CENTER so that we can read and understand your code) and a colspan of 7. But you don't use that cell object anywhere...

You create a PdfPTable for which you define an alignment (in the same ugly way). But this defines the alignment of the table as a whole, not the content of the cells.

You then add a series of cells to this table using the AddCell() method. This means that you want iText to create the PdfPCell. In that case, you can pre-define some properties in the default cell property:

pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

You can change the properties of the default cell after adding a certain number of cells, for instance:

 pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
pdfTable.AddCell(new iText.Phrase("NF", font4));
pdfTable.AddCell(new iText.Phrase("Emissão", font4));
pdfTable.AddCell(new iText.Phrase("Vencimento", font4));
pdfTable.AddCell(new iText.Phrase("Dias", font4));
pdfTable.AddCell(new iText.Phrase("Valor(R$)", font4));
pdfTable.AddCell(new iText.Phrase("Encargos(R$)", font4));
pdfTable.AddCell(new iText.Phrase("Vlr. Final(R$)", font4));
pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;

All the cells added with AddCell() after this last line will be right-aligned.

iTextSharp PdfPCell align image to bottom of cell

The question is a bit ambiguous because you create a Table with two columns, but add cells without verifying the employees collection has an even number of elements, which will throw if not....

Assuming you really do want both the text and the image in a single cell, probably the simplest way to get the layout you want is to implement IPdfPCellEvent:

public class BottomRightImage : IPdfPCellEvent
{
public Image Image { get; set; }

public void CellLayout(
PdfPCell cell,
Rectangle position,
PdfContentByte[] canvases)
{
if (Image == null) throw new InvalidOperationException("image is null");

PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
Image.SetAbsolutePosition(
position.Right - Image.ScaledWidth - cell.PaddingRight,
position.Bottom + cell.PaddingBottom
);
canvas.AddImage(Image);
}
}

Then set the CellEvent property on the PdfPCell. Here's a simple working example:

using (var stream = new MemoryStream())
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, stream);
document.Open();
var table = new PdfPTable(2)
{
HorizontalAlignment = Element.ALIGN_LEFT,
TotalWidth = 400f,
LockedWidth = true
};

var image = Image.GetInstance(imagePath);
image.ScaleAbsolute(40, 40);
var cellEvent = new BottomRightImage() { Image = image };

var testString =
@"first name: {0}
last name: {0}
ID no: {0}";
for (int i = 0; i < 2; ++i)
{
var cell = new PdfPCell()
{
FixedHeight = 140f,
PaddingLeft = 30f,
PaddingRight = 10f,
PaddingTop = 20f,
PaddingBottom = 5f
};
cell.CellEvent = cellEvent;

var p = new Paragraph(string.Format(testString, i))
{
Alignment = Element.ALIGN_TOP | Element.ALIGN_LEFT
};
cell.AddElement(p);
table.AddCell(cell);
}
document.Add(table);
}
File.WriteAllBytes(outputFile, stream.ToArray());
}

And PDF output:

Sample Image

An all-black square image is used to show the PdfPCell padding is taken into account.



Related Topics



Leave a reply



Submit