Hey,
see attached code and files. Unless there is something wrong with the code it seems that hyperlinks in cell tables break resizing algorithms when document is saved in PDF. It looks like it’s not the hyperlink label that is being used for calculating the cell width. You can see that the width of the cell containing a hyperlink ( testtable_hl) is much larger than the width of a cell containing plain text with the same value ( testtable_nohl).
Using AutoFitBehavior.AUTO_FIT_TO_WINDOW with this code ( uncomment that line) results in an even stranger behavior.
@SuppressWarnings( "nls")
private void testCellWidth_Impl( boolean withHyperlink, String path) throws Exception
{
DocumentBuilder docBuilder = new DocumentBuilder();
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();
// uncommenting this line results in an even stranger behavior
// table.autoFit( AutoFitBehavior.AUTO_FIT_TO_WINDOW);
table.setAllowAutoFit( true);
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.AUTO);
docBuilder.getParagraphFormat().clearFormatting();
if ( withHyperlink)
{
Style style = docBuilder.getDocument().getStyles().get("Hyperlink"); //$NON-NLS-1$
Style oldStyle = docBuilder.getFont().getStyle();
if (style != null)
{
docBuilder.getFont().setStyle(style);
}
docBuilder.insertHyperlink( "text", "<http://google.ro>", false);
if ( oldStyle != null)
{
docBuilder.getFont().setStyle(oldStyle);
}
}
else
{
docBuilder.write("text");
}
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(path + ".doc");
docBuilder.getDocument().save(path + ".pdf");
}
@SuppressWarnings( "nls")
@Test
public void testCellWidth_NoHyperlink() throws Exception
{
testCellWidth_Impl( false, "c:\testtable_nohl");
}
@SuppressWarnings( "nls")
@Test
public void testCellWidth_WithHyperlink() throws Exception
{
testCellWidth_Impl( true, "c:\testtable_hl");
}
Regards,
Dragos