List of usage examples for com.itextpdf.text.io FileChannelRandomAccessSource FileChannelRandomAccessSource
public FileChannelRandomAccessSource(FileChannel channel) throws IOException
From source file:com.ephesoft.dcma.imagemagick.impl.ITextPDFCreator.java
License:Open Source License
/** * Converts specified tiff file into pdf. * /*from w w w . j av a 2 s . c om*/ * @param tiffFile{@link File} to be converted into pdf. * @throws DCMAApplicationException if any error occurs while conversion. */ public static void convertTiffIntoPdf(final File tiffFile) throws DCMAApplicationException { if (null == tiffFile) { LOGGER.error("Unable to convert tiff file as specified file is null."); } else { com.itextpdf.text.Document document = null; RandomAccessFile randomAccessFile = null; RandomAccessFileOrArray randomAccessFileOrArray = null; FileChannelRandomAccessSource fileChannelRandomAccessSource = null; try { final String tiffFilePath = tiffFile.getAbsolutePath(); if (tiffFilePath.endsWith(FileType.TIF.getExtensionWithDot()) || tiffFilePath.endsWith(FileType.TIFF.getExtensionWithDot())) { randomAccessFile = new RandomAccessFile(tiffFile, ICommonConstants.READ_MODE); fileChannelRandomAccessSource = new FileChannelRandomAccessSource( randomAccessFile.getChannel()); document = new com.itextpdf.text.Document(); final int lastIndexofTiffExtension = tiffFilePath.toLowerCase() .lastIndexOf(FileType.TIF.getExtensionWithDot()); PdfWriter.getInstance(document, new FileOutputStream(EphesoftStringUtil.concatenate( tiffFilePath.substring(0, lastIndexofTiffExtension), FileType.PDF.getExtensionWithDot()))); document.open(); randomAccessFileOrArray = new RandomAccessFileOrArray(fileChannelRandomAccessSource); final int pageCount = TiffImage.getNumberOfPages(randomAccessFileOrArray); Image image; for (int index = 1; index <= pageCount; index++) { image = TiffImage.getTiffImage(randomAccessFileOrArray, index); final Rectangle pageSize = new Rectangle(image.getWidth(), image.getHeight()); document.setPageSize(pageSize); document.newPage(); document.add(image); } LOGGER.info(EphesoftStringUtil.concatenate(tiffFilePath, " successfully converted into PDF.")); } else { LOGGER.error("Unable to convert as specified file is not a valid tiff file."); } } catch (final DocumentException e) { LOGGER.error("DocumentException is occurred while processing specified tiff file for conversion."); throw new DCMAApplicationException(EphesoftStringUtil .concatenate("DocumentException occured while generating PDF", e.getMessage()), e); } catch (final IOException e) { LOGGER.error("IOException is occurred while processing specified tiff file for conversion."); throw new DCMAApplicationException( EphesoftStringUtil.concatenate("IOException occured while generating PDF", e.getMessage()), e); } finally { FileUtils.closeStream(randomAccessFileOrArray); FileUtils.closeFileChannelRandomAccessSource(fileChannelRandomAccessSource); FileUtils.closeResource(randomAccessFile); document.close(); } } }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * PdfReader ? pdf pdf ???/* www . j av a 2s . c om*/ * * @param pdfFile ? pdf * @return PdfReader * @throws IOException */ public static PdfReader getPdfReader(File pdfFile) throws IOException { Document.plainRandomAccess = true; FileInputStream fileStream = new FileInputStream(pdfFile); return new PdfReader( new RandomAccessFileOrArray(new FileChannelRandomAccessSource(fileStream.getChannel())), null); }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * PdfReader ? pdf pdf ??/*from w w w. j a v a 2 s. c om*/ * * @param pdfFile ? pdf * @param ownerPassword ? * @return * @throws IOException */ public static PdfReader getPdfReader(File pdfFile, String ownerPassword) throws IOException { FileInputStream fileStream = new FileInputStream(pdfFile); if (ownerPassword == null) return getPdfReader(pdfFile); return new PdfReader( new RandomAccessFileOrArray(new FileChannelRandomAccessSource(fileStream.getChannel())), ownerPassword.getBytes()); }