Kogie / xdocreport

Automatically exported from code.google.com/p/xdocreport
1 stars 0 forks source link

allow line breaks for docx #176

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use a String with a \n line break in the context.

What is the expected output? What do you see instead?
Expected output contains a line break in the word document, I see a space 
instead.

What version of the product are you using? On what operating system?
0.9.8 on Windows

Please provide any additional information below.
I do not (currently) need any formatting, but I would like to replace a merge 
field such as "$address" with a single Java String containing multiple address 
lines.

The correct XML for getting this done can be seen here:
http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.br
eak

Alternatively I could have used HTML formatting, but I found two issues with 
this:
- no formatting (font, size) is applied for lines 2 and higher, should use the 
formatting of the merge field
- special xml characters are not escaped when using html

As a workaround (attached as a zip) I implemented my own 
ITextStylingTransformer which uses escapes special XML characters of the 
content String and replaces newline characters with </w:t><w:br/><w:t 
xml:space="preserve">, using the knowledge that the content will be placed 
inside a <w:t> tag inside a <w:r> tag.

Using the more apparent documentHandler.handleLineBreak() does not work, it 
gives the same formatting issue mentioned above. (I suspect that it 
unnecessarily creates a new run (<w:r>) for the line break).

However this workaround is quite ugly because:
 - the code is really a hack
 - for each different type of template I have to manually register the transformer for each field in the template, this gives code that should not be necessary
 - I am using code that may be internal to xdocreport (I had to figure out how to inspect internal code and documentation on service registry to find out how to create an ITextStylingTransformerDiscovery to register my ITextStylingTransformer.)

Original issue reported on code.google.com by Henno.Ve...@gmail.com on 26 Oct 2012 at 12:48

Attachments:

GoogleCodeExporter commented 9 years ago
Another argument why this is useful: the address String can contain a variable 
number of lines (e.g. with or without country).

Original comment by Henno.Ve...@gmail.com on 26 Oct 2012 at 12:50

GoogleCodeExporter commented 9 years ago
Hi Henno,

At first, I have tried to replace '\n' with '<br :>' at 
http://code.google.com/p/xdocreport/source/browse/document/fr.opensagres.xdocrep
ort.document.docx/src/main/java/fr/opensagres/xdocreport/document/docx/discovery
/DocxTemplateEngineConfiguration.java but I have commented this code because 
sometimes <br /> was not setted on the well position in the document.xmll and 
docx crashes.

So I think we should to use text styling transformer to manage that.

> Alternatively I could have used HTML formatting, but I found two issues with 
this:
>- no formatting (font, size) is applied for lines 2 and higher, should use the 
>formatting of the merge field

It's really very difficult to manage that, because preprocesisng doesn't work 
with DOM but SAX (to improve performance) and it's hard to get information 
about styles of mergefield (I must study how we can do that).

- special xml characters are not escaped when using html
please try with 1.0.0 (not released), I though we have fixed this problem.

>As a workaround (attached as a zip) I implemented my own 
ITextStylingTransformer >which uses escapes special XML characters of the 
content String and replaces newline >characters with </w:t><w:br/><w:t 
xml:space="preserve">, using the knowledge that >the content will be placed 
inside a <w:t> tag inside a <w:r> tag.
Yes! It's a well explanation why we must create a text styling to manage that.

>Using the more apparent documentHandler.handleLineBreak() does not work, it 
gives >the same formatting issue mentioned above. (I suspect that it 
unnecessarily creates >a new run (<w:r>) for the line break).
Don't hesitate to create a patch if you think it's better to manage as you 
suggested.

I suggest you to modify the html text styling 1.0.0 code (and not create a new 
text styling transformer) and send you a new patch. I will integrate your code.

Many thank's for your contribution.

Regards Angelo

Original comment by angelo.z...@gmail.com on 26 Oct 2012 at 1:11

GoogleCodeExporter commented 9 years ago
Good to see that the xml escaping with HTML formatting is solved with 1.0.0.
Will 1.0.0 then also solve the styling problem for multiple lines? If so, I 
will switch to the HTML styling, otherwise I will keep the workaround in my own 
code because it currently does what I need.

I think ideally, the line breaks should work without the calling code having to 
do anything (register HTML styling for fields) to enable this. Currently the 
calling code has to know the names of all the fields. Even if HTML styling is 
still required, perhaps the calling code could become much easier if you can 
set a default field styling used for all fields.

I really understand if you don't put my workaround code in xcdocreport as it is 
now because it is indeed rather ugly to create a separate text style 
transformer to solve this and also the "</w:t><w:br/><w:t 
xml:space="preserve">" is not very clean.

Original comment by Henno.Ve...@gmail.com on 26 Oct 2012 at 1:54

GoogleCodeExporter commented 9 years ago
Hi Henno,

I have commited my work about \n and \t and now XDocReport manages natively 
those characters (no need to develop some text styling). You can see my commit 
at 
http://code.google.com/p/xdocreport/source/detail?r=27e737f26ade5c716e85d577047c
f012667eb35c to see the sources (you will see that's very easy, thanks for your 
code).

So your requirement will be available in the 1.0.0 (use maven to get it the 
1.0.0-SNAPSHOT version or build yourself from git the xdocreport to test it).

I have commited too samples with docx/odt and velocity/freemarker with \n and 
\t. You can see my commit at 
http://code.google.com/p/xdocreport/source/detail?r=de9a21993b145e0cab624c1e6df1
93afc9e6c780&repo=samples

I have created 3 issues too : 

1) "Manage '\n' and '\t' natively for odt and docx report." => 
http://code.google.com/p/xdocreport/issues/detail?id=181

This issue is finished.

2) "Set default text styling for fields" => 
http://code.google.com/p/xdocreport/issues/detail?id=177

3) "docx text styling reset the stlyle of the line after a br" => 
http://code.google.com/p/xdocreport/issues/detail?id=178

I close this issue.

Regards Angelo

Original comment by angelo.z...@gmail.com on 28 Oct 2012 at 2:39

GoogleCodeExporter commented 9 years ago
Thank you very much! It works like a charm! (I did have to update my 
artifactory version so it downloads Maven snapshots from sonatype). I can now 
throw away my ugly workaround code.

Original comment by Henno.Ve...@gmail.com on 29 Oct 2012 at 11:51