Im trying to figure out how to get my text inside a PdfPCell to show in the middle. I have tried many different options, like:
myCell.VerticalAlignment = Element.ALIGN_MIDDLE; myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE;
None of that works for me. VerticalAlignment takes an int, so I tried to make a loop, to see if i could find the right number, but everything just get left bottom align..
Document myDocument = new Document(PageSize.A4);
PdfWriter myPdfWriter = PdfWriter.GetInstance(myDocument, new FileStream(strPDFFilePath, FileMode.Create));
myDocument.Open();
myDocument.NewPage();
PdfContentByte myPdfContentByte = myPdfWriter.DirectContent;
PdfPCell myCell;
Paragraph myParagraph;
PdfPTable myTable = new PdfPTable(4);
myTable.WidthPercentage = 100;
myTable.SetWidths(new int[4] { 25, 25, 25, 25 });
myTable.DefaultCell.BorderWidth = 1;
myTable.DefaultCell.BorderColor = BaseColor.RED;
for (int i = -100; i < 100; i++)
{
myParagraph = new Paragraph(String.Format("Alignment: {0}", i));
myParagraph.Font.SetFamily("Verdana");
myParagraph.Font.SetColor(72, 72, 72);
myParagraph.Font.Size = 11;
myCell = new PdfPCell();
myCell.AddElement(myParagraph);
myCell.HorizontalAlignment = i;
myCell.VerticalAlignment = i;
myTable.AddCell(myCell);
}
myDocument.Add(myTable);
myDocument.Add(new Chunk(String.Empty));
myDocument.Close();
I think the basic problem you're having is that you're adding text to iTextSharp Paragraph
objects and then attempting to set this text's alignment using the PdfPCell
object that contains it. I'm not sure if the PdfPCell.VerticalAlignment
property is only for a PdfPCell
's text, or if aligning the Paragraph
object inside the PdfPCell
doesn't have any affect you can see in your test.
You're also setting myCell.HorizontalAlignment
and myCell.VerticalAlignment
to the index value in your for
loop. I think you meant to use 1 instread of i
.
Anyway, setting a PdfPCell's HorizontalAlignment
and VerticalAlignment
properties do work though. Below is a small method that demonstrates this. I wrote it very loosely based on what you were trying to do; if it's close enough to what you're trying to do perhaps you can use this as a starting point in your project.
private void TestTableCreation() {
using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfPTable table = new PdfPTable(4);
for (int i = -100; i < 100; i++) {
PdfPCell cell = new PdfPCell(new Phrase(String.Format("Alignment: {0}", i)));
// Give our rows some height so we can see test vertical alignment.
cell.FixedHeight = 30.0f;
// ** Try it **
//cell.HorizontalAlignment = Element.ALIGN_LEFT;
//cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
cell.VerticalAlignment = Element.ALIGN_TOP;
//cell.VerticalAlignment = Element.ALIGN_MIDDLE;
//cell.VerticalAlignment = Element.ALIGN_BOTTOM;
table.AddCell(cell);
}
doc.Add(table);
doc.Close();
}
}