Cell formatting issue when PDF table extends beyond one page

To start off, we are a licensed user. I am having an issue with the cell formatting of a pdf table when using padding on the cells and the table extends to more than one page. The first row of any page after the first one doesn’t display the cells correctly. Here is code to reproduce the issue. I’ve also attached a screen shot of the pdf. This issue occurs with the newest pdf version 4.5 jar.

Thanks,
Frank

public static void c() throws Exception
{
final Pdf pdf = new Pdf();
final Section section = pdf.getSections().add();
final Paragraphs paragraphs = section.getParagraphs();
section.getPageInfo().setMargin(new MarginInfo(50.0f));
final Table table = new Table();
final Rows rows = table.getRows();
table.setColumnWidths(“380 80”);
paragraphs.add(table);
final MarginInfo mi = new MarginInfo(3.0f);
for (int j=0; j<60; j++)
{
final Row row = rows.add();
final Cells cells = row.getCells();
Cell cell = cells.add(“Row->” + (j + 1) + “:This should be CENTERED for both alignment types”);
cell.getDefaultCellTextInfo().setFontName(“Arial”);
cell.isWordWrapped(true);
cell.setBackgroundColor(new aspose.pdf.Color("#CCCCCC"));
cell.setBorder(new BorderInfo(BorderSide.All, new aspose.pdf.Color("#000000")));
cell.setVerticalAlignment(VerticalAlignmentType.Center);
cell.setAlignment(AlignmentType.Center);
cell = cells.add(“Column 2”);
cell.setPadding(mi);
cell.getDefaultCellTextInfo().setFontName(“Arial”);
cell.isWordWrapped(true);
cell.setBackgroundColor(new aspose.pdf.Color("#CCCCCC"));
cell.setBorder(new BorderInfo(BorderSide.All, new aspose.pdf.Color("#000000")));
cell.setVerticalAlignment(VerticalAlignmentType.Center);
cell.setAlignment(AlignmentType.Center);
cell.setPadding(mi);
}

pdf.save(“test_font.pdf”);
}

Hi Frank,


Thanks for contacting support.

In order to resolve your problem, I would recommend you to please try using the latest Document Object Model (DOM) approach of Aspose.Pdf for Java. Please try using the following code snippet to resolve the formatting issues occurring when table extends to multiple pages.

[Java]

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

doc.getPages().add();

doc.getPages().get_Item(1).getPageInfo().setMargin(new com.aspose.pdf.MarginInfo(50.0f, 50.0f,50.0f, 50.0f));

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

final com.aspose.pdf.Rows rows = table.getRows();

table.setColumnWidths("380 80");

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

final com.aspose.pdf.MarginInfo mi = new com.aspose.pdf.MarginInfo(3.0f,3.0f,3.0f,3.0f);

for (int j=0; j<60; j++)

{

final com.aspose.pdf.Row row = rows.add();

final com.aspose.pdf.Cells cells = row.getCells();

com.aspose.pdf.Cell cell = cells.add("Row->" + (j + 1) + ":This should be CENTERED for both alignment types");

// cell.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont("Arial"));

cell.isWordWrapped(true);

cell.setBackgroundColor(com.aspose.pdf.Color.getLightGray());

cell.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

cell.setVerticalAlignment(VerticalAlignmentType.Center);

cell.setAlignment(AlignmentType.Center);

cell = cells.add("Column 2");

cell.setMargin(mi);

// cell.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont("Arial"));

cell.isWordWrapped(true);

cell.setBackgroundColor(com.aspose.pdf.Color.getLightGray());

cell.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

cell.setVerticalAlignment(VerticalAlignmentType.Center);

cell.setAlignment(AlignmentType.Center);

cell.setMargin(mi);

}

doc.save(“c:/pdftest/TableSpan_Issue_DOM.pdf”);


However I have observed some other issues when using this new approach. For the sake of correction, I have separately logged each problem in our issue tracking system.
  • PDFNEWJAVA-33937 - Top margin information for cell contents is not being honored.
  • PDFNEWJAVA-33938 - Unable to set font for table contents.
  • PDFNEWJAVA-33939 - Alignments are not being honored for cell contents.

We will further look into the details of these problems and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi Frank,


Thanks for your patience.

We have further investigated the issue PDFNEWJAVA-33937 and in order to generate the correct output (properly set the top margin for table cells), please try to change the following code lines with each other. For your reference, I have also attached the resultant PDF generated with latest release of Aspose.Pdf for Java 4.5.0.

[Java]
cell = cells.add(“Column 2”);
cell.setMargin(mi);

change to 

cell.setMargin(mi);<o:p></o:p>

cell = cells.add(“Column 2”);

Hi Frank,


In order to resolve PDFNEWJAVA-33939 Alignments are not being honored for cell contents problem, please set the Horizontal and vertical alignment for cell contents as specified below. For your reference, I have also attached the resultant PDF generated over my end.

[Java]

cell.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);<o:p></o:p>

cell.setAlignment(com.aspose.pdf.HorizontalAlignment.Center);

Hi Frank,


Thanks for your patience.

We have further investigated the issue PDFNEWJAVA-33938 reported earlier and in order resolve the problem, DefaultCellTextState needs to be created first. please try using the following code snippet. For your reference, the resultant PDF is also attached.

[Java]

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

doc.getPages().add();

doc.getPages().get_Item(1).getPageInfo().setMargin(new com.aspose.pdf.MarginInfo(50.0f, 50.0f,50.0f, 50.0f));

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

final com.aspose.pdf.Rows rows = table.getRows();

table.setColumnWidths("380 80");

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

final com.aspose.pdf.MarginInfo mi = new com.aspose.pdf.MarginInfo(3.0f,3.0f,3.0f,3.0f);

for (int j=0; j<60; j++)

{

final com.aspose.pdf.Row row = rows.add();

final com.aspose.pdf.Cells cells = row.getCells();

com.aspose.pdf.Cell cell = cells.add("Row->" + (j + 1) + ":This should be CENTERED for both alignment types");

cell.setDefaultCellTextState(new com.aspose.pdf.TextState());

cell.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont("Arial"));

cell.isWordWrapped(true);

cell.setBackgroundColor(com.aspose.pdf.Color.getLightGray());

cell.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

cell.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);

cell.setAlignment(com.aspose.pdf.HorizontalAlignment.Center);

cell = cells.add("Column 2");

cell.setMargin(mi);

cell.setDefaultCellTextState(new com.aspose.pdf.TextState());

cell.getDefaultCellTextState().setFont(com.aspose.pdf.FontRepository.findFont("Impact"));

cell.isWordWrapped(true);

cell.setBackgroundColor(com.aspose.pdf.Color.getLightGray());

cell.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All));

cell.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);

cell.setAlignment(com.aspose.pdf.HorizontalAlignment.Center);

cell.setMargin(mi);

}

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