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

Free Support Forum - aspose.com

No-width optional break and PDF export

I’ve got a table which has some large URLs. I need the URLs to wrap within their cells.

I’m accomplishing this by inserting “No-width optional break” characters inside the URL after every slash. This works great for DOCX export, but it doesn’t work for PDF export. If I open the DOCX in Word 2010 and export as a PDF, it looks great.

Any chance that Aspose Words will be able to export “No-width optional break” characters on PDF export?

Thanks!

Hi Ned,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.8.0) from here and let us know how it goes on your side.

Please call Document.UpdateTableLayout method before saving the Pdf file. This method updates widths of cells and tables in the document according to their preferred widths and content. Hope this helps you.

If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

I am using 14.8.0.

I tried Document.UpdateTableLayout, and it did not affect the output. No better, no worse. I’ve attached:

OptionalBreak.docx (which displays correctly)
OptionalBreak.pdf (which has a problem)
OptionalBreakExportedFromWord.pdf (which displays correctly)
The code which generated these documents is inlined below, and also attached as a zip file:

import com.aspose.words.DocumentBuilder;
import com.aspose.words.OoxmlSaveOptions;
import com.aspose.words.PdfSaveOptions;
import com.aspose.words.SaveOptions;

public class ReproduceOptionalBreakIssue {
    public static void main(String[] args) throws Exception {
        ReproduceOptionalBreakIssue repro = new ReproduceOptionalBreakIssue();
        repro.saveDocx("OptionalBreak.docx");
        repro.savePdf("OptionalBreak.pdf");
    }
    private DocumentBuilder doc = new DocumentBuilder();

    /** Creates a document which reproduces this issue. */*
    public ReproduceOptionalBreakIssue() throws Exception {
        // generate a giant URL, then insert breaks
        String giantUrl = generateGiantUrl();
        String giantUrlWithBreaks = makeBreakableOnSlash(giantUrl);
        // write these strings to a document, raw
        doc.setBold(true); doc.writeln("Giant URL:");
        doc.setBold(false); doc.writeln(giantUrl);
        doc.setBold(true); doc.writeln("Giant URL with breaks:");
        doc.setBold(false); doc.writeln(giantUrlWithBreaks);
        // write these strings into a table
        doc.writeln();
        doc.startTable();
        doc.insertCell(); doc.write("Giant URL:");
        doc.insertCell(); doc.write(giantUrl);
        doc.endRow();
        doc.endTable();
        doc.writeln();
        doc.startTable();
        doc.insertCell(); doc.write("Giant URL with breaks:");
        doc.insertCell(); doc.write(giantUrlWithBreaks);
        doc.endRow();
        doc.endTable();
    }
    /* Generates a giant URL. */*
    public static String generateGiantUrl() {
        StringBuilder giantUrlBuilder = new StringBuilder();
        giantUrlBuilder.append("<http://www.host.com/>");
        for (int i = 0; i < 50; ++i) {
            giantUrlBuilder.append("really/long/url/");
        }
        return giantUrlBuilder.toString();
    }
    /* Inserts a "no-width optional break" after each slash. */*
    public static String makeBreakableOnSlash(String path) {
        StringBuilder breakable = new StringBuilder(path.length() * 2);
        path.chars().forEach((ch) -> {
            if (ch == ‘/’) {
                breakable.append(NO_WIDTH_OPTIONAL_BREAK);
            }
            breakable.append((char) ch);
        });
        return breakable.toString();
    }
    /* The no-width optional break character. */*
    public static final char NO_WIDTH_OPTIONAL_BREAK = ‘\u200c’;
    public void savePdf(String path) throws Exception {
        PdfSaveOptions options = new PdfSaveOptions();
        save(path, options);
    }

    public void saveDocx(String path) throws Exception {
        OoxmlSaveOptions options = new OoxmlSaveOptions();
        save(path, options);
    }
    /* Saves to the given file with the given options. */
    public void save(String path, SaveOptions options) throws Exception {
        // this call doesn’t fix the problem, but it doesn’t make it worse either
        doc.getDocument().updateTableLayout();
        doc.getDocument().save(path, options);
    }
}

Hi Ned,

Thanks for sharing the detail.

I
have tested the scenario and have managed to reproduce the same issue
at my side. For the sake of correction, I have logged this problem in
our issue tracking system as WORDSNET-10874. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Ned,

Thanks for patience. The Document.CompatibilityOptions.GrowAutoFit property is enabled by default and causes tables to extend past the right margin. Once this option is disabled, the tables fit in the page and long string are wrapped automatically to fit in the cells.

No need to use the “No-width optional break” character to make long lines to wrap correctly. Here is the example how you can insert long URLs into a table cells. Please let us know if you have any more queries.

StringBuilder giantUrlBuilder = new StringBuilder();
giantUrlBuilder.append("http://www.host.com/");
for (int i = 0; i < 50; ++i)
{
    giantUrlBuilder.append("really/long/url/");
}
Document doc = new Document();
// Turn off the GrowAutoFit compatibility option.
doc.getCompatibilityOptions().setGrowAutofit(false);
// Alternatively, we can use Word2013 format, where GrowAutoFit is disabled.
// d.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2013);
DocumentBuilder b = new DocumentBuilder(d);
b.startTable();
b.insertCell();
b.write(giantUrlBuilder.toString());
b.endRow();
b.endTable();
doc.save(*MyDir * +"out.pdf");

Thanks! Worked great.

Hi Ned,

Thanks
for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.