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

Free Support Forum - aspose.com

Hyperlinks in tables break resizing algorithms for PDF output

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

Hi Dragos,

Thank you for reporting this problem to us, I managed to reproduce it on my side. Your request has been linked to the appropriate issue. We will let you know once it is resolved.

Also, I would like to bring your attention to the fact that you should call Table.autoFit method after you finish building the table. There is no sense to call this method before.

Best regards,

Hi Dragos,

Please note that calling doc.updateTableLayout() before rendering will also fix your issue in this case.

Thanks,

Hey Andrey,

thanks, this works to a certain extent. See the details I’ve posted here: https://forum.aspose.com/t/tables-are-rendered-differently-in-word-and-pdf/57480/5

Regards,
Dragos

The issues you have found earlier (filed as WORDSNET-5317) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as WORDSNET-5727) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(7)