Page 1 of 1

Table headers are not repeating on page break.

PostPosted: Thu Jun 27, 2013 8:34 am
by gchakilam
Hello Jason,

I am new to docx4j and I was trying to play with this and found something I was doing is not working.

I have a word template(attched:projecttemplate_input.docx) and I hand placed 'place holders' for replacing with dynamic values from database. In my java code, I am reading this template input file and loading WordprocessingMLPackage then I am substituting the place holders with dynamic data from backend. So far so good, replacing works great with help of examples I found in docx4j site. Here is what issues I am running into:

Issue #1) In my template I have a table with header(mulitiple column) and one template row with 'place holders' in it. As shown in exampls, I am adding dynamic data rows to the table and lastly remove the template row. But when table rows goes to second page, table headers are not repeating, data rows are printed right. Just not carrying the table headers when there is page break. To fix this, tried solution in ths link suggessted by Jason but still no luck.

Issue #2) I have table with one row and one column and again replacing the place holders from backend is going great. But issue is about data printing. This data is concatenated string and placing '\n'(new line) between each string and expecting to print this whole concatenated string in seperate lines. but it is not working. I am using System.getProperty("line.separator"); for new line. Anything wrong with this. What I need is new line between string tokens in a concatenated string.

Also I am attaching input and output files here. Problem is table with UTILITY,UTL OR UADATES" table header for issue#1 and table with "COMMENTS" is for issue#2.

Please find attached and reply me with suggession to fix this isue.

Thanks in advance,
out file after replacing the place holders and issue with table heades and new line.
(31.51 KiB) Downloaded 249 times

Input file with place holders to replace value dynamically from backend.
(25.87 KiB) Downloaded 637 times

Re: Table headers are not repeating on page break.

PostPosted: Thu Jun 27, 2013 11:57 pm
by jason
Looking at the table which has "UTILITY" in row 1, column 1 - is this the one that seems to have "header row" problems?

Actually this table is only 2 lines long. The row containing "Adams County" is a separate table, which would explain the problem.

For your new line issue, you'll need to explain what your code is doing better. In docx terms, new lines can translate to new paragraphs, or soft returns (shift enter).

Soft returns result in a <br/> for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                        <w:t>line 1</w:t>
                        <w:t>line 2</w:t>
Parsed in 0.001 seconds, using GeSHi

To create <br/>:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        R r = wmlObjectFactory.createR();
        Br br = wmlObjectFactory.createBr();
        r.getContent().add( br);
Parsed in 0.014 seconds, using GeSHi

Re: Table headers are not repeating on page break.

PostPosted: Wed Jul 10, 2013 1:47 am
by gchakilam
Thanks for the reply Jason. Sorry for the reply, I was out of office for a week.

Yes, We fixed the table(UTILITY) to have just one table with header row and data rows, but still headers are not repeating. See attached output file.

Code for the new line issue:
List<Object> texts = Doc4JUtils.getAllElementFromObject(wordMLPackage.getMainDocumentPart(), Text.class);
public static void replaceUSRComments(List<Object> texts, String replaceWith, String placeholder) {
for (Object text : texts) {
Text textElement = (Text) text;
if (textElement.getValue().equals(placeholder)) {
replaceWith=" comment1 \n comment2 \n comment3 \n comment4";

expected output is:

Actual output is: comment1 comment2 comment3 comment4

when I invoke replaceUSRComments method, text is replaced but "\n" is vanishing and output word is printed with out new line between comment strings.

Thanks and let me know if you need more info.