Extra line when inserting Html in document builder

I am trying to insert html content into a cell Using aspose words for java, but the html contains <div> tags, and this tags (I suppose) adds extra blank spaces. I can’t remove the divs, because this is generated when I create an Html document from another word document. In this example you can see the extra blank space, so how can I remove it?

Example:

try {

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    Table table = builder.startTable();
    Cell cellContest = builder.insertCell();

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    builder.endRow();
    builder.getCellFormat().clearFormatting();
    builder.getCellFormat().setVerticalAlignment(VerticalAlignment.TOP);
    builder.getCellFormat().setLeftPadding(5);
    builder.getCellFormat().setTopPadding(1);
    builder.getCellFormat().setWrapText(true);
    Cell cell1 = builder.insertCell();

    Shape oval = new Shape(doc, ShapeType.ELLIPSE);
    oval.setWrapType(WrapType.SQUARE);
    oval.setAnchorLocked(true);
    oval.setWidth(20);

    oval.setHeight(10);

    oval.setStrokeColor(Color.RED);
    oval.setLeft(1);
    oval.setTop(0);

    builder.insertNode(oval);

    builder.insertHtml("<html><body><div><span style=\"font-family:Verdana; font-size:10pt\">#1. Daniel, Mora R.</span></div></body></html>");

    // builder.insertHtml("Sample 1");
    Cell cell2 = builder.insertCell();
    builder.insertHtml("Sample 2");
    Cell cell3 = builder.insertCell();
    builder.insertHtml("Sample 3");
    Cell cell4 = builder.insertCell();
    builder.insertHtml("Sample 4");
    builder.endRow();
    builder.insertCell();

    Shape oval2 = new Shape(doc, ShapeType.ELLIPSE);
    oval2.setWrapType(WrapType.SQUARE);
    oval2.setAnchorLocked(true);
    oval2.setWidth(20);

    oval2.setHeight(10);

    oval2.setStrokeColor(Color.RED);
    oval2.setLeft(1);
    oval2.setTop(0);

    builder.insertNode(oval2);

    // builder.getCellFormat().setLeftPadding(5 + oval.getWidth() + 1);
    // builder.insertHtml("<html><body><div><p style=\"margin:0pt\"><span style=\"font-family:Verdana; font-size:10pt\">#1. ABAD, Mark V.</span></p></div></body></html>");
    builder.insertHtml("<html><body><span style=\"font-family:Verdana; font-size:10pt\">#11. Daniel, Mora R.</span></body></html>");

    // builder.insertHtml("Sample 1");
    Cell cell12 = builder.insertCell();
    builder.insertHtml("Sample 12");
    Cell cell13 = builder.insertCell();
    builder.insertHtml("Sample 13");
    Cell cell14 = builder.insertCell();
    builder.insertHtml("Sample 14");
    builder.endRow();

    builder.endTable();
    doc.save("sample.docx");
    System.out.println("Finish!!!!!!");
} catch (Exception e) {

    e.printStackTrace();
}

Hi David,

Thanks for your inquiry. In your case, I suggest you please remove the empty paragraph after creating the table. Please check the following code snippet. Hope this helps you. Please let us know if you have any more queries.

.....
.....
.....

Cell cell12 = builder.insertCell();

builder.insertHtml("Sample 12");
Cell cell13 = builder.insertCell();
builder.insertHtml("Sample 13");
Cell cell14 = builder.insertCell();
builder.insertHtml("Sample 14");
builder.endRow();
builder.endTable();

if(cell1.getLastParagraph().toString(SaveFormat.TEXT).trim().equals(""))
{
       cell1.getLastParagraph().remove();
}

doc.save(MyDir + "sample.docx");
System.out.println("Finish!!!!!!");
1 Like

It works, good workaround,

Best regards,

Hi David,

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

1 Like

A post was split to a new topic: Empty/blank space issue