Adding a new page at the start of the document

I am converting an html file to a Document and then am trying to add a cover page. I move to section 0, user builder to write a table with text and then insert a page break.
However the page break doesn’t take affect and the existing content (that was converted from the html) gets inserted inside the table after the text that was written as part of the cover page.

What am i missing?

Here’s the code which inserts the cover page.

public void createCustomCoverPageWithTitle(String title, String prepFor, String prepBy) throws Exception {

    builder.moveToSection(0);
    builder.getParagraphFormat().setSpaceBefore(132);
    builder.insertParagraph();
    builder.getParagraphFormat().setSpaceBeforeAuto(true);
    builder.startTable();
    builder.insertCell();
    builder.getFont().setBold(true);
    builder.getFont().setSize(16);
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.AT_LEAST);
    builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);
    builder.writeln(title);
    builder.endTable();
    Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);

    table.setAlignment(TableAlignment.CENTER);
    table.clearBorders();
    table.setBorder(BorderType.LEFT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.RIGHT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.TOP, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.BOTTOM, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setAlignment(TableAlignment.CENTER);
    table.setPreferredWidth(PreferredWidth.fromPercent(80));
    table.getFirstRow().getFirstCell().getCellFormat().setVerticalAlignment(VerticalAlignment.CENTER);
    table.getFirstRow().getFirstCell().getCellFormat().setTopPadding(5);

    builder.getFont().setName("Times New Roman");
    builder.getFont().setSize(12);
    builder.getFont().setBold(false);
    builder.getParagraphFormat().setSpaceBefore(54);
    builder.insertParagraph();
    if (prepFor != null && !prepFor.isEmpty()) {
        builder.getParagraphFormat().setSpaceBefore(12);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        builder.writeln("Prepared For");
        builder.getParagraphFormat().setSpaceBefore(0);
        builder.getFont().setBold(false);
        builder.write(prepFor);
        builder.insertParagraph();
    }
    if (prepBy != null && !prepBy.isEmpty()) {
        builder.getParagraphFormat().setSpaceBefore(12);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        builder.writeln("Prepared By");
        builder.getParagraphFormat().setSpaceBefore(0);
        builder.getFont().setBold(false);
        builder.write(prepBy);
        builder.insertParagraph();
    }
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

    builder.insertBreak(BreakType.PAGE_BREAK);
}

Hi Daniel,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Html document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please share the value of variables passed to createCustomCoverPageWithTitle.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

I’ve attached a zip that contains both the html file and the incorrect output word doc.

Variable values that were used were: “This is a title”, “Sample PrepFor”, “Sample PrepBy”

Hi Daniel,

Thanks for sharing the detail. Please move the cursor to the end of document using DocumentBuilder.MoveToDocumentEnd method if you want to insert the page break at the end of document.

If you want to insert the page break at the start of document and first node of document is Table, please insert an empty paragraph at the start of document and move the cursor to the start of document using DocumentBuilder.MoveToDocumentStart.

Insert the page break after moving the cursor to the start or end of document. Please check following modified code snippet. Hope this helps you.

public void createCustomCoverPageWithTitle(String title, String prepFor, String prepBy) throws Exception {
    Document doc = new Document(MyDir + "test.doc");
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.moveToSection(0);
    builder.getParagraphFormat().setSpaceBefore(132);
    builder.insertParagraph();
    builder.getParagraphFormat().setSpaceBeforeAuto(true);
    builder.startTable();
    builder.insertCell();
    builder.getFont().setBold(true);
    builder.getFont().setSize(16);
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.AT_LEAST);
    builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);
    builder.writeln(title);
    builder.endTable();
    Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
    table.setAlignment(TableAlignment.CENTER);
    table.clearBorders();
    table.setBorder(BorderType.LEFT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.RIGHT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.TOP, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setBorder(BorderType.BOTTOM, LineStyle.SINGLE, 2.5, Color.BLACK, true);
    table.setAlignment(TableAlignment.CENTER);
    table.setPreferredWidth(PreferredWidth.*fromPercent*(80));
    table.getFirstRow().getFirstCell().getCellFormat().setVerticalAlignment(VerticalAlignment.CENTER);
    table.getFirstRow().getFirstCell().getCellFormat().setTopPadding(5);
    builder.getFont().setName("Times New Roman");
    builder.getFont().setSize(12);
    builder.getFont().setBold(false);
    builder.getParagraphFormat().setSpaceBefore(54);
    builder.insertParagraph();
    if (prepFor != null && !prepFor.isEmpty()) {
        builder.getParagraphFormat().setSpaceBefore(12);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        builder.writeln("Prepared For");
        builder.getParagraphFormat().setSpaceBefore(0);
        builder.getFont().setBold(false);
        builder.write(prepFor);
        builder.insertParagraph();
    }
    if (prepBy != null && !prepBy.isEmpty()) {
        builder.getParagraphFormat().setSpaceBefore(12);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        builder.writeln("Prepared By");
        builder.getParagraphFormat().setSpaceBefore(0);
        builder.getFont().setBold(false);
        builder.write(prepBy);
        builder.insertParagraph();
    }
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
    // Move the cursor to the start of document if you want to insert the page break at the start of document.
    builder.moveToDocumentStart();
    // Insert empty paragraph at the start of document if first node of document is Table and you want to insert page break
    // at the start of document
    if(doc.getFirstSection().getBody().getFirstChild().getNodeType() == NodeType.TABLE)
    {
        doc.getFirstSection().getBody().insertBefore(new Paragraph(doc), doc.getFirstSection().getBody().getFirstChild());
    }
    // Move the cursor to the end of document if you want to insert the page break at the end of document.
    // builder.moveToDocumentEnd();
    builder.insertBreak(BreakType.PAGE_BREAK);
    doc.save(MyDir + "Out.docx");
}

