Example usage for com.lowagie.text.pdf PdfReader PdfReader

List of usage examples for com.lowagie.text.pdf PdfReader PdfReader

Introduction

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

Prototype

public PdfReader(PdfReader reader) 

Source Link

Document

Creates an independent duplicate.

Usage

From source file:br.gov.jfrj.siga.cd.AssinaturaDigital.java

License:Open Source License

@SuppressWarnings("unchecked")
protected static byte[] getHasheableRangeFromPDF(byte[] pdf) throws Exception {
    PdfReader reader = new PdfReader(pdf);
    OutputStream fout = new NullOutputStream();

    final int SIZE = 128000;

    PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
    PdfSignatureAppearance sap = stp.getSignatureAppearance();

    PdfDictionary dic = new PdfDictionary();
    dic.put(PdfName.TYPE, PdfName.SIG);//from  w w  w.j av  a 2 s  .  c  om
    dic.put(PdfName.FILTER, new PdfName("Adobe.PPKMS"));
    dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached"));

    sap.setCryptoDictionary(dic);
    HashMap exc = new HashMap();
    exc.put(PdfName.CONTENTS, new Integer(SIZE));
    sap.preClose(exc);

    byte[] data = streamToByteArray(sap.getRangeStream());

    byte[] outc = new byte[(SIZE - 2) / 2];
    PdfDictionary dic2 = new PdfDictionary();

    dic2.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));
    sap.close(dic2);
    System.out.println("Hash: " + MessageDigest.getInstance("MD5").digest(data, 0, data.length));
    return data;
}

From source file:buckley.extract.PdfReaderFactory.java

License:Apache License

