PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/IndepModExporter/test/unit/src/cz/cvut/hnatuluk/test/word/WordTest.java

https://github.com/fritsric/EPIM
Java | 287 lines | 201 code | 17 blank | 69 comment | 0 complexity | b6e3463ec5ce2f8e118206dc4b3a4bea MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package cz.cvut.hnatuluk.test.word;
  6. import cz.cvut.hnatuluk.factories.ExportFactory;
  7. import cz.cvut.hnatuluk.factories.WordFactory.WordDocument;
  8. import cz.cvut.hnatuluk.factories.WordFactory.WordOutputFactory;
  9. import java.io.File;
  10. import java.math.BigInteger;
  11. import org.docx4j.wml.Body;
  12. import org.docx4j.wml.Color;
  13. import org.docx4j.wml.HpsMeasure;
  14. import org.docx4j.wml.Jc;
  15. import org.docx4j.wml.JcEnumeration;
  16. import org.docx4j.wml.ObjectFactory;
  17. import org.docx4j.wml.P;
  18. import org.docx4j.wml.PPr;
  19. import org.docx4j.wml.PPrBase.PStyle;
  20. import org.docx4j.wml.R;
  21. import org.docx4j.wml.RFonts;
  22. import org.docx4j.wml.RPr;
  23. import org.docx4j.wml.Text;
  24. import org.docx4j.wml.U;
  25. import org.docx4j.wml.UnderlineEnumeration;
  26. import org.junit.Test;
  27. /**
  28. *
  29. * @author Lukáš Hnaťuk ČVUT FEL 2010
  30. */
  31. public class WordTest {
  32. @Test
  33. public void testCrateFactory() throws Exception {
  34. WordOutputFactory wof = ExportFactory.getWordFactory();
  35. }
  36. @Test
  37. public void testExtensions() throws Exception {
  38. WordOutputFactory wof = ExportFactory.getWordFactory();
  39. File f = wof.createOutputFile("testfile.docx");
  40. WordDocument wd = wof.createOutputWorkbook(f);
  41. WordDocument wd1 = wof.createOutWorkbook(f.getName());
  42. }
  43. @Test
  44. public void testHelloWord() throws Exception {
  45. WordOutputFactory wof = ExportFactory.getWordFactory();
  46. File f = wof.createOutputFile("testFiles/WordFactory/helloWorld.docx");
  47. WordDocument wd = wof.createOutputWorkbook(f);
  48. Body b = wd.getDocumentBody();
  49. ObjectFactory cf = wd.getCreateFactory();
  50. P para = cf.createP();
  51. //Nastaví styl
  52. PPr ppr = cf.createPPr();
  53. PStyle pStyle = cf.createPPrBasePStyle();
  54. pStyle.setVal("style0");
  55. //Nastaví run a přidá text
  56. R run = cf.createR();
  57. Text t = cf.createText();
  58. t.setValue("Hello world!!!");
  59. run.getRunContent().add(t);
  60. //Přidat styl do odstavce
  61. ppr.setPStyle(pStyle);
  62. //A pak to vše poskládat zpět
  63. para.getParagraphContent().add(ppr);
  64. para.getParagraphContent().add(run);
  65. b.getEGBlockLevelElts().add(para);
  66. wof.write(f, wd);
  67. }
  68. @Test
  69. public void testComplexWord() throws Exception {
  70. WordOutputFactory wof = ExportFactory.getWordFactory();
  71. File f = wof.createOutputFile("testFiles/WordFactory/complexFile.docx");
  72. WordDocument wd = wof.createOutputWorkbook(f);
  73. Body b = wd.getDocumentBody();
  74. ObjectFactory cf = wd.getCreateFactory();
  75. // Vlevo
  76. P para = cf.createP();
  77. //Nastaví styl
  78. PPr ppr = cf.createPPr();
  79. PStyle pStyle = cf.createPPrBasePStyle();
  80. pStyle.setVal("style0");
  81. //Nastaví run a přidá text
  82. R run = cf.createR();
  83. Text t = cf.createText();
  84. t.setValue("Left");
  85. RPr rpr = cf.createRPr();
  86. run.getRunContent().add(rpr);
  87. run.getRunContent().add(t);
  88. //Přidat styl do odstavce
  89. ppr.setPStyle(pStyle);
  90. //A pak to vše poskládat zpět
  91. para.getParagraphContent().add(ppr);
  92. para.getParagraphContent().add(run);
  93. b.getEGBlockLevelElts().add(para);
  94. //Na střed
  95. para = cf.createP();
  96. //Nastaví styl
  97. ppr = cf.createPPr();
  98. pStyle = cf.createPPrBasePStyle();
  99. pStyle.setVal("style0");
  100. Jc jc = cf.createJc();
  101. jc.setVal(JcEnumeration.CENTER);
  102. //Nastaví run a přidá text
  103. run = cf.createR();
  104. t = cf.createText();
  105. t.setValue("Center");
  106. rpr = cf.createRPr();
  107. run.getRunContent().add(rpr);
  108. run.getRunContent().add(t);
  109. //Přidat styl do odstavce
  110. ppr.setPStyle(pStyle);
  111. ppr.setJc(jc);
  112. //A pak to vše poskládat zpět
  113. para.getParagraphContent().add(ppr);
  114. para.getParagraphContent().add(run);
  115. b.getEGBlockLevelElts().add(para);
  116. //Na Doprava
  117. para = cf.createP();
  118. //Nastaví styl
  119. ppr = cf.createPPr();
  120. pStyle = cf.createPPrBasePStyle();
  121. pStyle.setVal("style0");
  122. jc = cf.createJc();
  123. jc.setVal(JcEnumeration.RIGHT);
  124. //Nastaví run a přidá text
  125. run = cf.createR();
  126. t = cf.createText();
  127. t.setValue("Right");
  128. rpr = cf.createRPr();
  129. run.getRunContent().add(rpr);
  130. run.getRunContent().add(t);
  131. //Přidat styl do odstavce
  132. ppr.setPStyle(pStyle);
  133. ppr.setJc(jc);
  134. //A pak to vše poskládat zpět
  135. para.getParagraphContent().add(ppr);
  136. para.getParagraphContent().add(run);
  137. b.getEGBlockLevelElts().add(para);
  138. //Bold
  139. para = cf.createP();
  140. //Nastaví styl
  141. ppr = cf.createPPr();
  142. pStyle = cf.createPPrBasePStyle();
  143. pStyle.setVal("style0");
  144. //Nastaví run a přidá text
  145. run = cf.createR();
  146. t = cf.createText();
  147. t.setValue("Bold");
  148. rpr = cf.createRPr();
  149. rpr.setB(cf.createBooleanDefaultTrue());
  150. rpr.setBCs(cf.createBooleanDefaultTrue());
  151. run.getRunContent().add(rpr);
  152. run.getRunContent().add(t);
  153. //Přidat styl do odstavce
  154. ppr.setPStyle(pStyle);
  155. //A pak to vše poskládat zpět
  156. para.getParagraphContent().add(ppr);
  157. para.getParagraphContent().add(run);
  158. b.getEGBlockLevelElts().add(para);
  159. //Italic
  160. para = cf.createP();
  161. //Nastaví styl
  162. ppr = cf.createPPr();
  163. pStyle = cf.createPPrBasePStyle();
  164. pStyle.setVal("style0");
  165. //Nastaví run a přidá text
  166. run = cf.createR();
  167. t = cf.createText();
  168. t.setValue("Italic");
  169. rpr = cf.createRPr();
  170. rpr.setI(cf.createBooleanDefaultTrue());
  171. rpr.setICs(cf.createBooleanDefaultTrue());
  172. run.getRunContent().add(rpr);
  173. run.getRunContent().add(t);
  174. //Přidat styl do odstavce
  175. ppr.setPStyle(pStyle);
  176. //A pak to vše poskládat zpět
  177. para.getParagraphContent().add(ppr);
  178. para.getParagraphContent().add(run);
  179. b.getEGBlockLevelElts().add(para);
  180. //Underline
  181. para = cf.createP();
  182. //Nastaví styl
  183. ppr = cf.createPPr();
  184. pStyle = cf.createPPrBasePStyle();
  185. pStyle.setVal("style0");
  186. //Nastaví run a přidá text
  187. run = cf.createR();
  188. t = cf.createText();
  189. t.setValue("Underline");
  190. rpr = cf.createRPr();
  191. U u = cf.createU();
  192. u.setVal(UnderlineEnumeration.SINGLE);
  193. rpr.setU(u);
  194. run.getRunContent().add(rpr);
  195. run.getRunContent().add(t);
  196. //Přidat styl do odstavce
  197. ppr.setPStyle(pStyle);
  198. //A pak to vše poskládat zpět
  199. para.getParagraphContent().add(ppr);
  200. para.getParagraphContent().add(run);
  201. b.getEGBlockLevelElts().add(para);
  202. //Red
  203. para = cf.createP();
  204. //Nastaví styl
  205. ppr = cf.createPPr();
  206. pStyle = cf.createPPrBasePStyle();
  207. pStyle.setVal("style0");
  208. //Nastaví run a přidá text
  209. run = cf.createR();
  210. t = cf.createText();
  211. t.setValue("Red");
  212. rpr = cf.createRPr();
  213. Color c =cf.createColor();
  214. c.setVal("FF0000");
  215. rpr.setColor(c);
  216. run.getRunContent().add(rpr);
  217. run.getRunContent().add(t);
  218. //Přidat styl do odstavce
  219. ppr.setPStyle(pStyle);
  220. //A pak to vše poskládat zpět
  221. para.getParagraphContent().add(ppr);
  222. para.getParagraphContent().add(run);
  223. b.getEGBlockLevelElts().add(para);
  224. //Red
  225. para = cf.createP();
  226. //Nastaví styl
  227. ppr = cf.createPPr();
  228. pStyle = cf.createPPrBasePStyle();
  229. pStyle.setVal("style0");
  230. //Nastaví run a přidá text
  231. run = cf.createR();
  232. t = cf.createText();
  233. t.setValue("Font");
  234. rpr = cf.createRPr();
  235. HpsMeasure hps = cf.createHpsMeasure();
  236. hps.setVal(new BigInteger("80"));
  237. RFonts rf = cf.createRFonts();
  238. rf.setAscii("Arial Black");
  239. rf.setHAnsi("Arial Black");
  240. rpr.setRFonts(rf);
  241. rpr.setSz(hps);
  242. rpr.setSzCs(hps);
  243. run.getRunContent().add(rpr);
  244. run.getRunContent().add(t);
  245. //Přidat styl do odstavce
  246. ppr.setPStyle(pStyle);
  247. //A pak to vše poskládat zpět
  248. para.getParagraphContent().add(ppr);
  249. para.getParagraphContent().add(run);
  250. b.getEGBlockLevelElts().add(para);
  251. wof.write(f, wd);
  252. }
  253. /*
  254. =====================================================================================================
  255. Object XML element docx4j class Factory method
  256. =====================================================================================================
  257. Document body w:body org.docx4j.wml.Body factory.createBody();
  258. Paragraph w:p org.docx4j.wml.P factory.createP();
  259. Paragraph props w:pPr org.docx4j.wml.PPr factory.createPPr();
  260. Run w:r org.docx4j.wml.R factory.createR();
  261. Run props w:rPr org.docx4j.wml.RPr factory.createRPr();
  262. Text w:t org.docx4j.wml.Text factory.createText();
  263. Table w:tbl org.docx4j.wml.Tbl factory.createTbl();
  264. Table row w:tr org.docx4j.wml.Tr factory.createTr();
  265. Table cell w:tc org.docx4j.wml.Tc factory.createTc();
  266. Drawing w:drawing org.docx4j.wml.Drawing factory.createDrawing();
  267. Page break w:br org.docx4j.wml.Br factory.createBr();
  268. Footnote or endnote ref ? org.docx4j.wml.CTFtnEdnRef factory.createCTFtnEdnRef()
  269. */
  270. }