-
Notifications
You must be signed in to change notification settings - Fork 374
DocxReportingJavaMainHTMLTextStyling
To style text with HTML syntax, you need tell to XDocReport that you wish to use HTML syntax for the field. You can
- test the HTML text styling at http://xdocreport.opensagres.cloudbees.net/processReport?reportId=DocxTextStylingWithFreemarker.docx&dispatch=load&converter=PDF_XWPF
- see demo at http://xdocreport.opensagres.cloudbees.net/textStyling.jsp?reportId=docx
To do that you must use FieldsMetadata like this :
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.addFieldAsTextStyling("comments", SyntaxKind.Html);
You can find samples DocxTextStylingWithFreemarker.java and DocxTextStylingWithVelocity.java
You can try it on the online demo :
- click here and click on "Report" button to generate docx report which contains "comments" field and is replaced which XHTML content.
- click here which generates Docx report which contains "comments" field and is replaced which XHTML content and convert it to HTML.
- click here which generates Docx report which contains "comments" field and is replaced which XHTML content and convert it to PDF.
Create a docx DocxTextStylingWithVelocity.docx with mergefield nammed "comment" like this :
[### Java code
Create a Java main class like this:
package fr.opensagres.xdocreport.samples.docxandvelocity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import fr.opensagres.xdocreport.core.document.SyntaxKind;
public class DocxTextStylingWithVelocity {
public static void main(String[](http://wiki.xdocreport.googlecode.com/git/screenshots/DocxTextStylingExplanation1.png)) args) {
try {
// 1) Load Docx file by filling Velocity template engine and cache
// it to the registry
InputStream in = DocxTextStylingWithVelocity.class
.getResourceAsStream("DocxTextStylingWithVelocity.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
in, TemplateEngineKind.Velocity);
// 2) Create fields metadata to manage text styling
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.addFieldAsTextStyling("comments",
SyntaxKind.Html);
// 3) Create context Java model
IContext context = report.createContext();
context.put("comments",
"<i>Text</i> coming from <b>Java context</b>.");
// 4) Generate report by merging Java model with the Docx
OutputStream out = new FileOutputStream(new File(
"DocxTextStylingWithVelocity_Out.docx"));
report.process(context, out);
} catch (IOException e) {
e.printStackTrace();
} catch (XDocReportException e) {
e.printStackTrace();
}
}
This class will generate this report :
Since *XDocReport095 XDocReport 0.9.5), you can use Velocity/Freemarker in the HTML content.
To do that you must use FieldsMetadata like this (with true parameter) :
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.addFieldAsTextStyling("comments", SyntaxKind.Html, true);
Imagine you have putted your context like this :
context.put("project", "XDocReport");
context.put("url", "http://code.google.com/p/xdocreport");
You can use $project and $url (for Velocity) and ${project} and ${url} (for Freemarker) in your html :
context.put("comments", "<a href=\"$url\">$project</a> ");
which in this case is the same thing than :
context.put("comments", "<a href=\"http://code.google.com/p/xdocreport\">XDocReport</a> ");
You can use any Velocity/Freemarker directives (ex:use #foreach/[in your HTML to generate for instance some ul/li list :
context.put("comments", "
<ul>
#set ($planets = ['Earth', 'Mars', 'Neptune'](#list)))
#foreach($p in $planets)
<li>$p</li>
#end
</ul>");
which in this case is the same thing than :
context.put("comments", "<ul><li>Earth</li><li>Mars</li><li>Neptune</li></ul>");
You can find samples DocxTextStylingWithDirectiveWithFreemarker.java and DocxTextStylingWithDirectiveWithVelocity.java
**Style** | **Syntax sample** | **Supported** |
Bold | ``text`` or ``text`` | Since *[XDocReport 0.9.3](XDocReport093) |
Italic | ``text`` or ``text`` | Since *[XDocReport 0.9.3](XDocReport093) |
Underline | ``text`` | Since *[XDocReport 1.0.0](XDocReport100) |
Strike | ` |
Since *[XDocReport 1.0.0](XDocReport100) |
Subscript | ``text``` | Since *[XDocReport 1.0.6](XDocReport106) |
Superscript | ``text``` | Since *[XDocReport 1.0.6](XDocReport106) |
Paragraph | ` `a paragraph` ` |
Since *[XDocReport 0.9.3](XDocReport093) |
Span | ``text`` | Since *[XDocReport 1.0.2](XDocReport102) |
Heading | `` | Since *[XDocReport 0.9.6](XDocReport096) |
Hyperlink | ``XDocReport`` | Since *[XDocReport 0.9.6](XDocReport096) |
Bullet list | `
|
Since *[XDocReport 0.9.6](XDocReport096) |
Numbered list | `
|
Since *[XDocReport 0.9.6](XDocReport096) |
Line break | ` ` |
Since *[XDocReport 0.9.8](XDocReport098) |
**Style** | **Syntax sample** | **Supported** |
Page Break Before | ` `text` ` |
Since *[XDocReport 0.9.8](XDocReport098) |
Page Break After | ` `text` ` |
Since *[XDocReport 0.9.8](XDocReport098) |
Text Alignment | ` `text` ` |
Since *[XDocReport 1.0.2](XDocReport102) |
- Overview
- Getting Started
- FAQ
- Which License Applies
- Download
- Developer's Guide
- User's Guide
- Contributor's Guide
- Acknowledgment
- Articles
- Releases