Package com.lowagie.text

Examples of com.lowagie.text.LwgDocument


      // we retrieve the total number of pages
      int n = reader.getNumberOfPages();
      System.out.println("There are " + n + " pages in the original file.");

      // step 1: creation of a document-object
      LwgDocument document = new LwgDocument(LwgPageSize.A4);
      // 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();
      PdfContentByte cb = writer.getDirectContent();
      PdfImportedPage page;
      int rotation;
      int i = 0;
      int p = 0;
      // step 4: we add content
      while (i < n) {
        i++;
        LwgRectangle rect = reader.getPageSizeWithRotation(i);
        float factorx = (x2 - x1) / rect.getWidth();
        float factory = (y1[p] - y2[p]) / rect.getHeight();
        float factor = (factorx < factory ? factorx : factory);
        float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.getWidth() * factor) / 2f);
        float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.getHeight() * factor) / 2f);
        page = writer.getImportedPage(reader, i);
        rotation = reader.getPageRotation(i);
        if (rotation == 90 || rotation == 270) {
          cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.getHeight() * factor);
        }
        else {
          cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy);
        }
        cb.setRGBColorStroke(0xC0, 0xC0, 0xC0);
        cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f);
        for (float l = y1[p] - 19; l > y2[p]; l -= 16) {
          cb.moveTo(x3, l);
          cb.lineTo(x4, l);
        }
        cb.rectangle(x1 + dx, y2[p] + dy, rect.getWidth() * factor, rect.getHeight() * factor);
        cb.stroke();
        System.out.println("Processed page " + i);
        p++;
        if (p == pages) {
          p = 0;
          document.newPage();
        }
      }
      // step 5: we close the document
      document.close();
    }
    catch(Exception e) {
          JOptionPane.showMessageDialog(internalFrame,
                e.getMessage(),
                e.getClass().getName(),
View Full Code Here


      PdfReader reader = new PdfReader(src.getAbsolutePath());
      System.out.println("The original file had " + reader.getNumberOfPages() + " pages.");
      reader.selectPages(selection);
      int pages = reader.getNumberOfPages();
      System.err.println("The new file has " + pages + " pages.");
      LwgDocument document = new LwgDocument(reader.getPageSizeWithRotation(1));
      PdfCopy copy = new PdfCopy(document, new FileOutputStream(dest.getAbsolutePath()));
      document.open();
            PdfImportedPage page;
            for (int i = 0; i < pages; ) {
                ++i;
                System.out.println("Processed page " + i);
                page = copy.getImportedPage(reader, i);
                copy.addPage(page);
            }
            PRAcroForm form = reader.getAcroForm();
      if (form != null)
                copy.copyAcroForm(reader);
      document.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

            if (getValue("destfile") == null) {
                throw new InstantiationException(
                        "You need to choose a destination file");
            }
            File pdf_file = (File) getValue("destfile");
            LwgDocument document = new LwgDocument();
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream(pdf_file));
            writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
            PdfPageLabels pageLabels = new PdfPageLabels();
            int dpiX, dpiY;
            float imgWidthPica, imgHeightPica;
            TreeSet<File> images = new TreeSet<File>();
            File[] files = directory.listFiles();
            if (files == null) {
                throw new NullPointerException("listFiles() returns null");
            }
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    images.add(files[i]);
                }
            }
            String label;
            for (File image: images) {
                System.out.println("Testing image: " + image.getName());
                try {
                    LwgImage img = LwgImage.getInstance(image.getAbsolutePath());
                    String caption = "";
                    dpiX = img.getDpiX();
                    if (dpiX == 0) {
                        dpiX = 72;
                    }
                    dpiY = img.getDpiY();
                    if (dpiY == 0) {
                        dpiY = 72;
                    }
                    imgWidthPica = (72 * img.getPlainWidth()) / dpiX;
                    imgHeightPica = (72 * img.getPlainHeight()) / dpiY;
                    img.scaleAbsolute(imgWidthPica, imgHeightPica);
                    document.setPageSize(new LwgRectangle(imgWidthPica,
                            imgHeightPica));
                    if (document.isOpen()) {
                        document.newPage();
                    } else {
                        document.open();
                    }
                    img.setAbsolutePosition(0, 0);
                    document.add(img);

                    BaseFont bf = BaseFont.createFont("Helvetica",
                            BaseFont.WINANSI,
                            false);
                    PdfGState gs1 = new PdfGState();
                    gs1.setBlendMode(PdfGState.BM_OVERLAY);
                    PdfContentByte cb = writer.getDirectContent();
                    cb.saveState();
                    cb.setGState(gs1);
                    cb.beginText();
                    cb.setFontAndSize(bf, 40);
                    cb.setTextMatrix(50, 50);
                    cb.showText(caption);
                    cb.endText();
                    cb.restoreState();

                    label = image.getName();
                    if (label.lastIndexOf('.') > 0) {
                        label = label.substring(0, label.lastIndexOf('.'));
                    }
                    pageLabels.addPageLabel(writer.getPageNumber(),
                                            PdfPageLabels.EMPTY, label);
                    System.out.println("Added image: " + image.getName());
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
            }
            if (document.isOpen()) {
                writer.setPageLabels(pageLabels);
                document.close();
            } else {
                System.err.println("No images were found in directory " +
                                   directory.getAbsolutePath());
            }
        } catch (Exception e) {
View Full Code Here

            throw new InstantiationException("You need to choose a destination file");
        
         File pdf_file = (File) getValue("destfile");
         int pageOffset = 0;
         List<HashMap<String, Object>> master = new ArrayList<HashMap<String, Object>>();
         LwgDocument 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<HashMap<String, Object>> 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 LwgDocument(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

      if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
      File pdf_file = (File)getValue("destfile");
      RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath());
            int comps = TiffImage.getNumberOfPages(ra);
      boolean adjustSize = false;
      LwgDocument document = new LwgDocument(LwgPageSize.A4);
            float width = LwgPageSize.A4.getWidth() - 40;
            float height = LwgPageSize.A4.getHeight() - 120;
      if ("ORIGINAL".equals(getValue("pagesize"))) {
        LwgImage img = TiffImage.getTiffImage(ra, 1);
                if (img.getDpiX() > 0 && img.getDpiY() > 0) {
                    img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
                }
        document.setPageSize(new LwgRectangle(img.getScaledWidth(), img.getScaledHeight()));
        adjustSize = true;
      }
      else if ("LETTER".equals(getValue("pagesize"))) {
        document.setPageSize(LwgPageSize.LETTER);
                width = LwgPageSize.LETTER.getWidth() - 40;
                height = LwgPageSize.LETTER.getHeight() - 120;
      }
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
            for (int c = 0; c < comps; ++c) {
                LwgImage img = TiffImage.getTiffImage(ra, c + 1);
                if (img != null) {
                    if (img.getDpiX() > 0 && img.getDpiY() > 0) {
                        img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
                    }
                  if (adjustSize) {
              document.setPageSize(new LwgRectangle(img.getScaledWidth(),
                  img.getScaledHeight()));
                        document.newPage();
                    img.setAbsolutePosition(0, 0);
                  }
                  else {
                    if (img.getScaledWidth() > width || img.getScaledHeight() > height) {
                            if (img.getDpiX() > 0 && img.getDpiY() > 0) {
                                float adjx = width / img.getScaledWidth();
                                float adjy = height / img.getScaledHeight();
                                float adj = Math.min(adjx, adjy);
                                img.scalePercent(7200f / img.getDpiX() * adj, 7200f / img.getDpiY() * adj);
                            }
                            else
                                img.scaleToFit(width, height);
                    }
                    img.setAbsolutePosition(20, 20);
                        document.newPage();
                        document.add(new Paragraph(tiff_file + " - page " + (c + 1)));
                  }
                    cb.addImage(img);
                    System.out.println("Finished page " + (c + 1));
                }
            }
            ra.close();
            document.close();
    } catch (Exception e) {
          JOptionPane.showMessageDialog(internalFrame,
                e.getMessage(),
                e.getClass().getName(),
                JOptionPane.ERROR_MESSAGE);
View Full Code Here

   * @see com.lowagie.toolbox.AbstractTool#execute()
   */
  public void execute() {
    try {
            String line = null;
            LwgDocument document;
            LwgFont f;
            LwgRectangle pagesize = (LwgRectangle)getValue("pagesize");
            if ("LANDSCAPE".equals(getValue("orientation"))) {
                f = FontFactory.getFont(FontFactory.COURIER, 10);
                document = new LwgDocument(pagesize.rotate(), 36, 9, 36, 36);
            }
            else {
                f = FontFactory.getFont(FontFactory.COURIER, 11);
                document = new LwgDocument(pagesize, 72, 36, 36, 36);
            }
            BufferedReader in = new BufferedReader(new FileReader((File)getValue("srcfile")));
            PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile")));
            document.open();
            while ((line = in.readLine()) != null) {
                document.add(new Paragraph(12, line, f));
            }
            document.close();
    } catch (Exception e) {
          JOptionPane.showMessageDialog(internalFrame,
                e.getMessage(),
                e.getClass().getName(),
                JOptionPane.ERROR_MESSAGE);
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");
      LwgDocument document = new LwgDocument();
      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<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);
      if (list == null) {
        document.add(new Paragraph("This document has no bookmarks."));
      }
      else {
        for (HashMap<String, Object> c: list) {
          Chapter chapter = (Chapter)createBookmark(src.getName(), null, c);
          List<HashMap<String, Object>> kids = (List<HashMap<String, Object>>) c.get("Kids");
          if (kids != null) {
            for (HashMap<String, Object> m: kids) {
              addBookmark(src.getName(), chapter, m);
            }
          }
          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());
      LwgImage img = TiffImage.getTiffImage(odd, 1);
      LwgDocument document = new LwgDocument(new LwgRectangle(img.getScaledWidth(),
          img.getScaledHeight()));
      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 {
          LwgImage imgOdd = TiffImage.getTiffImage(odd, c + 1);
          LwgImage imgEven = TiffImage.getTiffImage(even, count - c);
          document.setPageSize(new LwgRectangle(imgOdd.getScaledWidth(),
              imgOdd.getScaledHeight()));
          document.newPage();
          imgOdd.setAbsolutePosition(0, 0);
          cb.addImage(imgOdd);
          document.setPageSize(new LwgRectangle(imgEven.getScaledWidth(),
              imgEven.getScaledHeight()));
          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
      LwgDocument document1 = new LwgDocument(reader.getPageSizeWithRotation(1));
      LwgDocument document2 = new LwgDocument(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).getHeight());
        }
        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).getHeight());
        }
        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

         int width,
         int height,
         String strPathAbsoluteFile)
   {
      // step 1
      LwgDocument document = new LwgDocument(new LwgRectangle(width, height));
      try
      {
         // step 2
         PdfWriter writer;
         writer = PdfWriter.getInstance(document, new FileOutputStream(strPathAbsoluteFile));
         // step 3
         document.open();
         // step 4
         PdfContentByte cb = writer.getDirectContent();

         PdfTemplate tp = cb.createTemplate(width, height);

         Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());


         Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);

         chart.draw(g2d, r2d);
         g2d.dispose();

         tp.sanityCheck();
         cb.addTemplate(tp, 0, 0);
         cb.sanityCheck();
      }
      catch (DocumentException de)
      {
         de.printStackTrace();
      }
      catch (FileNotFoundException e)
      {
         e.printStackTrace();
      }
      // step 5
      document.close();
   }
View Full Code Here

TOP

Related Classes of com.lowagie.text.LwgDocument

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.