Package com.lowagie.text

Examples of com.lowagie.text.Document


    }

    public static void savePDF(File filename, JFreeChart chart, int width, int height) {
      try {
      // step 1
        Document document = new Document(new Rectangle(width, height));
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfContentByte canvas = writer.getDirectContent();
        Graphics2D g2 = canvas.createGraphics(width, height);
        Rectangle2D area = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, area);
        g2.dispose();
        // step 5
        document.close();
      } catch(DocumentException ex) {
        throw new WorkBenchError(ex);
      }
       catch(IOException ex) {
         throw new WorkBenchError(ex);
View Full Code Here


          if (!aFile.canWrite()) {
            if (!aFile.mkdirs()) {
              //TODO throw
            }
          }
        Document aDocument = new Document();
        PdfWriter.getInstance(aDocument, new FileOutputStream(theFileName));
        toPDF(aDocument, theSkipLeadingCols);
        } else {
          //TODO throw
        }
View Full Code Here

  public ByteArrayOutputStream toPDF() throws Exception {
    return toPDF(0);
  }

  public ByteArrayOutputStream toPDF(int theSkipLeadingCols) throws Exception {
    Document aDocument = new Document();
    ByteArrayOutputStream aBAOS = new ByteArrayOutputStream();
    PdfWriter.getInstance(aDocument, aBAOS);
    toPDF(aDocument, theSkipLeadingCols);

    return aBAOS;
View Full Code Here

      PdfReader reader = new PdfReader(src.getAbsolutePath());
      // we retrieve the total number of pages
      int n = reader.getNumberOfPages();
      int digits = 1 + (n / 10);
      System.out.println("There are " + n + " pages in the original file.");
      Document document;
      int pagenumber;
      String filename;
            for (int i = 0; i < n; i++) {
              pagenumber = i + 1;
              filename = String.valueOf(pagenumber);
              while (filename.length() < digits) filename = "0" + filename;
              filename = "_" + filename + ".pdf";
              // step 1: creation of a document-object
              document = new Document(reader.getPageSizeWithRotation(pagenumber));
        // step 2: we create a writer that listens to the document
              PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(directory, name + filename)));
              // step 3: we open the document
              document.open();
              PdfContentByte cb = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, pagenumber);
        int rotation = reader.getPageRotation(pagenumber);
        if (rotation == 90 || rotation == 270) {
          cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pagenumber).height());
        }
        else {
          cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
        // step 5: we close the document
        document.close();
      }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