public PdfReader build(InputStream input) {
    try {//from   w  w  w .j  av  a 2s.  c o m
        return new PdfReader(input);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:buckley.Pdf.java

License:Apache License

public Pdf(InputStream templateInputStream, OutputStream outputStream) {
    boolean errorOccured = false;
    try {//from   ww w .  j a  v  a 2s. c o m
        reader = new PdfReader(templateInputStream);
        document = new Document(reader.getPageSizeWithRotation(1));
        writer = PdfWriter.getInstance(document, outputStream);
        pdfAcroForm = new PdfAcroForm(writer);
        document.open();
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (errorOccured) {
            close();
        }
    }
}

From source file:clases.unirPdf.java

public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {

    Document document = new Document();
    try {//from  w  w  w.  j av a  2 s.  c o  m
        List<InputStream> pdfs = streamOfPDFFiles;
        List<PdfReader> readers = new ArrayList<PdfReader>();
        int totalPages = 0;
        Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }

        PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {

                Rectangle rectangle = pdfReader.getPageSizeWithRotation(1);
                document.setPageSize(rectangle);
                document.newPage();

                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                switch (rectangle.getRotation()) {
                case 0:
                    cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    break;
                case 90:
                    cb.addTemplate(page, 0, -1f, 1f, 0, 0, pdfReader.getPageSizeWithRotation(1).getHeight());
                    break;
                case 180:
                    cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0);
                    break;
                case 270:
                    cb.addTemplate(page, 0, 1.0F, -1.0F, 0, pdfReader.getPageSizeWithRotation(1).getWidth(), 0);
                    break;
                default:
                    break;
                }
                if (paginate) {
                    cb.beginText();
                    cb.getPdfDocument().getPageSize();
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

From source file:classroom.filmfestival_c.Movies21.java

@SuppressWarnings("unchecked")
public static void main(String args[]) {
    Movies18.main(args);//from  w  w w .ja v a2  s.c o  m
    Movies20.main(args);
    try {
        PdfReader reader1 = new PdfReader(Movies18.RESULT);
        List<Map> list1 = SimpleBookmark.getBookmark(reader1);
        int[] offsets1 = new int[list1.size() + 1];
        int count = 0;
        for (Map<String, String> mark : list1) {
            offsets1[count++] = getPageNumber(mark.get("Page"));
        }
        offsets1[count] = reader1.getNumberOfPages() + 1;
        PdfReader reader2 = new PdfReader(Movies20.RESULT);
        List<Map> list2 = SimpleBookmark.getBookmark(reader2);
        if (list2.size() != list1.size()) {
            throw new DocumentException("The documents don't have the same number of bookmark entries.");
        }
        int[] offsets2 = new int[list2.size() + 1];
        count = 0;
        for (Map<String, String> mark : list2) {
            offsets2[count++] = getPageNumber(mark.get("Page"));
        }
        offsets2[count] = reader2.getNumberOfPages() + 1;
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        document.open();
        for (int i = 0; i < list1.size(); i++) {
            for (int j = offsets1[i]; j < offsets1[i + 1]; j++) {
                copy.addPage(copy.getImportedPage(reader1, j));
            }
            for (int j = offsets2[i]; j < offsets2[i + 1]; j++) {
                copy.addPage(copy.getImportedPage(reader2, j));
            }
        }
        document.close();
    } catch (IOException e) {
        LOGGER.warn("IOException: " + e);
    } catch (DocumentException e) {
        LOGGER.warn("IOException: " + e);
    }
}

From source file:classroom.filmfestival_c.Movies23.java

public static void fillForm(String filename, AccreditationData data, OutputStream out)
        throws IOException, DocumentException {

    PdfReader reader = new PdfReader(filename);
    PdfStamper stamper = new PdfStamper(reader, out);

    AcroFields form = stamper.getAcroFields();
    form.setField("name", data.getName());
    form.setFieldProperty("type", "textcolor", data.getTypeColor(), null);
    form.setField("type", data.getTypeName());
    form.setField("number", data.getNumber(false));
    form.setFieldProperty("filmfestival", "bgcolor", data.getTypeColor(), null);
    form.regenerateField("filmfestival");

    if (data.getPhoto() != null) {
        PushbuttonField bt = form.getNewPushbuttonFromField("photo");
        bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        bt.setProportionalIcon(true);//from w w w .j  av a2s  . c o  m
        bt.setImage(data.getPhoto());
        form.replacePushbuttonField("photo", bt.getField());
    }

    try {
        BarcodeInter25 code = new BarcodeInter25();
        code.setGenerateChecksum(true);
        code.setBarHeight(mm2pt(3));
        code.setCode(data.getNumber(true));
        code.setFont(null);
        PdfContentByte cb = new PdfContentByte(stamper.getWriter());
        PdfTemplate template = code.createTemplateWithBarcode(cb, null, null);
        PushbuttonField bt = form.getNewPushbuttonFromField("barcode");
        bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        bt.setProportionalIcon(false);
        bt.setTemplate(template);
        form.replacePushbuttonField("barcode", bt.getField());
    } catch (Exception e) {
        // not a valid code, do nothing
    }

    stamper.setFormFlattening(data.isFlatten());
    stamper.close();
}

From source file:classroom.filmfestival_c.Movies24.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    Session session = null;/*w w  w .  jav  a 2  s. c  o m*/
    try {
        session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery("from FilmTitle order by title");
        List<FilmTitle> results = q.list();
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT));
        document.open();
        PdfReader reader;
        PdfStamper stamper;
        AcroFields form;
        ByteArrayOutputStream baos;
        for (FilmTitle movie : results) {
            baos = new ByteArrayOutputStream();
            reader = new PdfReader(DATASHEET);
            stamper = new PdfStamper(reader, baos);
            form = stamper.getAcroFields();
            form.setField("title", movie.getTitle());
            form.setField("director", getDirectors(movie));
            form.setField("year", String.valueOf(movie.getYear()));
            form.setField("duration", String.valueOf(movie.getDuration()));
            form.setField("category", "c" + getCategory(movie));
            Set<FestivalScreening> screenings = (Set<FestivalScreening>) movie.getFestivalScreenings();
            for (FestivalScreening screening : screenings) {
                form.setField(screening.getId().getPlace(), "Yes");
            }
            stamper.setFormFlattening(true);
            stamper.close();
            reader = new PdfReader(baos.toByteArray());
            copy.addPage(copy.getImportedPage(reader, 1));
        }
        document.close();
    } catch (HibernateException e) {
        LOGGER.warn("HibernateException: " + e);
    } catch (IOException e) {
        LOGGER.warn("IOException: " + e);
    } catch (DocumentException e) {
        LOGGER.warn("DocumentException: " + e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (HibernateException e) {
            LOGGER.warn("HibernateTest - Closing session: " + e);
        }
    }
}

From source file:classroom.filmfestival_c.Movies25.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    createTemplate();//w w  w  .j ava2s  . com
    Session session = (Session) MySessionFactory.currentSession();
    Query q = session.createQuery("from FilmTitle order by title");
    java.util.List<FilmTitle> results = q.list();
    try {
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT));
        document.open();

        PdfReader reader;
        PdfStamper stamper = null;
        ByteArrayOutputStream baos = null;
        AcroFields form = null;
        int count = 0;
        for (FilmTitle movie : results) {
            if (count == 0) {
                baos = new ByteArrayOutputStream();
                reader = new PdfReader(BACKGROUND);
                stamper = new PdfStamper(reader, baos);
                stamper.setFormFlattening(true);
                form = stamper.getAcroFields();
            }
            count++;
            byte[] pdf = createPdf(movie);
            reader = new PdfReader(pdf);
            PdfImportedPage page = stamper.getImportedPage(reader, 1);
            PushbuttonField bt = form.getNewPushbuttonFromField("movie_" + count);
            bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
            bt.setProportionalIcon(true);
            bt.setTemplate(page);
            form.replacePushbuttonField("movie_" + count, bt.getField());
            if (count == 16) {
                stamper.close();
                reader = new PdfReader(baos.toByteArray());
                copy.addPage(copy.getImportedPage(reader, 1));
                count = 0;
            }
        }
        if (count > 0) {
            stamper.close();
            reader = new PdfReader(baos.toByteArray());
            copy.addPage(copy.getImportedPage(reader, 1));
            count = 0;
        }
        document.close();
    } catch (IOException ioe) {
        LOGGER.error("IOException: ", ioe);
    } catch (DocumentException de) {
        LOGGER.error("DocumentException: ", de);
    }
}

