-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCreateSimpleRTFDocumentTest.java
64 lines (55 loc) · 2.23 KB
/
CreateSimpleRTFDocumentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* CreateSimpleRTFDocumentTest.java
*/
package com.lowagie.text.rtf.document;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfFont;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Test creation of a simple RTF document.
*
* @author <A HREF="mailto:[email protected]">Chris</A>
*/
public class CreateSimpleRTFDocumentTest {
@Test
public void createDocument() throws FileNotFoundException {
final File OUTPUT_FILE = new File("target/CreateSimpleRTFDocumentTest.rtf");
// Delete output file if it already exists
if (OUTPUT_FILE.exists()) {
assertTrue ("Could NOT delete existing output file!", OUTPUT_FILE.delete());
}
// Create a document using A4 paper format
Document document = new Document(PageSize.A4);
RtfWriter2 rtf = RtfWriter2.getInstance(document, new FileOutputStream(OUTPUT_FILE));
document.open();
document.setMargins(50, 50, 50, 50);
// Add a paragraph with some text.
document.add(new Paragraph("Just a test to write RTF!", new RtfFont("SansSerif", 14, RtfFont.BOLD)));
// Add a table with 2 columns.
RtfFont headerFont = new RtfFont("SansSerif", 10, RtfFont.BOLD);
RtfFont tableFont = new RtfFont("SansSerif", 9);
int[] aWidths = {10,10};
Table table = new Table(2);
table.setWidth(80);
table.setWidths(aWidths);
table.addCell(new Phrase("Column", headerFont));
table.addCell(new Phrase("Another Column", headerFont));
table.addCell(new Phrase("Table data", tableFont));
table.addCell(new Phrase("Other data", tableFont));
table.addCell(new Phrase("Another row..", tableFont));
table.addCell(new Phrase("with data", tableFont));
document.add(table);
// Write document to OUTPUT_FILE.
rtf.close();
assertTrue ("RTF document was NOT created!", OUTPUT_FILE.exists());
}
}