We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Position a table using setLeft function in aspose pdf

All, I am trying to position this table manually by using the table.setLeft(200f) function seen below. This is not working. It works for a Text object, but not for tables.

Please advise.

public void addBYHeaders() throws InternalPDFException {
// add table
Table table = new Table();
table.setColumnWidths("192 68 95");
table.setMarginTop("10");
table.setLeft(200f);
// draw border lines
table.setDefaultCellBorder(new BorderPartAll(), BorderStyleTypeEnum.NORMAL, "black", null, null, 1f, null);

// cell format
Text leftCell = this.getText(this.getheaderFontSize());
leftCell.setAlignment(TextAlignmentTypeEnum.CENTER);
leftCell.setIsTrueTypeFontBold(true);
leftCell.setBackgroundColor(this.getheaderColor());
leftCell.setMarginTop("2");
leftCell.setMarginBottom("2");
leftCell.setMarginLeft("2");
leftCell.setMarginRight("2");
// create row
Row row = new Row();
row.setBackgroundColor(this.getheaderColor());

// write
row.setCells(new Cell(),
this.getText(this.getheaderFontSize()),
new TextFormat("BY2012 $M", leftCell),
new TextFormat("BY2012 $M", leftCell),
new TextFormat("TY $M", leftCell));
// add row to table
table.addRow(row);
// add table to page
this.addComponent(table);
}

Hi Demetri,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for Java 9.5.0 where I have used following code snippet based over new Document Object Model (DOM) of com.aspose.pdf namespace and I am afraid I am unable to notice any issue. The table is properly being aligned according to value passed in setLeft(…) method.

For your reference, I have also attached the resultant file generated over my end. Please try using the new DOM and in case you still face the same issue, please share the complete code snippet. We are sorry for this inconvenience.

[Java]

com.aspose.pdf.Document
doc =
new
com.aspose.pdf.Document();<o:p></o:p>

doc.getPages().add();

//add table

com.aspose.pdf.Table table = new com.aspose.pdf.Table();

table.setColumnWidths("192 68 95");

table.getMargin().setTop(10);

table.setLeft(100f);

// draw border lines

table.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, com.aspose.pdf.Color.getBlack()));

// cell format

com.aspose.pdf.TextFragment leftCell = new com.aspose.pdf.TextFragment("sample string");

leftCell.setHorizontalAlignment(com.aspose.pdf.TextAlignment.Center);

leftCell.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

//leftCell.get setBackgroundColor(com.aspose.pdf.Color.getBeige());

leftCell.getMargin().setTop(2);

leftCell.getMargin().setBottom(2);

leftCell.getMargin().setLeft(2);

leftCell.getMargin().setRight(2);

// create row

com.aspose.pdf.Row row = new com.aspose.pdf.Row();

row.setBackgroundColor(com.aspose.pdf.Color.getPink());

// write

//row.setCells(new Cell(),"text",new TextFormat("BY2012 $M", leftCell),new TextFormat("BY2012 $M", leftCell),new TextFormat("TY $M", leftCell));

com.aspose.pdf.Cell cell = new com.aspose.pdf.Cell();

cell.getParagraphs().add(leftCell);

row.getCells().add(cell);

//row.getCells().get_Item(1).getParagraphs().add(leftCell);

// add row to table

table.getRows().add(row);

// add table to page

doc.getPages().get_Item(1).getParagraphs().add(table);

doc.save("c:/pdftest/TableAlignment.pdf");