may help you . You need to use a JTextPane and use attributes. The following should center all the text:
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
doc.setParagraphAttributes(0, doc.getLength(), center, false);
should help you out In the first example, you're working in "text mode" and the properties of the cell are respected. As soon as you use AddElement (), you're working in "composite mode" and the properties of the cell are ignored (as documented in my book). Instead, the properties of the separate elements are used.
iTextSharp: which alignment properties are used in a PdfPCell?
will help you You are confusing text mode with composite mode. In the first code snippet, you work in text mode. This means that the content of the cell is considered to be text only and the properties of the cell are respected, whereas the properties of the elements added to the cell are ignored.
This might help you See this answer for a discussion on Text Mode versus Composite Mode. Basically, if you use AddElement() you need to set the alignment on your object instead of the cell itself. Chunk doesn't have alignment, however, so you need to wrap your chunk in something that does which is the Paragraph.
var table = new PdfPTable(1);
//Create our chunk with anchor
var webAddress = new Chunk("hello");
//Create a paragraph holding the chunk and set the alignment on _it_
var para = new Paragraph(webAddress);
para.Alignment = Element.ALIGN_CENTER;
//Create our cell
PdfPCell ProductCodeCell = new PdfPCell();
//Add the paragrph to it
//Padding on the cell still works
ProductCodeCell.Padding = 30f;
//Add cell to table
Horizontal alignment of multi-line text in a matplotlib text box