Skip to content

ODTReportingJavaMainHTMLTextStyling

Angelo edited this page Nov 23, 2015 · 3 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 ODTTextStylingWithFreemarker.java and ODTTextStylingWithVelocity.java

You can try it on the online demo :

  • click here and click on "Report" button to generate ODT report which contains "comments" field and is replaced which XHTML content.
  • click here which generates ODT report which contains "comments" field and is replaced which XHTML content and convert it to HTML.
  • click here which generates ODT report which contains "comments" field and is replaced which XHTML content and convert it to PDF.

Simple sample

Design the odt

Create a odt ODTTextStylingWithVelocity.odt with input field nammed "comment" like this :

Java code

Create a Java main class like this:

package fr.opensagres.xdocreport.samples.odtandvelocity;

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 ODTTextStylingWithVelocity {

	public static void main(String[](http://wiki.xdocreport.googlecode.com/git/screenshots/ODTTextStylingExplanation1.png)) args) {
		try {
			// 1) Load ODT file by filling Velocity template engine and cache
			// it to the registry
			InputStream in = ODTTextStylingWithVelocity.class
					.getResourceAsStream("ODTTextStylingWithVelocity.odt");
			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 ODT
			OutputStream out = new FileOutputStream(new File(
					"ODTTextStylingWithVelocity_Out.odt"));
			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 ODTTextStylingWithDirectiveWithFreemarker.java and ODTTextStylingWithDirectiveWithVelocity.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)
Paragraph `

`a paragraph`

`
Since *[XDocReport 0.9.3](XDocReport093)
Span ``text`` Since *[XDocReport 1.0.2](XDocReport102)
Heading `

`Title1`

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

Attributes

Style Syntax sample Supported
Page Break Before <p style="page-break-before:always">text</p> Since XDocReport 0.9.8
Page Break After <p style="page-break-after:always">text</p> Since XDocReport 0.9.8
Text Alignment `<p style="text-align:left center
Clone this wiki locally