Example usage for com.lowagie.text.pdf.codec TiffImage getTiffImage

List of usage examples for com.lowagie.text.pdf.codec TiffImage getTiffImage

Introduction

In this page you can find the example usage for com.lowagie.text.pdf.codec TiffImage getTiffImage.

Prototype

public static Image getTiffImage(RandomAccessFileOrArray s, int page) 

Source Link

Document

Reads a page from a TIFF image.

Usage

From source file:AppletViewer.java

License:Open Source License

/**
 * Metodo que crea un PDF a partir del archivo TIF con sus anotaciones
 * @param docid//  ww  w  .  ja v  a2s.  c o m
 */
public void convertDocument(String docid) {
    try {
        File dir = new File(descargados);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        dir = new File(transferencias);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // CARGAR TIF CON ANOTACIONES
        String tmpFileName = "TMP" + String.valueOf(System.currentTimeMillis()) + ".tif";
        OutputStream output = new FileOutputStream(descargados + File.separator + tmpFileName);
        myGenDocViewer.exportDocument(output, true, false, "image/tiff");
        output.close();

        // CONVERTIR TIF A PDF
        RandomAccessFileOrArray myTiffFile = new RandomAccessFileOrArray(
                descargados + File.separator + tmpFileName);
        int numberOfPages = TiffImage.getNumberOfPages(myTiffFile);
        Document TifftoPDF = new Document();
        TifftoPDF.setMargins(0, 0, 0, 0);
        PdfWriter.getInstance(TifftoPDF,
                new FileOutputStream(transferencias + File.separator + type + "-AN.pdf"));
        TifftoPDF.open();
        for (int i = 1; i <= numberOfPages; i++) {
            Image tempImage = TiffImage.getTiffImage(myTiffFile, i);
            float dpiX = tempImage.getDpiX() == 0 ? 200 : tempImage.getDpiX();
            float dpiY = tempImage.getDpiY() == 0 ? 200 : tempImage.getDpiY();
            float factorX = 72 / dpiX;
            float factorY = 72 / dpiY;
            tempImage.scaleAbsolute(factorX * tempImage.getWidth(), factorY * tempImage.getHeight());
            TifftoPDF.add(tempImage);
        }
        myTiffFile.close();
        TifftoPDF.close();

        // BORRAR EL TIFF
        File file = new File(descargados + File.separator + tmpFileName);
        if (file.delete()) {
            System.out.println(file.getName() + " eliminado");
        } else {
            file.deleteOnExit();
            ;
            System.out.println("Eliminacion fallo");
        }

        System.out.println("Fin de la creacion del PDF con anotaciones");

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

}

From source file:com.geek.tutorial.itext.image.SimpleImages.java

License:Open Source License

public SimpleImages() throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimpleImages.pdf"));
    document.open();/*w  w  w.jav  a2s  .co  m*/

    // Code 1
    document.add(new Paragraph("Simple Image"));
    com.lowagie.text.Image image = com.lowagie.text.Image.getInstance("mouse.jpg");
    document.add(image);

    // Code 2
    document.add(new Paragraph("\n" + "AWT Image"));
    java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage("square.jpg");
    com.lowagie.text.Image image2 = com.lowagie.text.Image.getInstance(awtImg, null);
    document.add(image2);
    document.newPage();

    // Code 3
    document.add(new Paragraph("Multipages tiff file"));
    RandomAccessFileOrArray ra = new RandomAccessFileOrArray("multipage.tif");
    int pages = TiffImage.getNumberOfPages(ra);
    for (int i = 1; i <= pages; i++) {
        document.add(TiffImage.getTiffImage(ra, i));
    }
    document.newPage();

    // Code 4
    document.add(new Paragraph("Animated Gifs"));
    GifImage img = new GifImage("bee.gif");
    int frame_count = img.getFrameCount();
    for (int i = 1; i <= frame_count; i++) {
        document.add(img.getImage(i));
    }
    document.close();
}

From source file:com.ikon.util.DocConverter.java

License:Open Source License

/**
 * TIFF to PDF conversion// www  . j  a  va 2  s  . co  m
 */
public void tiff2pdf(File input, File output) throws ConversionException {
    RandomAccessFileOrArray ra = null;
    Document doc = null;

    try {
        // Open PDF
        doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output));
        PdfContentByte cb = writer.getDirectContent();
        doc.open();
        //int pages = 0;

        // Open TIFF
        ra = new RandomAccessFileOrArray(input.getPath());
        int comps = TiffImage.getNumberOfPages(ra);

        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);

            if (img != null) {
                log.debug("tiff2pdf - page {}", c + 1);

                if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                    img.scaleToFit(500, 700);
                }

                img.setAbsolutePosition(20, 20);
                //doc.add(new Paragraph("page " + (c + 1)));
                cb.addImage(img);
                doc.newPage();
                //++pages;
            }
        }
    } catch (FileNotFoundException e) {
        throw new ConversionException("File not found: " + e.getMessage(), e);
    } catch (DocumentException e) {
        throw new ConversionException("Document exception: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ConversionException("IO exception: " + e.getMessage(), e);
    } finally {
        if (ra != null) {
            try {
                ra.close();
            } catch (IOException e) {
                // Ignore
            }
        }

        if (doc != null) {
            doc.close();
        }
    }
}

From source file:com.openkm.util.DocConverter.java

License:Open Source License

/**
 * TIFF to PDF conversion/*from   ww w  .  ja va 2 s .c o m*/
 */
public void tiff2pdf(File input, File output) throws ConversionException {
    RandomAccessFileOrArray ra = null;
    Document doc = null;

    try {
        // Open PDF
        doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output));
        PdfContentByte cb = writer.getDirectContent();
        doc.open();
        // int pages = 0;

        // Open TIFF
        ra = new RandomAccessFileOrArray(input.getPath());
        int comps = TiffImage.getNumberOfPages(ra);

        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);

            if (img != null) {
                log.debug("tiff2pdf - page {}", c + 1);

                if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                    img.scaleToFit(500, 700);
                }

                img.setAbsolutePosition(20, 20);
                // doc.add(new Paragraph("page " + (c + 1)));
                cb.addImage(img);
                doc.newPage();
                // ++pages;
            }
        }
    } catch (FileNotFoundException e) {
        throw new ConversionException("File not found: " + e.getMessage(), e);
    } catch (DocumentException e) {
        throw new ConversionException("Document exception: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ConversionException("IO exception: " + e.getMessage(), e);
    } finally {
        if (ra != null) {
            try {
                ra.close();
            } catch (IOException e) {
                // Ignore
            }
        }

        if (doc != null) {
            doc.close();
        }
    }
}

From source file:convert.Convertings.java

public void convertTif2PDF(String tifPath, String path) {
    System.out.println("one");
    String arg[] = { tifPath };/*from w w  w .  j a va  2  s . c  om*/
    System.out.println("one2");
    if (arg.length < 1) {
        System.out.println("Usage: Tiff2Pdf file1.tif [file2.tif ... fileN.tif]");
        System.exit(1);
    }
    String tiff;
    String pdf;
    System.out.println("two");
    for (int i = 0; i < arg.length; i++) {
        tiff = arg[i];
        pdf = path + ".pdf";
        Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));
            int pages = 0;
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            RandomAccessFileOrArray ra = null;
            int comps = 0;
            try {
                ra = new RandomAccessFileOrArray(tiff);
                comps = TiffImage.getNumberOfPages(ra);
            } catch (Throwable e) {
                System.out.println("Exception in " + tiff + " " + e.getMessage());
                continue;
            }
            System.out.println("Processing: " + tiff);
            for (int c = 0; c < comps; ++c) {
                try {
                    Image img = TiffImage.getTiffImage(ra, c + 1);
                    if (img != null) {
                        System.out.println("page " + (c + 1));
                        System.out.println("img.getDpiX() : " + img.getDpiX());
                        System.out.println("img.getDpiY() : " + img.getDpiY());
                        img.scalePercent(6200f / img.getDpiX(), 6200f / img.getDpiY());
                        //img.scalePercent(img.getDpiX(), img.getDpiY());
                        //document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                        img.setAbsolutePosition(0, 0);
                        cb.addImage(img);
                        document.newPage();
                        ++pages;
                    }
                } catch (Throwable e) {
                    System.out.println("Exception " + tiff + " page " + (c + 1) + " " + e.getMessage());
                }
            }
            ra.close();
            document.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("done");
    }
}

