General

Components

Community

Development

TDF

Demos > Navigation API in Hotel Receipt

Overview

This demo is a simple template application about hotel receipt. By loading the configuration file "consume-data.properties" and navigating the hotel receipt template, this demo could generate the ODF documents (ODT, ODP, and ODS format).

In the generated ODF documents, you can see the concrete information about hotel receipt, such as hotel name, customer name, consume time, consume data, total expense, headcount, and consume item details including the price, quantity, total expense of each item. Taking the ODT document as an example, the ODT document template is shown in the following picture. Please notice the area surrounding by eight small green square, it is a chart. Because this chart is generated by the column ConsumeItem and the column Total Expense($) in the above table and the values of the column Total Expense($) are not numeric type, the template chart might not display correctly in your ODF editor, but it will display correctly after the template is expanded.

And the generated ODT document is shown in the following picture.

Use Simple API to complete this simple template application. This demo mainly uses the API of package navigation.

In the root directory of this demo project, there are three defined ODF template documents, "Navigation-ODT-Templating.odt", "Navigation-ODP-Templating.odp" and "Navigation-ODS-Templating.ods", and one configuration file, "consume-data.properties". Run NavigationForODF.java, and then in the root directory of this demo project you can find three generated ODF documents, "Navigation-ODT-Generated.odt", "Navigation-ODP-Generated.odp" and "Navigation-ODS-Generated.ods", which contain the information of "consume-data.properties". In these ODF documents, the consume item details are shown as table and chart.

Please notice the differences of ODT, ODP, ODS template document. In ODT and ODS template documents, the chart could be automatically generated by the ODF editor according to the data of table, and in this demo the data of chart comes from the column of ConsumeItem and Total Expense($). But in ODP template documents, the chart can not be automatically generated by the table, in fact, the chart in ODP template document is created manually through the function "insert chart" of openoffice, and the type of second column must be numeric. In addition, please notice the cell value of ODS document, the cell value has character type and numeric type, for example, ‘5,000 represents character type and 5,000 represents numeric type, and we could see that the different point is the single quotation marks before 5,000.

Code Introduction

First, load the key-value in the file "consume-data.properties" into the instance of Properties, and put ConsumeItem as key and TotalExpenseOfItem as value to the instance of HashMap for navigating ODP document template.

public void loadConsumeData(String filepath) throws Exception {
	InputStream is = new FileInputStream(filepath);
	properties.load(is);
	is.close();
	// put ConsumeItem as key and TotalExpenseOfItem as value to map
	Enumeration<String> enu = (Enumeration<String>) properties.propertyNames();
	while (enu.hasMoreElements()) {
		String key = (String) enu.nextElement();
		if (key.contains("ConsumeItem")) {
			String consumeItem = properties.getProperty(key);
			String totalExpenseOfItem = properties.getProperty("TotalExpenseOfItem"
										+ key.charAt(key.length() - 1));
			map.put(consumeItem, totalExpenseOfItem);
		}
	}
}

Then, introduce how to navigate the template documents and generate the ODF documents.

Navigate ODT document template

Load "Navigation-ODT-Templating.odt" and get the instance of TextDocument. Then traverse the instance of Properties, use the API of TextNavigation and TextSelection to complete the replacement of template document as the line 10-14. Please notice the line 17 and 18, make sure that the chart is shown rightly when open the generated document, so is navigating ODP and ODS document template. At last, generate "Navigation-ODT-Generated.odt" as the line 19.

public void navigateODT() throws Exception {
	Iterator it = properties.entrySet().iterator();
	System.out.println("Navigate ODT document: Navigation-ODT-Templating.odt");
	TextDocument textdoc = (TextDocument) TextDocument.loadDocument("Navigation-ODT-Templating.odt");
	TextNavigation search;
	while (it.hasNext()) {
		Map.Entry entry = (Map.Entry) it.next();
		String key = (String) entry.getKey();
		String value = (String) entry.getValue();
		search = new TextNavigation(key, textdoc);
		while (search.hasNext()) {
			TextSelection item = (TextSelection) search.nextSelection();
			item.replaceWith(value);
		}
	}
	// remove ObjectReplacements/ and Thumbnails/
	textdoc.getPackage().removePackageDocument("ObjectReplacements/");
	textdoc.getPackage().removePackageDocument("Thumbnails/");
	textdoc.save("Navigation-ODT-Generated.odt");
	System.out.println("...\nNavigation is over, and Navigation-ODT-Generated.odt is generated");
}

Navigate ODP document template

Unlike the ODT document, the chart of ODP document is an embedded document. Before navigate the chart, get the embedded document as the line 2 and 3. The chart comes from a table, and we could change the chart by setting the cell value of the table as the line 13-15. The line 15 uses the Class DefaultCellValueAdapter to update the cell value and value type.

	// set the cell value in the table of embedderdocument
	List<Document> embeddedDocuments = pdoc.getEmbeddedDocuments();
	Document embeddedDocument = embeddedDocuments.get(0);
	Table table = embeddedDocument.getTableList().get(0);
	String consumeItem;
	String totalExpenseOfItem;
	int index = 0;
	Iterator<String> it = map.keySet().iterator();
	while (it.hasNext()) {
		index++;
		consumeItem = (String) it.next();
		totalExpenseOfItem = (String) map.get(consumeItem);
		table.getColumnByIndex(0).getCellByIndex(index).setDisplayText(consumeItem);
		table.getColumnByIndex(1).getCellByIndex(index).setDisplayText(totalExpenseOfItem, new DefaultCellValueAdapter());
	}

Navigate ODS document template

Unlike the ODT document, use the Class CellSelection to complete the replacement. Class CellSelection is the subclass of Class TextSelection. Use the method "advancedReplaceWith" to update the cell value and value type.

	search = new TextNavigation(key, ssdoc);
	while (search.hasNext()) {
		CellSelection item = (CellSelection) search.nextSelection();
		item.advancedReplaceWith(value, new DefaultCellValueAdapter());
	}

Download

Powered by Simple API version 0.3.5. You can download the code of this demo from here.

Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.