Ejemplo n.º 1
0
  /** Externalize files resolving imagedata references */
  private void externalizeFiles(Document doc, File out) throws IOException {

    // got any external image file references in document?
    File[] files = doc.getImages();
    if (files.length > 0) {

      // grab image directory
      File dir = new File(out.getParentFile(), out.getName() + ".images");
      if (!dir.mkdirs()) throw new IOException("cannot create directory " + dir);

      // copy all images so their local to the generated document
      if (dir.exists()) {
        for (int i = 0; i < files.length; i++) {
          File file = files[i];
          File copy = new File(dir, file.getName());
          FileChannel from = null, to = null;
          long count = -1;
          try {
            from = new FileInputStream(file).getChannel();
            count = from.size();
            to = new FileOutputStream(copy).getChannel();
            from.transferTo(0, count, to);
            doc.setImage(file, dir.getName() + "/" + copy.getName());
          } catch (Throwable t) {
            LOG.log(
                Level.WARNING,
                "Copying '" + file + "' to '" + copy + "' failed (size=" + count + ")",
                t);
          } finally {
            try {
              to.close();
            } catch (Throwable t) {
            }
            try {
              from.close();
            } catch (Throwable t) {
            }
          }
        }
      }

      // done
    }

    // done
  }
Ejemplo n.º 2
0
  /** Format a document */
  public void format(Document doc, OutputStream out) throws IOException {

    // close doc
    doc.close();

    // impl for out
    try {
      formatImpl(doc, out);
    } catch (Throwable t) {
      LOG.log(Level.WARNING, "unexpected expection formatting " + doc.getTitle(), t);
      if (t instanceof OutOfMemoryError) throw new IOException("out of memory");
      if (t instanceof IOException) throw (IOException) t;
      throw new IOException(t.getMessage());
    } finally {
      try {
        out.close();
      } catch (Throwable t) {
      }
    }

    // done
  }
Ejemplo n.º 3
0
  /** our format logic */
  protected void formatImpl(Document doc, OutputStream out) throws Throwable {

    // could do thsi with FOP TextRenderer but it doesn't do the trick for simple text that we want
    // to generate
    //    org.xml.sax.ContentHandler handler = new org.apache.fop.fo.FOTreeBuilder("text/plain", new
    // org.apache.fop.apps.FOUserAgent(), out);
    //    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    //    transformer.transform(doc.getDOMSource(), new SAXResult(handler));

    // grab xsl transformer
    Transformer transformer = getTemplates("./contrib/xslt/fo2txt.xsl").newTransformer();

    // do the transformation
    transformer.transform(doc.getDOMSource(), new StreamResult(out));

    // done
  }
Ejemplo n.º 4
0
  /** A test main */
  public static void main(String[] args) {

    try {

      Document doc = new Document("Testing FO");

      doc.addText("A paragraph");
      doc.nextParagraph("start-indent=10pt");
      doc.addText(
          "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. ");

      doc.nextParagraph("text-decoration=underline");
      doc.addText("this paragraph is underlined");

      doc.nextParagraph();
      doc.addText("this line contains ");
      doc.addText("underlined", "text-decoration=underline");
      doc.addText(" text");

      doc.startList();
      doc.nextListItem("genj:label=a)");
      doc.addText("A foo'd bullet");
      doc.nextListItem("genj:label=b)");
      doc.addText("A foo'd bullet");
      doc.nextListItem();
      doc.addText("A normal bullet");

      doc.addTOC();
      doc.startSection("Section 1");
      doc.addText("here comes a ")
          .addText("table", "font-weight=bold, color=rgb(255,0,0)")
          .addText(" for you:");
      doc.addImage(
          new File(
              "C:/Documents and Settings/Nils/My Documents/Java/Workspace/GenJ/gedcom/meiern.jpg"),
          "vertical-align=middle");
      doc.addImage(
          new File("C:/Documents and Settings/Nils/My Documents/My Pictures/usamap.gif"),
          "vertical-align=middle");

      //      doc.startTable("width=100%,border=0.5pt solid black,genj:csv=true");
      //      doc.addTableColumn("column-width=10%");
      //      doc.addTableColumn("column-width=10%");
      //      doc.addTableColumn("column-width=80%");
      //      doc.nextTableCell("color=red");
      //      doc.addText("AA");
      //      doc.nextTableCell();
      //      doc.addText("AB");
      //      doc.nextTableCell();
      //      //doc.addText("AC");
      //      doc.nextTableCell();
      //      doc.addText("BA"); // next row
      //      doc.nextTableCell("number-columns-spanned=2");
      //      doc.addText("BB+BC");
      //      doc.nextTableRow();
      //      doc.addText("CA");
      //      doc.nextTableCell();
      //      doc.addText("CB");
      //      doc.nextTableCell();
      //      doc.addText("CC");
      //      doc.endTable();
      //
      //      doc.startList();
      //      doc.nextListItem();
      //      doc.addText("Item 1");
      //      doc.addText(" with text talking about");
      //      doc.addIndexTerm("Animals", "Mammals");
      //      doc.addText(" elephants and ");
      //      doc.addIndexTerm("Animals", "Mammals", "Horse");
      //      doc.addText(" horses as well as ");
      //      doc.addIndexTerm("Animals", "Mammals", "Horse");
      //      doc.addText(" ponys and even ");
      //      doc.addIndexTerm("Animals", "Fish", "");
      //      doc.addText(" fish");
      //      doc.nextParagraph();
      //      doc.addText("and a newline");
      //      doc.nextListItem();
      //      doc.addText("Item 2");
      //      doc.startList();
      //      doc.addText("Item 2.1");
      //      doc.nextListItem();
      //      doc.addText("Item 2.2");
      //      doc.endList();
      //      doc.endList();
      //      doc.addText("Text");
      //
      doc.startSection("Section 2");
      doc.addText("Text and a page break");
      //      doc.nextPage();

      doc.addTOCEntry("Foo");

      doc.startSection("Section 3");
      doc.addText("Text");

      Format format;
      if (args.length > 0) format = Format.getFormat(args[0]);
      else format = new PDFFormat();

      File file = null;
      String ext = format.getFileExtension();
      if (ext != null) {
        file = new File("c:/temp/foo." + ext);
      }
      format.format(doc, file);

      if (file != null)
        Runtime.getRuntime()
            .exec(
                "c:/Program Files/Internet Explorer/iexplore.exe \""
                    + file.getAbsolutePath()
                    + "\"");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }