Example usage for com.lowagie.text.pdf RandomAccessFileOrArray close

List of usage examples for com.lowagie.text.pdf RandomAccessFileOrArray close

Introduction

In this page you can find the example usage for com.lowagie.text.pdf RandomAccessFileOrArray close.

Prototype

public void close() throws IOException 

Source Link

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// w  ww  .  j  a  v  a  2s .  c om
 */
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.ikon.util.DocConverter.java

License:Open Source License

/**
 * TIFF to PDF conversion//from  w w w . j  ava  2s  .  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:com.openkm.util.DocConverter.java

License:Open Source License

/**
 * TIFF to PDF conversion//from w  w w  .  j  a v  a 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:convert.Convertings.java

public void convertTif2PDF(String tifPath, String path) {
    System.out.println("one");
    String arg[] = { tifPath };//from ww w . j a v a2 s  .co  m
    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;//from   ww w.  java  2 s.  c  om
    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.xhtmlrenderer.pdf.TrueTypeUtil.java

License:Open Source License

public static void populateDescription(String path, BaseFont font, FontDescription descr)
        throws IOException, NoSuchFieldException, IllegalAccessException, DocumentException {
    RandomAccessFileOrArray rf = null;
    try {/*from   www.j av  a  2  s  .c  o m*/
        rf = new RandomAccessFileOrArray(getTTCName(path));

        rf = populateDescription0(path, font, descr, rf);
    } finally {
        if (rf != null) {
            try {
                rf.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:org.xhtmlrenderer.pdf.TrueTypeUtil.java

License:Open Source License

public static void populateDescription(String path, byte[] contents, BaseFont font, FontDescription descr)
        throws IOException, NoSuchFieldException, IllegalAccessException, DocumentException {
    RandomAccessFileOrArray rf = null;
    try {//  w  ww  . j av a 2s.c  o  m
        rf = new RandomAccessFileOrArray(contents);

        rf = populateDescription0(path, font, descr, rf);
    } finally {
        if (rf != null) {
            try {
                rf.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:org.xhtmlrenderer.pdf.TrueTypeUtil.java

License:Open Source License

private static RandomAccessFileOrArray populateDescription0(String path, BaseFont font, FontDescription descr,
        RandomAccessFileOrArray rf)
        throws NoSuchFieldException, IllegalAccessException, DocumentException, IOException {
    Map tables = extractTables(font);

    descr.setStyle(guessStyle(font));/*from ww  w  .j ava2s  .  c  o m*/

    int[] location = (int[]) tables.get("OS/2");
    if (location == null) {
        throw new DocumentException("Table 'OS/2' does not exist in " + path);
    }
    rf.seek(location[0]);
    int want = 4;
    long got = rf.skip(want);
    if (got < want) {
        throw new DocumentException("Skip TT font weight, expect read " + want + " bytes, but only got " + got);
    }
    descr.setWeight(rf.readUnsignedShort());
    want = 20;
    got = rf.skip(want);
    if (got < want) {
        throw new DocumentException(
                "Skip TT font strikeout, expect read " + want + " bytes, but only got " + got);
    }
    descr.setYStrikeoutSize(rf.readShort());
    descr.setYStrikeoutPosition(rf.readShort());

    location = (int[]) tables.get("post");

    if (location != null) {
        rf.seek(location[0]);
        want = 8;
        got = rf.skip(want);
        if (got < want) {
            throw new DocumentException(
                    "Skip TT font underline, expect read " + want + " bytes, but only got " + got);
        }
        descr.setUnderlinePosition(rf.readShort());
        descr.setUnderlineThickness(rf.readShort());
    }

    rf.close();
    rf = null;
    return rf;
}

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

License:Open Source License

/**
 * Prints the consultation request./*from  www.  ja v  a 2s.com*/
 * @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();
}