fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. package org.docx4j.samples;
  8. import java.io.OutputStream;
  9. import org.docx4j.Docx4J;
  10. import org.docx4j.convert.out.FOSettings;
  11. import org.docx4j.fonts.IdentityPlusMapper;
  12. import org.docx4j.fonts.Mapper;
  13. import org.docx4j.fonts.PhysicalFont;
  14. import org.docx4j.fonts.PhysicalFonts;
  15. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
  16. public class ConvertOutPDF extends AbstractSample {
  17. /*
  18. * NOT WORKING?
  19. *
  20. * If you are getting:
  21. *
  22. * "fo:layout-master-set" must be declared before "fo:page-sequence"
  23. *
  24. * please check:
  25. *
  26. * 1. the jaxb-xslfo jar is on your classpath
  27. *
  28. * 2. that there is no stack trace earlier in the logs
  29. *
  30. * 3. your JVM has adequate memory, eg
  31. *
  32. * -Xmx1G -XX:MaxPermSize=128m
  33. *
  34. */
  35. // Config for non-command line use
  36. static {
  37. inputfilepath = null; // to generate a docx (and PDF output) containing font samples
  38. inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.docx";
  39. saveFO = true;
  40. }
  41. // For demo/debugging purposes, save the intermediate XSL FO
  42. // Don't do this in production!
  43. static boolean saveFO;
  44. public static void main(String[] args)
  45. throws Exception {
  46. try {
  47. getInputFilePath(args);
  48. }
  49. // Font regex (optional)
  50. // Set regex if you want to restrict to some defined subset of fonts
  51. // Here we have to do this before calling createContent,
  52. // since that discovers fonts
  53. String regex = null;
  54. // Windows:
  55. // String
  56. // regex=".*(calibri|camb|cour|arial|symb|times|Times|zapf).*";
  57. regex=".*(calibri|camb|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
  58. // Mac
  59. // String
  60. // regex=".*(Courier New|Arial|Times New Roman|Comic Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans Serif|MS Serif).*";
  61. PhysicalFonts.setRegex(regex);
  62. // Document loading (required)
  63. WordprocessingMLPackage wordMLPackage;
  64. if (inputfilepath==null) {
  65. // Create a docx
  66. System.out.println("No imput path passed, creating dummy document");
  67. wordMLPackage = WordprocessingMLPackage.createPackage();
  68. SampleDocument.createContent(wordMLPackage.getMainDocumentPart());
  69. } else {
  70. // Load .docx or Flat OPC .xml
  71. System.out.println("Loading file from " + inputfilepath);
  72. wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  73. }
  74. // Set up font mapper (optional)
  75. Mapper fontMapper = new IdentityPlusMapper();
  76. wordMLPackage.setFontMapper(fontMapper);
  77. // .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
  78. // eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
  79. // eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
  80. // to a font which does
  81. PhysicalFont font
  82. = PhysicalFonts.getPhysicalFonts().get("Arial Unicode MS");
  83. // make sure this is in your regex (if any)!!!
  84. if (font!=null) {
  85. fontMapper.getFontMappings().put("Times New Roman", font);
  86. }
  87. fontMapper.getFontMappings().put("Libian SC Regular", PhysicalFonts.getPhysicalFonts().get("SimSun"));
  88. // FO exporter setup (required)
  89. // .. the FOSettings object
  90. FOSettings foSettings = Docx4J.createFOSettings();
  91. if (saveFO) {
  92. foSettings.setFoDumpFile(new java.io.File(inputfilepath + ".fo"));
  93. }
  94. foSettings.setWmlPackage(wordMLPackage);
  95. // Document format:
  96. // The default implementation of the FORenderer that uses Apache Fop will output
  97. // a PDF document if nothing is passed via
  98. // foSettings.setApacheFopMime(apacheFopMime)
  99. // apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
  100. // FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
  101. //foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
  102. // exporter writes to an OutputStream.
  103. String outputfilepath;
  104. if (inputfilepath==null) {
  105. outputfilepath = System.getProperty("user.dir") + "/OUT_FontContent.pdf";
  106. } else {
  107. outputfilepath = inputfilepath + ".pdf";
  108. }
  109. OutputStream os = new java.io.FileOutputStream(outputfilepath);
  110. // Specify whether PDF export uses XSLT or not to create the FO
  111. // (XSLT takes longer, but is more complete).
  112. // Don't care what type of exporter you use
  113. Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
  114. // Prefer the exporter, that uses a xsl transformation
  115. // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
  116. // Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
  117. // .. faster, but not yet at feature parity
  118. // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
  119. System.out.println("Saved: " + outputfilepath);
  120. }
  121. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class, interface, or enum expected
package org.docx4j.samples;
^
1 error
stdout
Standard output is empty