From source file:classroom.filmfestival_c.Movies25.java

public static byte[] createPdf(FilmTitle movie) throws IOException, DocumentException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfReader reader = new PdfReader(TEMPLATE);
    PdfStamper stamper = new PdfStamper(reader, baos);
    AcroFields form = stamper.getAcroFields();

    File file = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg");
    if (file.exists()) {
        PushbuttonField bt = form.getNewPushbuttonFromField(POSTER);
        bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        bt.setProportionalIcon(true);/* w w  w.  ja  v a  2 s  .  c o m*/
        bt.setImage(Image.getInstance(file.getPath()));
        form.replacePushbuttonField(POSTER, bt.getField());
    }

    String s = createHtml(movie);
    PdfContentByte canvas = stamper.getOverContent(1);
    float size = 12;
    float[] f = form.getFieldPositions(TEXT);
    while (addText(s, canvas, f, size, true) && size > 6) {
        size -= 0.2;
    }
    addText(s, canvas, f, size, false);

    form.setField(YEAR, String.valueOf(movie.getYear()));

    stamper.setFormFlattening(true);
    stamper.close();
    return baos.toByteArray();
}

From source file:classroom.intro.HelloWorld09.java

public static void main(String[] args) {
    Document.compress = false;/*from w  w  w . j a v  a2 s  .c  o m*/
    BaseFont bf = null;
    // step 1
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT1));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.moveText(88.66f, 788);
        cb.showText("ld");
        cb.moveText(-22f, 0);
        cb.showText("Wor");
        cb.endText();
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();

    try {
        PdfReader reader = new PdfReader(RESULT1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2));
        PdfContentByte cb1 = stamper.getUnderContent(1);
        cb1.beginText();
        cb1.setFontAndSize(bf, 12);
        cb1.setTextMatrix(51.33f, 788);
        cb1.showText("llo");
        cb1.endText();
        PdfContentByte cb2 = stamper.getOverContent(1);
        PdfTemplate tmp = cb2.createTemplate(250, 25);
        tmp.beginText();
        tmp.setFontAndSize(bf, 12);
        tmp.moveText(0, 7);
        tmp.showText("He");
        tmp.endText();
        cb2.addTemplate(tmp, 36, 781);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}