List of usage examples for com.itextpdf.text.io RandomAccessSourceFactory createBestSource
public RandomAccessSource createBestSource(FileChannel channel) throws IOException
From source file:com.betel.flowers.pdf.util.RemoveBlankPageFromPDF.java
public static void removeBlankPdfPages(String source, String destination) throws IOException, DocumentException { PdfReader r = null;/*from w ww . j av a2s .c om*/ RandomAccessSourceFactory rasf = null; RandomAccessFileOrArray raf = null; Document document = null; PdfCopy writer = null; try { r = new PdfReader(source); // deprecated // RandomAccessFileOrArray raf // = new RandomAccessFileOrArray(pdfSourceFile); // itext 5.4.1 rasf = new RandomAccessSourceFactory(); raf = new RandomAccessFileOrArray(rasf.createBestSource(source)); document = new Document(r.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(destination)); document.open(); PdfImportedPage page = null; for (int i = 1; i <= r.getNumberOfPages(); i++) { // first check, examine the resource dictionary for /Font or // /XObject keys. If either are present -> not blank. PdfDictionary pageDict = r.getPageN(i); PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES); boolean noFontsOrImages = true; if (resDict != null) { noFontsOrImages = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; } if (!noFontsOrImages) { byte bContent[] = r.getPageContent(i, raf); ByteArrayOutputStream bs = new ByteArrayOutputStream(); bs.write(bContent); if (bs.size() > BLANK_THRESHOLD) { page = writer.getImportedPage(r, i); writer.addPage(page); } } } } finally { if (document != null) { document.close(); } if (writer != null) { writer.close(); } if (raf != null) { raf.close(); } if (r != null) { r.close(); } } }
From source file:com.vectorprint.report.itext.DefaultElementProducer.java
License:Open Source License
@Override public void loadPdf(File pdf, PdfWriter writer, byte[] password, ImageProcessor imageProcessor, int... pages) throws VectorPrintException { RandomAccessFileOrArray ra = null;//from w ww . j a v a 2 s .c o m try { RandomAccessSourceFactory rasf = new RandomAccessSourceFactory(); ra = new RandomAccessFileOrArray(rasf.createBestSource(pdf.getPath())); PdfReader reader = new PdfReader(ra, password); if (pages == null) { for (int i = 0; i < reader.getNumberOfPages();) { imageProcessor.processImage(Image.getInstance(writer.getImportedPage(reader, ++i))); writer.freeReader(reader); } } else { for (int i : pages) { imageProcessor.processImage(Image.getInstance(writer.getImportedPage(reader, i))); writer.freeReader(reader); } } } catch (BadElementException | IOException ex) { throw new VectorPrintException(String.format("unable to load image %s", pdf.toString()), ex); } finally { if (ra != null) { try { ra.close(); } catch (IOException ex) { } } } }
From source file:com.vectorprint.report.itext.DefaultElementProducer.java
License:Open Source License
@Override public void loadTiff(File tiff, ImageProcessor imageProcessor, int... pages) throws VectorPrintException { RandomAccessFileOrArray ra = null;// w w w. java 2 s . c o m try { RandomAccessSourceFactory rasf = new RandomAccessSourceFactory(); ra = new RandomAccessFileOrArray(rasf.createBestSource(tiff.getPath())); if (pages == null) { for (int i = 0; i < TiffImage.getNumberOfPages(ra);) { imageProcessor.processImage(TiffImage.getTiffImage(ra, ++i)); } } else { for (int i : pages) { imageProcessor.processImage(TiffImage.getTiffImage(ra, i)); } } } catch (IOException ex) { throw new VectorPrintException(String.format("unable to load tiff %s", tiff.toString()), ex); } finally { if (ra != null) { try { ra.close(); } catch (IOException ex) { } } } }
From source file:spntoolsdata.pdf.util.RemoveBlankPageFromPDF.java
public static void removeBlankPdfPages(String source, String destination) throws IOException, DocumentException { PdfReader r = null;/* w w w. ja v a2 s . co m*/ RandomAccessSourceFactory rasf = null; RandomAccessFileOrArray raf = null; Document document = null; PdfCopy writer = null; try { r = new PdfReader(source); // deprecated // RandomAccessFileOrArray raf // = new RandomAccessFileOrArray(pdfSourceFile); // itext 5.4.1 rasf = new RandomAccessSourceFactory(); raf = new RandomAccessFileOrArray(rasf.createBestSource(source)); document = new Document(r.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(destination)); document.open(); PdfImportedPage page = null; for (int i = 1; i <= r.getNumberOfPages(); i++) { // first check, examine the resource dictionary for /Font or // /XObject keys. If either are present -> not blank. PdfDictionary pageDict = r.getPageN(i); PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES); boolean noFontsOrImages = true; if (resDict != null) { noFontsOrImages = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; } if (!noFontsOrImages) { byte bContent[] = r.getPageContent(i, raf); ByteArrayOutputStream bs = new ByteArrayOutputStream(); bs.write(bContent); if (bs.size() > BLANK_THRESHOLD) { page = writer.getImportedPage(r, i); writer.addPage(page); } } } } finally { if (document != null) document.close(); if (writer != null) writer.close(); if (raf != null) raf.close(); if (r != null) r.close(); } }