Hey all,
look at this code and the generated Word and PDF. The PDF table overflows the right margin of the page regardless of what is set for table.setAllowAutoFit or table.autoFit. What needs to be done to obtain a PDF output
similar to the Word one?
@Test
public void testCellWidth() throws Exception
{
DocumentBuilder docBuilder = new DocumentBuilder();
com.aspose.words.Style style = docBuilder.getDocument().getStyles().get("Normal");
if (style != null)
{
docBuilder.getParagraphFormat().setStyle(style);
}
docBuilder.clearRunAttrs();
// begin table
Table table = docBuilder.startTable();
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.getCellFormat().clearFormatting();
docBuilder.getRowFormat().clearFormatting();
// The first row
docBuilder.getRowFormat().setHeight(0);
// The first cell
docBuilder.insertCell();
// setting or not setting the AutoFitBehavior doesn’t seem to make any difference
// table.autoFit( AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
// table.autoFit( AutoFitBehavior.AUTO_FIT_TO_WINDOW);
// setting or not setting the setAllowAutoFit doesn’t seem to make any difference on PDF.
// setting to FALSE results in a Word document expanding past the border which is expected
table.setAllowAutoFit( true);
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getCellFormat().setWidth( 100);
docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints( 100));
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.write("Lorem ipsum dolor sit amet");
// The second cell
docBuilder.insertCell();
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getCellFormat().setWidth( 300);
docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints( 300));
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.write("Lorem ipsum dolor sit amet");
// The third cell
docBuilder.insertCell();
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.AUTO);
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.write("Lorem ipsum dolor sit amet");
// The fourth cell
docBuilder.insertCell();
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.AUTO);
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.write("Lorem ipsum dolor sit amet");
// End the first row
docBuilder.endRow();
// end table
docBuilder.endTable();
docBuilder.getDocument().save("c:\testtable1.doc");
docBuilder.getDocument().save("c:\testtable1.pdf");
}
Regards,
Dragos