Skip to content

DocxReportingJavaMainHTMLTextStyling

Angelo edited this page Jun 21, 2015 · 2 revisions

HTML Text Styling

To style text with HTML syntax, you need tell to XDocReport that you wish to use HTML syntax for the field. You can

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.

Simple sample

Design the docx

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 :

Use Velocity/Freemarker in HTML

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

Supported styles

Elements

**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 ``text`` or ``text`` 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 `

`Title1`

`
Since *[XDocReport 0.9.6](XDocReport096)
Hyperlink ``XDocReport`` Since *[XDocReport 0.9.6](XDocReport096)
Bullet list `
  • `item`
`
Since *[XDocReport 0.9.6](XDocReport096)
Numbered list `
  1. `item`
`
Since *[XDocReport 0.9.6](XDocReport096)
Line break `
`
Since *[XDocReport 0.9.8](XDocReport098)

Attributes

**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)
Clone this wiki locally