From source file:net.filterlogic.util.imaging.ToPDF.java

License:Apache License

public static void FromTIFF(String[] files, String pdfOutFile) throws OpenCaptureImagingException {
    String tiff_file;//w w  w .  j a v a2  s . co  m
    String pdf_file = pdfOutFile;

    try {

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
        writer.setFullCompression();
        int pages = 0;
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        RandomAccessFileOrArray ra = null;

        for (int i = 0; i < files.length; i++) {
            int comps = 0;
            tiff_file = files[i];

            try {
                ra = new RandomAccessFileOrArray(tiff_file);
                comps = TiffImage.getNumberOfPages(ra);
            } catch (Throwable e) {
                throw new Exception("Exception in " + tiff_file + " " + e.toString());
                //continue;
            }

            //System.out.println("Processing: " + tiff_file);

            for (int c = 0; c < comps; ++c) {
                try {
                    Image img = TiffImage.getTiffImage(ra, c + 1);
                    if (img != null) {
                        //System.out.println("page " + (c + 1));

                        img.scaleToFit(675, 775);
                        img.setAbsolutePosition(0, 50);
                        //                            document.add(new Paragraph(tiff_file + " - page " + (c + 1)));

                        cb.addImage(img);
                        document.newPage();
                        ++pages;
                    }
                } catch (Throwable e) {
                    throw new Exception("Exception " + tiff_file + " page " + (c + 1) + " " + e.getMessage());
                }
            }

            ra.close();
        }

        // close pdf
        document.close();

    } catch (Exception e) {
        throw new OpenCaptureImagingException("ToPDF exception: " + e.toString());
    }

}

From source file:org.sipfoundry.faxrx.FaxProcessor.java

License:Open Source License

private File tiff2Pdf(File tiffFile) {
    Pattern pattern = Pattern.compile("(.*).tiff");
    Matcher matcher = pattern.matcher(tiffFile.getName());
    boolean matchFound = matcher.find();

    // check if tiffFile is actually a TIFF file, just in case
    if (matchFound) {
        // located at default tmp-file directory
        File pdfFile = new File(System.getProperty("java.io.tmpdir"), matcher.group(1) + ".pdf");
        try {/*  w  w w.  j ava  2s.  co m*/
            // read TIFF file
            RandomAccessFileOrArray tiff = new RandomAccessFileOrArray(tiffFile.getAbsolutePath());

            // get number of pages of TIFF file
            int pages = TiffImage.getNumberOfPages(tiff);

            // create PDF file
            Document pdf = new Document(PageSize.LETTER, 0, 0, 0, 0);

            PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(pdfFile));
            writer.setStrictImageSequence(true);

            // open PDF filex
            pdf.open();

            PdfContentByte contentByte = writer.getDirectContent();

            // write PDF file page by page
            for (int page = 1; page <= pages; page++) {
                Image temp = TiffImage.getTiffImage(tiff, page);
                temp.scalePercent(7200f / temp.getDpiX(), 7200f / temp.getDpiY());
                pdf.setPageSize(new Rectangle(temp.getScaledWidth(), temp.getScaledHeight()));
                temp.setAbsolutePosition(0, 0);
                contentByte.addImage(temp);
                pdf.newPage();
            }
            // close PDF file
            pdf.close();
        } catch (Exception e) {
            LOG.error("faxrx::tiff2Pdf error " + e.getMessage());
            e.printStackTrace();
            return null;
        }
        return pdfFile;
    }

    else {
        return null;
    }
}

