-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: added multipart document example
- Loading branch information
Showing
5 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import io.github.erdos.stencil.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
|
||
public static void main(String... args) throws IOException { | ||
|
||
// prepare template file | ||
PreparedTemplate template = API.prepare(findFile("template.docx")); | ||
|
||
// prepare header fragment | ||
PreparedFragment header = API.fragment(findFile("header.docx")); | ||
PreparedFragment footer = API.fragment(findFile("footer.docx")); | ||
|
||
Map<String, PreparedFragment> fragments = new HashMap<>(); | ||
fragments.put("header", header); | ||
fragments.put("footer", footer); | ||
|
||
// assemble template data | ||
Map<String, Object> dataMap = new HashMap<>(); | ||
dataMap.put("companyName", "ACME Company"); | ||
dataMap.put("companyAddress", "3786 Farm Meadow Drive, Nashville TN, 37214"); | ||
|
||
TemplateData data = TemplateData.fromMap(dataMap); | ||
|
||
// render template | ||
EvaluatedDocument result = API.render(template, fragments, data); | ||
|
||
// write generated document to a file | ||
File output = new File("multipart-output.docx"); | ||
result.writeToFile(output); | ||
} | ||
|
||
private static File findFile(String name) { | ||
return new File(Main.class.getResource(name).getFile()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Multipart document example | ||
|
||
In this example, the contents of the header and footer are stored in distinct template files. These templates | ||
may also refer dynamic content from the template data map. | ||
|
||
Using multipart templates are useful in many scenarios. For example, you can store addresses, company information | ||
or legal texts in separate documents. You can also reuse common elements across templates to make it easier to change | ||
them in the future. | ||
|
||
See the [Main.java](Main.java) file for usage. |
Binary file not shown.
Binary file not shown.
Binary file not shown.