Package com.lowagie.text.pdf

Examples of com.lowagie.text.pdf.RandomAccessFileOrArray


    try {
      if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile");
      File tiff_file = (File)getValue("srcfile");
      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;
      Document document = new Document(PageSize.A4);
            float width = PageSize.A4.getWidth() - 40;
            float height = PageSize.A4.getHeight() - 120;
      if ("ORIGINAL".equals(getValue("pagesize"))) {
        Image img = TiffImage.getTiffImage(ra, 1);
                if (img.getDpiX() > 0 && img.getDpiY() > 0) {
                    img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY());
                }
        document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
        adjustSize = true;
      }
      else if ("LETTER".equals(getValue("pagesize"))) {
        document.setPageSize(PageSize.LETTER);
                width = PageSize.LETTER.getWidth() - 40;
                height = PageSize.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) {
                Image 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 Rectangle(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(),
View Full Code Here


      File odd_file = (File)getValue("odd");
      if (getValue("even") == null) throw new InstantiationException("You need to choose a sourcefile for the even pages");
      File even_file = (File)getValue("even");
      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.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 {
          Image imgOdd = TiffImage.getTiffImage(odd, c + 1);
          Image imgEven = TiffImage.getTiffImage(even, count - c);
          document.setPageSize(new Rectangle(imgOdd.getScaledWidth(),
              imgOdd.getScaledHeight()));
          document.newPage();
          imgOdd.setAbsolutePosition(0, 0);
          cb.addImage(imgOdd);
          document.setPageSize(new Rectangle(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

        out.println("==============Page " + pageNum + "====================");
        out.println("- - - - - Dictionary - - - - - -");
        PdfDictionary pageDictionary = reader.getPageN(pageNum);
        out.println(getDictionaryDetail(pageDictionary));
        out.println("- - - - - Content Stream - - - - - -");
        RandomAccessFileOrArray f = reader.getSafeFile();
       
        byte[] contentBytes = reader.getPageContent(pageNum, f);
        f.close();

       
        InputStream is = new ByteArrayInputStream(contentBytes);
        int ch;
        while ((ch = is.read()) != -1){
View Full Code Here

     * @param pageNum  the page number of page you want get the content stream from
     * @return  a byte array with the content stream of a page
     * @throws IOException
     */
    private byte[] getContentBytesForPage(int pageNum) throws IOException {
        RandomAccessFileOrArray f = reader.getSafeFile();
        byte[] contentBytes = reader.getPageContent(pageNum, f);
        f.close();
        return contentBytes;
    }
View Full Code Here

      if (c1 == 'B' && c2 == 'M') {
        return  BmpImage.getImage(url);
      }
      if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
          || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
        RandomAccessFileOrArray ra = null;
        try {
          if (url.getProtocol().equals("file")) {
            String file = url.getFile();
                        file = Utilities.unEscapeURL(file);
            ra = new RandomAccessFileOrArray(file);
          } else
            ra = new RandomAccessFileOrArray(url);
          Image img = TiffImage.getTiffImage(ra, 1);
          img.url = url;
          return img;
        } finally {
          if (ra != null)
            ra.close();
        }

      }
      if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' &&
          c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) {
        RandomAccessFileOrArray ra = null;
        try {
          if (url.getProtocol().equals("file")) {
            String file = url.getFile();
            file = Utilities.unEscapeURL(file);
                  ra = new RandomAccessFileOrArray(file);
          } else
            ra = new RandomAccessFileOrArray(url);
          Image img = JBIG2Image.getJbig2Image(ra, 1);
          img.url = url;
          return img;
        } finally {
            if (ra != null)
              ra.close();
        }
      }
      throw new IOException(url.toString()
          + " is not a recognized imageformat.");
    } finally {
View Full Code Here

      if (c1 == 'B' && c2 == 'M') {
        return BmpImage.getImage(imgb);
      }
      if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
          || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
        RandomAccessFileOrArray ra = null;
        try {
          ra = new RandomAccessFileOrArray(imgb);
          Image img = TiffImage.getTiffImage(ra, 1);
                    if (img.getOriginalData() == null)
                        img.setOriginalData(imgb);
          return img;
        } finally {
          if (ra != null)
            ra.close();
        }

      }
      if ( c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' ) {
        is = new java.io.ByteArrayInputStream(imgb);
        is.skip(4);
        int c5 = is.read();
        int c6 = is.read();
        int c7 = is.read();
        int c8 = is.read();
        if ( c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n' ) {
          int file_header_flags = is.read();
          int number_of_pages = -1;
          if ( (file_header_flags & 0x2) == 0x2 ) {
            number_of_pages = (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
          }
          is.close();
          // a jbig2 file with a file header.  the header is the only way we know here.                                                          
          // embedded jbig2s don't have a header, have to create them by explicit use of Jbig2Image?
          // nkerr, 2008-12-05  see also the getInstance(URL)
          RandomAccessFileOrArray ra = null;
          try {
            ra = new RandomAccessFileOrArray(imgb);
            Image img = JBIG2Image.getJbig2Image(ra, 1);
            if (img.getOriginalData() == null)
              img.setOriginalData(imgb);
            return img;
          } finally {
            if (ra != null)
              ra.close();
          }
        }
      }
      throw new IOException(
          "The byte array is not a recognized imageformat.");
View Full Code Here

    if (file.exists()) {
      int page = 1;
      PdfReader reader = null;

      try {
        reader = new PdfReader(new RandomAccessFileOrArray(file.getAbsolutePath()), null);
        HashMap<String, String> pdfinfo = reader.getInfo();

        StringBuffer sb = new StringBuffer();
        sb.append("<html>=== Document Information ===<p>");
        sb.append(reader.getCropBox(page).getHeight() + "*"
 
View Full Code Here

   * @throws DocumentException
   */
  public PdfFile(File file) throws IOException, DocumentException {
    if (file == null)
      throw new IOException("No file selected.");
    RandomAccessFileOrArray pdf = new RandomAccessFileOrArray(file.getAbsolutePath());
    directory = file.getParentFile();
    filename = file.getName();
    readFile(pdf);
  }
View Full Code Here

   * @param  file  the byte[] to read
   * @throws IOException
   * @throws DocumentException
   */
  public PdfFile(byte[] file) throws IOException, DocumentException {
    RandomAccessFileOrArray pdf = new RandomAccessFileOrArray(file);
    readFile(pdf);
  }
View Full Code Here

  protected RandomAccessFileOrArray workAround() throws DocumentException, IOException {
    if (directory == null) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      PdfStamper stamper = new PdfStamper(reader, baos);
      stamper.close();
      return new RandomAccessFileOrArray(baos.toByteArray());
    }
    else {
      File temp = File.createTempFile(filename.substring(0, filename.lastIndexOf(".pdf")) + "~", ".pdf", directory);
      temp.deleteOnExit();
      FileOutputStream fos = new FileOutputStream(temp);
      PdfStamper stamper = new PdfStamper(reader, fos);
      stamper.close();
      return new RandomAccessFileOrArray(temp.getAbsolutePath());
    }   
  }
View Full Code Here

TOP

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

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.