Package com.lowagie.text.pdf

Examples of com.lowagie.text.pdf.PdfReader


      }
      byte[] ownerpassword = null;
      if (getValue("ownerpassword") != null) {
        ownerpassword = ((String)getValue("ownerpassword")).getBytes();
      }
      PdfReader reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath());
      PdfEncryptor.encrypt(
            reader,
        new FileOutputStream((File)getValue("destfile")),
        userpassword,
        ownerpassword,
View Full Code Here


          if (getValue("destfile2") == null) throw new InstantiationException("You need to choose a destination file for the second part of the PDF");
          File file2 = (File)getValue("destfile2");
          int pagenumber = Integer.parseInt((String)getValue("pagenumber"));

          // we create a reader for a certain document
      PdfReader reader = new PdfReader(src.getAbsolutePath());
      // we retrieve the total number of pages
      int n = reader.getNumberOfPages();
      System.out.println("There are " + n + " pages in the original file.");

      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);
        }
      }
View Full Code Here

            ArrayList<HashMap<String, Object>> master = new ArrayList<HashMap<String, Object>>();
            LwgDocument document = null;
            PdfCopy writer = null;
            for (int i = 0; i < files.length; i++) {
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(files[i].getAbsolutePath());
                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();
View Full Code Here

      File src = (File)getValue("srcfile");
            File directory = src.getParentFile();
            String name = src.getName();
            name = name.substring(0, name.lastIndexOf('.'));
          // we create a reader for a certain document
      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.");
      LwgDocument 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 LwgDocument(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).getHeight());
        }
        else {
          cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
        // step 5: we close the document
View Full Code Here

            String p = args[PERMISSIONS];
            for (int k = 0; k < p.length(); ++k) {
                permissions |= (p.charAt(k) == '0' ? 0 : permit[k]);
            }
            System.out.println("Reading " + args[INPUT_FILE]);
            PdfReader reader = new PdfReader(args[INPUT_FILE]);
            System.out.println("Writing " + args[OUTPUT_FILE]);
            HashMap moreInfo = new HashMap();
            for (int k = MOREINFO; k < args.length - 1; k += 2)
                moreInfo.put(args[k], args[k + 1]);
            PdfEncryptor.encrypt(reader, new FileOutputStream(args[OUTPUT_FILE]),
View Full Code Here

            "You need to choose a destination file");
      }
      File dest = (File) getValue("destfile");

      // we create a reader for a certain document
      PdfReader reader = new PdfReader(src.getAbsolutePath());
      // we retrieve the total number of pages and the page size
      int total = reader.getNumberOfPages();
      System.out.println("There are " + total
          + " pages in the original file.");

      LwgRectangle pageSize = reader.getPageSize(1);
      LwgRectangle newSize = new LwgRectangle(pageSize.getWidth() / 2, pageSize
          .getHeight());
      // step 1: creation of a document-object
      LwgDocument document = new LwgDocument(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;
      int p;
      for (int i = 0; i < total; i++) {
        p = i + 1;
        pageSize = reader.getPageSize(p);
        newSize = new LwgRectangle(pageSize.getWidth() / 2, pageSize.getHeight());

        document.newPage();
        offsetX = 0;
        offsetY = 0;
View Full Code Here

   * @see com.lowagie.toolbox.AbstractTool#execute()
   */
  public void execute() {
    try {
      if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile");
      PdfReader reader;
      if (getValue("ownerpassword") == null) {
        reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath());
      }
      else {
        reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath(), ((String)getValue("ownerpassword")).getBytes());
      }
      // Some general document information and page size
      System.out.println("=== Document Information ===");
      System.out.println("PDF Version: " + reader.getPdfVersion());
      System.out.println("Number of pages: " + reader.getNumberOfPages());
      System.out.println("Number of PDF objects: " + reader.getXrefSize());
      System.out.println("File length: " + reader.getFileLength());
      System.out.println("Encrypted? " + reader.isEncrypted());
      if (reader.isEncrypted()) {
        System.out.println("Permissions: " + PdfEncryptor.getPermissionsVerbose(reader.getPermissions()));
        System.out.println("128 bit? " + reader.is128Key());
      }
      System.out.println("Rebuilt? " + reader.isRebuilt());
      // Some metadata
      System.out.println("=== Metadata ===");
      HashMap<String, String> info = reader.getInfo();
      String key;
      String value;
      for (Map.Entry<String, String> entry: info.entrySet()) {
        key = entry.getKey();
        value = entry.getValue();
        System.out.println(key + ": " + value);
      }
      if (reader.getMetadata() == null) {
        System.out.println("There is no XML Metadata in the file");
      }
      else {
        System.out.println("XML Metadata: " + new String(reader.getMetadata()));
      }
    }
    catch(Exception e) {
          JOptionPane.showMessageDialog(internalFrame,
                e.getMessage(),
View Full Code Here

        else {
            try {
        int pagenumber = Integer.parseInt(args[3]);
               
        // we create a reader for a certain document
        PdfReader reader = new PdfReader(args[0]);
        // we retrieve the total number of pages
        int n = reader.getNumberOfPages();
        System.out.println("There are " + n + " pages in the original file.");
               
        if (pagenumber < 2 || pagenumber > n) {
          throw new DocumentException(MessageLocalization.getComposedMessage("you.can.t.split.this.document.at.page.1.there.is.no.such.page", pagenumber));
        }
               
        // 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(args[1]));
        PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(args[2]));
        // 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);
          }
          System.out.println("Processed page " + i);
View Full Code Here

            "You need to choose a destination file");
      }
      File dest = (File) getValue("destfile");

      // we create a reader for a certain document
      PdfReader reader = new PdfReader(src.getAbsolutePath());
      PdfObject o;
      PdfDictionary d;
      PdfDictionary l;
      PdfName n;
      for (int i = 1; i < reader.getXrefSize(); i++) {
        o = reader.getPdfObject(i);
        if (o instanceof PdfDictionary) {
          d = (PdfDictionary)o;
          o = d.get(PdfName.A);
          if (o == null) continue;
          if (o instanceof PdfDictionary) {
            l = (PdfDictionary)o;
          }
          else {
            PRIndirectReference r =(PRIndirectReference)o;
            l = (PdfDictionary)reader.getPdfObject(r.getNumber());
          }
          n = (PdfName)l.get(PdfName.S);
          if (PdfName.LAUNCH.equals(n)) {
            if (l.get(PdfName.F) != null) {
              System.out.println("Removed: " + l.get(PdfName.F));
View Full Code Here

      }
      catch(Exception e) {
        pow2 = 1;
      }
      // we create a reader for a certain document
      PdfReader reader = new PdfReader(src.getAbsolutePath());
      // we retrieve the total number of pages and the page size
      int total = reader.getNumberOfPages();
      System.out.println("There are " + total + " pages in the original file.");
            LwgRectangle pageSize = reader.getPageSize(1);
            LwgRectangle newSize = (pow2 % 2) == 0 ? new LwgRectangle(pageSize.getWidth(), pageSize.getHeight()) : new LwgRectangle(pageSize.getHeight(), pageSize.getWidth());
            LwgRectangle unitSize = new LwgRectangle(pageSize.getWidth(), pageSize.getHeight());
            LwgRectangle currentSize;
            for (int i = 0; i < pow2; i++) {
              unitSize = new LwgRectangle(unitSize.getHeight() / 2, unitSize.getWidth());
            }
            int n = (int)Math.pow(2, pow2);
            int r = (int)Math.pow(2, pow2 / 2);
            int c = n / r;
      // step 1: creation of a document-object
      LwgDocument document = new LwgDocument(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.getWidth() * ((i % n) % c);
        offsetY = newSize.getHeight() - (unitSize.getHeight() * (((i % n) / c) + 1));
        currentSize = reader.getPageSize(p);
        factor = Math.min(unitSize.getWidth() / currentSize.getWidth(), unitSize.getHeight() / currentSize.getHeight());
        offsetX += (unitSize.getWidth() - (currentSize.getWidth() * factor)) / 2f;
        offsetY += (unitSize.getHeight() - (currentSize.getHeight() * factor)) / 2f;
        page = writer.getImportedPage(reader, p);
        cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
View Full Code Here

TOP

Related Classes of com.lowagie.text.pdf.PdfReader

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.