This did not fix the problem.
The problem is that all the content that was imported via html is gettting inserted into the table itself. What I am trying to do is make a cover page which has a table in it. The content of the doc should start after the cover page which is the content imported from the html.

  1. create the doc using the html
  2. insert a new page at the start of the doc
  3. add a table to that first new page

a table is created, but the content of the html is inside the table as well

Hi Daniel,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Html and Word documents
  • Please attach the expected output Word file.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

I’ve attached a zip containing:

  • input html - content.html
  • standalone console application - Test.java
  • expected output word file - expected_output.doc
  • generated output word file - generated_output.doc
  • ouput when not adding a cover page - html_only.doc

Hi Daniel,

Thanks for sharing the detail. In your input html, the first node is table. You are moving the cursor to the start of document and inserting the contents. In this case, the contents will be inserted in the first cell of table. You need to insert paragraph break at the start of document and then write your desired contents. Please use following modified code example to get the desired output.

Document doc = new Document(MyDir + "content.html");
if (doc.getFirstSection().getBody().getFirstChild().getNodeType() == NodeType.TABLE)
{
    doc.getFirstSection().getBody().insertBefore(new Paragraph(doc), doc.getFirstSection().getBody().getFirstChild());
}
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.getParagraphFormat().setSpaceBefore(132);
builder.insertParagraph();
builder.getParagraphFormat().setSpaceBeforeAuto(true);
builder.startTable();
builder.insertCell();
builder.getFont().setBold(true);
builder.getFont().setSize(16);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.AT_LEAST);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);
builder.writeln("This is a title");
builder.endTable();
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.setAlignment(TableAlignment.CENTER);
table.clearBorders();
table.setBorder(BorderType.LEFT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
table.setBorder(BorderType.RIGHT, LineStyle.SINGLE, 2.5, Color.BLACK, true);
table.setBorder(BorderType.TOP, LineStyle.SINGLE, 2.5, Color.BLACK, true);
table.setBorder(BorderType.BOTTOM, LineStyle.SINGLE, 2.5, Color.BLACK, true);
table.setAlignment(TableAlignment.CENTER);
table.setPreferredWidth(PreferredWidth.* fromPercent * (80));
table.getFirstRow().getFirstCell().getCellFormat().setVerticalAlignment(VerticalAlignment.CENTER);
table.getFirstRow().getFirstCell().getCellFormat().setTopPadding(5);
builder.getFont().setName("Times New Roman");
builder.getFont().setSize(12);
builder.getFont().setBold(false);
builder.getParagraphFormat().setSpaceBefore(54);
builder.insertParagraph();
String prepFor = "Sample PrepFor";
String prepBy = "Sample PrepBy";
if (prepFor != null && !prepFor.isEmpty())
{
    builder.getParagraphFormat().setSpaceBefore(12);
    builder.getFont().setName("Arial");
    builder.getFont().setBold(true);
    builder.writeln("Prepared For");
    builder.getParagraphFormat().setSpaceBefore(0);
    builder.getFont().setBold(false);
    builder.write(prepFor);
    builder.insertParagraph();
}
if (prepBy != null && !prepBy.isEmpty())
{
    builder.getParagraphFormat().setSpaceBefore(12);
    builder.getFont().setName("Arial");
    builder.getFont().setBold(true);
    builder.writeln("Prepared By");
    builder.getParagraphFormat().setSpaceBefore(0);
    builder.getFont().setBold(false);
    builder.write(prepBy);
    builder.insertParagraph();
}
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertBreak(BreakType.PAGE_BREAK);
doc.save(MyDir + "Out.doc");

thanks. that worked.