Moving to header when the focus is at the end of a row

Hi support,

Please check the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.startTable();
builder.insertCell();
builder.write("cel1");
builder.endRow();

// move to header
Node curNode = builder.getCurrentNode();
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("header");

if (curNode != null)
{
    builder.moveTo(curNode);
}
builder.insertCell();
builder.write("cel2");
builder.endRow();
builder.endTable();
doc.save("C:\Temp\docTableAndHeader.doc");

Note that the header text is written between the two rows.
How can I make this to work as expected ?

Regards,
Milan

Hi
Thanks for your request. Your code is not correct from my point of view. You started creating header but did not finish the table. I think your code should looks like the following:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// move to header
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("header");
// move to document start
builder.moveToDocumentStart();
// build a table
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.startTable();
builder.insertCell();
builder.write("cel1");
builder.endRow();
builder.insertCell();
builder.write("cel2");
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\docTableAndHeader.doc");

Best regards.

Hy Alexey,

My code was “not correct” intentionally. I wanted to be able to move to header from that point, because this is the natural flow in coding on my side. But I can remember to move to header when the table ends, or before starting the table, if this is not possible at the end of a row, when writing the table.

Regards,
Milan

Hi

Thank you for additional information. Yes, I think it is better to remember to move to header when the table ends, or before starting the table.
Another solution is using one more DocumentBuidler as shown in the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(2);
builder.startTable();
builder.insertCell();
builder.write("cel1");
builder.endRow();
// move to header
DocumentBuilder builder1 = new DocumentBuilder(doc);
builder1.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder1.write("header");
builder.insertCell();
builder.write("cel2");
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\docTableAndHeader.doc");

Best regards.