From source file:org.sipfoundry.faxrx.FaxRx.java

License:Contributor Agreement License

private File tiff2Pdf(File tiffFile) {

    Pattern pattern = Pattern.compile("(.*).tiff");
    Matcher matcher = pattern.matcher(tiffFile.getName());
    boolean matchFound = matcher.find();

    // check if tiffFile is actually a TIFF file, just in case
    if (matchFound) {

        // located at default tmp-file directory
        File pdfFile = new File(System.getProperty("java.io.tmpdir"), matcher.group(1) + ".pdf");

        try {//w  ww.  jav  a2s  .  com

            // read TIFF file
            RandomAccessFileOrArray tiff = new RandomAccessFileOrArray(tiffFile.getAbsolutePath());

            // get number of pages of TIFF file
            int pages = TiffImage.getNumberOfPages(tiff);

            // create PDF file
            Document pdf = new Document(PageSize.LETTER);

            PdfWriter.getInstance(pdf, new FileOutputStream(pdfFile));

            // open PDF filex
            pdf.open();

            // write PDF file page by page
            for (int page = 1; page <= pages; page++) {
                Image temp = TiffImage.getTiffImage(tiff, page);
                pdf.add(temp);
            }

            // close PDF file
            pdf.close();
        }

        catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return pdfFile;
    }

    else {
        return null;
    }
}

From source file:oscar.oscarEncounter.oscarConsultationRequest.pageUtil.ImagePDFCreator.java

License:Open Source License

/**
 * Prints the consultation request./*from w w  w  .j  a  v  a2  s  .  c om*/
 * @throws IOException when an error with the output stream occurs
 * @throws DocumentException when an error in document construction occurs
 */
public void printPdf() throws IOException, DocumentException {

    Image image;
    try {
        image = Image.getInstance((String) request.getAttribute("imagePath"));
    } catch (Exception e) {
        logger.error("Unexpected error:", e);
        throw new DocumentException(e);
    }

    // Create the document we are going to write to
    document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, os);

    document.setPageSize(PageSize.LETTER);
    ResourceBundle.getBundle("oscarResources", request.getLocale())
            .getString("oscarEncounter.oscarConsultationRequest.consultationFormPrint.msgImage");
    document.addCreator("OSCAR");
    document.open();

    int type = image.getOriginalType();
    if (type == Image.ORIGINAL_TIFF) {
        // The following is composed of code from com.lowagie.tools.plugins.Tiff2Pdf modified to create the 
        // PDF in memory instead of on disk
        RandomAccessFileOrArray ra = new RandomAccessFileOrArray((String) request.getAttribute("imagePath"));
        int comps = TiffImage.getNumberOfPages(ra);
        boolean adjustSize = false;
        PdfContentByte cb = writer.getDirectContent();
        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);
            if (img != null) {
                if (adjustSize) {
                    document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                    document.newPage();
                    img.setAbsolutePosition(0, 0);
                } else {
                    if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                        img.scaleToFit(500, 700);
                    }
                    img.setAbsolutePosition(20, 20);
                    document.newPage();
                    document.add(
                            new Paragraph((String) request.getAttribute("imageTitle") + " - page " + (c + 1)));
                }
                cb.addImage(img);

            }
        }
        ra.close();
    } else {
        PdfContentByte cb = writer.getDirectContent();
        if (image.getScaledWidth() > 500 || image.getScaledHeight() > 700) {
            image.scaleToFit(500, 700);
        }
        image.setAbsolutePosition(20, 20);
        cb.addImage(image);
    }
    document.close();
}