Example usage for com.itextpdf.text.io RandomAccessSourceFactory RandomAccessSourceFactory

List of usage examples for com.itextpdf.text.io RandomAccessSourceFactory RandomAccessSourceFactory

Introduction

In this page you can find the example usage for com.itextpdf.text.io RandomAccessSourceFactory RandomAccessSourceFactory.

Prototype

public RandomAccessSourceFactory() 

Source Link

Document

Creates a factory that will give preference to accessing the underling data source using memory mapped files

Usage

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 w  w  .j  a v  a 2  s.c o  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();
        }
    }
}

From source file:com.github.albfernandez.joinpdf.JoinPdf.java

License:Open Source License

private static RandomAccessSource createRamdomAccessSource(final File file) throws IOException {
    RandomAccessSource source = new RandomAccessSourceFactory().setForceRead(false)
            .setUsePlainRandomAccess(Document.plainRandomAccess).createBestSource(file.getAbsolutePath());
    return source;
}

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;// ww w  .  ja  v a  2s  .c om
    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;/*from  www  .jav  a 2  s.  co  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:me.Aron.Heinecke.fbot.lib.Converter.java

License:Apache License

/***
 * Return the amount of sites in a pdf/*w ww . ja v a 2  s  . c  om*/
 * using a deprecated (working) iText function
 * @param file path of the file to use
 * @return amount of sites
 */
@SuppressWarnings("deprecation")
public int pdfSites(String file) {
    try {
        RandomAccessFile raf = new RandomAccessFile(new File(file), "r");
        RandomAccessFileOrArray pdfFile;
        pdfFile = new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(raf));
        PdfReader reader = new PdfReader(pdfFile, new byte[0]);
        int pages = reader.getNumberOfPages();
        reader.close();
        return pages;
    } catch (InvalidPdfException e) {
        fbot.getLogger().severe("converter", "Invalid PDF file!: no index");
    } catch (Exception e) {
        fbot.getLogger().exception("converter", e);
    }
    return -1;
}

From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java

License:Open Source License

Map<String, List> parseDAParam(PdfString DA) throws IOException {
    Map<String, List> commandArguments = new HashMap<String, List>();

    PRTokeniser tokeniser = new PRTokeniser(
            new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(DA.getBytes())));
    List currentArguments = new ArrayList();

    while (tokeniser.nextToken()) {
        if (tokeniser.getTokenType() == PRTokeniser.TokenType.OTHER) {
            String key = tokeniser.getStringValue();

            if (key.equals("RG") || key.equals("G") || key.equals("K")) {
                key = STROKE_COLOR;//ww w  .  j  av  a 2 s. co m
            } else if (key.equals("rg") || key.equals("g") || key.equals("k")) {
                key = FILL_COLOR;
            }

            commandArguments.put(key, currentArguments);
            currentArguments = new ArrayList();
        } else {
            switch (tokeniser.getTokenType()) {
            case NUMBER:
                currentArguments.add(new PdfNumber(tokeniser.getStringValue()));
                break;

            case NAME:
                currentArguments.add(new PdfName(tokeniser.getStringValue()));
                break;

            default:
                currentArguments.add(tokeniser.getStringValue());
            }
        }
    }

    return commandArguments;
}

From source file:sc.emea.tools.image.Tiff2PDF.java

License:Apache License

public static void main(String args[]) {

    int page_counter = 0;

    try {//from  www . j ava  2 s . com

        // Read the Tiff File
        FileList myFileList = new FileList("C:\\DEV\\Eclipse-Workspace\\Luna-J\\Tiff2PdfConverter\\TIFF\\");
        myFileList.setFilterPreMiddleSuffix("", "", ".tiff");
        myFileList.sort(ComparableAttribute.dateLastModified, SortingDirection.asc);

        File[] myTiffFileFiles = myFileList.getFilesArray();

        if (myTiffFileFiles.length < 1)
            return;
        else
            for (File file : myTiffFileFiles)
                System.out.println(file.getName());

        Document myDocument = new Document();
        myDocument.setMargins(0, 0, 0, 0);
        PdfWriter.getInstance(myDocument,
                new FileOutputStream("C:\\DEV\\Eclipse-Workspace\\Luna-J\\Tiff2PdfConverter\\PDF\\test.pdf"));
        myDocument.open();

        RandomAccessSourceFactory rasFactory = new RandomAccessSourceFactory();

        for (File file : myTiffFileFiles) {

            FileInputStream _fis = new FileInputStream(file.getAbsolutePath());

            RandomAccessSource _ra_source = rasFactory.createSource(_fis);
            RandomAccessFileOrArray _ra_file = new RandomAccessFileOrArray(_ra_source);

            int numberOfPages = TiffImage.getNumberOfPages(_ra_file);
            for (int i = 1; i <= numberOfPages; i++) {
                Image _image = TiffImage.getTiffImage(_ra_file, i);
                _image.scaleAbsolute(myDocument.getPageSize());
                myDocument.add(_image);
                page_counter++;
            }

        }
        myDocument.close();
        System.out.println("Tiff to PDF conversion completed for " + page_counter);

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

From source file:spntoolsdata.pdf.util.RemoveBlankPageFromPDF.java

public static void removeBlankPdfPages(String source, String destination)
        throws IOException, DocumentException {
    PdfReader r = null;//from  ww  w . j  a  va2  s  .  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();
    }
}