View Full Code Here

      }
            File directory = src.getParentFile();
            String name = src.getName();
            name = name.substring(0, name.lastIndexOf('.'));
            File html = new File(directory, name + "_index.html");
      Document document = new Document();
      HtmlWriter.getInstance(document, new FileOutputStream(html));
      Object css = getValue("css");
      if (css != null) {
        document.add(new Header(HtmlTags.STYLESHEET, css.toString()));
      }
      Object title = reader.getInfo().get("Title");
      if (title == null)
        document.addTitle("Index for " + src.getName());
      else
        document.addKeywords("Index for '" + title + "'");
      Object keywords = reader.getInfo().get("Keywords");
      if (keywords != null)
        document.addKeywords((String)keywords);
      Object description = reader.getInfo().get("Subject");
      if (keywords != null)
        document.addSubject((String)description);
      document.open();
      Paragraph t;
      if (title == null)
        t = new Paragraph("Index for " + src.getName());
      else
        t = new Paragraph("Index for '" + title + "'");
      document.add(t);
      if (description != null) {
        Paragraph d = new Paragraph((String) description);
        document.add(d);
      }
      List list = SimpleBookmark.getBookmark(reader);
      if (list == null) {
        document.add(new Paragraph("This document has no bookmarks."));
      }
      else {
        HashMap c;
        for (Iterator i = list.iterator(); i.hasNext(); ) {
          c = (HashMap) i.next();
          Chapter chapter = (Chapter)createBookmark(src.getName(), null, c);
          ArrayList kids = (ArrayList) c.get("Kids");
          if (kids != null) {
            for (Iterator k = kids.iterator(); k.hasNext(); ) {
              addBookmark(src.getName(), chapter, (HashMap)k.next());
            }
          }
          document.add(chapter);
        }
      }
      document.close();
      Executable.launchBrowser(html.getAbsolutePath());
    }
    catch(Exception e) {
      e.printStackTrace();
          JOptionPane.showMessageDialog(internalFrame,
View Full Code Here

      if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
      File pdf_file = (File)getValue("destfile");
      RandomAccessFileOrArray odd = new RandomAccessFileOrArray(odd_file.getAbsolutePath());
      RandomAccessFileOrArray even = new RandomAccessFileOrArray(even_file.getAbsolutePath());
      Image img = TiffImage.getTiffImage(odd, 1);
      Document document = new Document(new Rectangle(img.scaledWidth(),
          img.scaledHeight()));
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream(pdf_file));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage
          .getNumberOfPages(even));
      for (int c = 0; c < count; ++c) {
        try {
          Image imgOdd = TiffImage.getTiffImage(odd, c + 1);
          Image imgEven = TiffImage.getTiffImage(even, count - c);
          document.setPageSize(new Rectangle(imgOdd.scaledWidth(),
              imgOdd.scaledHeight()));
          document.newPage();
          imgOdd.setAbsolutePosition(0, 0);
          cb.addImage(imgOdd);
          document.setPageSize(new Rectangle(imgEven.scaledWidth(),
              imgEven.scaledHeight()));
          document.newPage();
          imgEven.setAbsolutePosition(0, 0);
          cb.addImage(imgEven);

        } catch (Exception e) {
          System.out.println("Exception page " + (c + 1) + " "
              + e.getMessage());
        }
      }
      odd.close();
      even.close();
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
View Full Code Here

      if (pagenumber < 2 || pagenumber > n) {
        throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page.");
      }

      // step 1: creation of a document-object
      Document document1 = new Document(reader.getPageSizeWithRotation(1));
      Document document2 = new Document(reader.getPageSizeWithRotation(pagenumber));
      // step 2: we create a writer that listens to the document
      PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(file1));
      PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(file2));
      // step 3: we open the document
      document1.open();
      PdfContentByte cb1 = writer1.getDirectContent();
      document2.open();
      PdfContentByte cb2 = writer2.getDirectContent();
      PdfImportedPage page;
      int rotation;
      int i = 0;
      // step 4: we add content
      while (i < pagenumber - 1) {
        i++;
        document1.setPageSize(reader.getPageSizeWithRotation(i));
        document1.newPage();
        page = writer1.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height());
        }
        else {
          cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
      }
      while (i < n) {
        i++;
        document2.setPageSize(reader.getPageSizeWithRotation(i));
        document2.newPage();
        page = writer2.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height());
        }
        else {
          cb2.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
      }
      // step 5: we close the document
      document1.close();
      document2.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

      files[1] = ((File)getValue("srcfile2")).getAbsolutePath();
      if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
      File pdf_file = (File)getValue("destfile");
            int pageOffset = 0;
            ArrayList master = new ArrayList();
            Document document = null;
            PdfCopy  writer = null;
            for (int i = 0; i < 2; i++) {
              // we create a reader for a certain document
                PdfReader reader = new PdfReader(files[i]);
                reader.consolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.getNumberOfPages();
                List bookmarks = SimpleBookmark.getBookmark(reader);
                if (bookmarks != null) {
                    if (pageOffset != 0)
                        SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                    master.addAll(bookmarks);
                }
                pageOffset += n;
                System.out.println("There are " + n + " pages in " + files[i]);
                if (i == 0) {
                    // step 1: creation of a document-object
                    document = new Document(reader.getPageSizeWithRotation(1));
                    // step 2: we create a writer that listens to the document
                    writer = new PdfCopy(document, new FileOutputStream(pdf_file));
                    // step 3: we open the document
                    document.open();
                }
                // step 4: we add content
                PdfImportedPage page;
                for (int p = 0; p < n; ) {
                    ++p;
                    page = writer.getImportedPage(reader, p);
                    writer.addPage(page);
                    System.out.println("Processed page " + p);
                }
            }
            if (!master.isEmpty())
                writer.setOutlines(master);
            // step 5: we close the document
            document.close();
    }
    catch(Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

            }
            int n = (int)Math.pow(2, pow2);
            int r = (int)Math.pow(2, (int)pow2 / 2);
            int c = n / r;
      // step 1: creation of a document-object
      Document document = new Document(newSize, 0, 0, 0, 0);
      // step 2: we create a writer that listens to the document
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
      // step 3: we open the document
      document.open();
      // step 4: adding the content
      PdfContentByte cb = writer.getDirectContent();
      PdfImportedPage page;
      float offsetX, offsetY, factor;
      int p;
      for (int i = 0; i < total; i++) {
        if (i % n == 0) {
          document.newPage();
        }
        p = i + 1;
        offsetX = unitSize.width() * ((i % n) % c);
        offsetY = newSize.height() - (unitSize.height() * (((i % n) / c) + 1));
        currentSize = reader.getPageSize(p);
        factor = Math.min(unitSize.width() / currentSize.width(), unitSize.height() / currentSize.height());
        offsetX += (unitSize.width() - (currentSize.width() * factor)) / 2f;
        offsetY += (unitSize.height() - (currentSize.height() * factor)) / 2f;
        page = writer.getImportedPage(reader, p);
        cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
      }
      // step 5: we close the document
      document.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

    public void execute() {
        try {
            // step 1: creation of a document-object
            Rectangle pageSize = new Rectangle(780, 525);
            if (getValue("backgroundcolor") != null) pageSize.setBackgroundColor((Color)getValue("backgroundcolor"));
            Document document = new Document(pageSize);
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
          if (getValue("destfile") == null) throw new DocumentException("You must provide a destination file!");
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile")));

            // step 3: we open the document
            document.open();

            // step 4:
          PdfContentByte cb = writer.getDirectContent();
            if (getValue("title") != null) {
              cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
              cb.beginText();
              if (getValue("front") == null) {
                cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 595f, 262f, 0f);
              }
              if (getValue("side") == null) {
                cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 385f, 262f, 270f);
              }
              cb.endText();
            }
            cb.moveTo(370, 0);
            cb.lineTo(370, 525);
            cb.moveTo(410, 525);
            cb.lineTo(410, 0);
            cb.stroke();
            if (getValue("front") != null) {
              Image front = (Image)getValue("front");
              front.scaleToFit(370, 525);
              front.setAbsolutePosition(410f + (370f - front.scaledWidth()) / 2f, (525f - front.scaledHeight()) / 2f);
              document.add(front);
            }
            if (getValue("back") != null) {
              Image back = (Image)getValue("back");
              back.scaleToFit(370, 525);
              back.setAbsolutePosition((370f - back.scaledWidth()) / 2f, (525f - back.scaledHeight()) / 2f);
              document.add(back);
            }
            if (getValue("side") != null) {
              Image side = (Image)getValue("side");
              side.scaleToFit(40, 525);
              side.setAbsolutePosition(370 + (40f - side.scaledWidth()) / 2f, (525f - side.scaledHeight()) / 2f);
              document.add(side);
            }

            // step 5: we close the document
            document.close();
        }
        catch(Exception e) {
          JOptionPane.showMessageDialog(internalFrame,
                e.getMessage(),
                e.getClass().getName(),
View Full Code Here

TOP

Related Classes of com.lowagie.text.Document

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.