Example usage for com.itextpdf.text Image getImageMask

List of usage examples for com.itextpdf.text Image getImageMask

Introduction

In this page you can find the example usage for com.itextpdf.text Image getImageMask.

Prototype

public Image getImageMask() 

Source Link

Document

Gets the explicit masking.

Usage

From source file:cz.muni.pdfjbim.PdfImageProcessor.java

License:Apache License

/**
 * replace images by they recompressed version according to JBIG2 standard
 * positions and image data given in imagesData
 * @param pdfName represents name of original PDF file
 * @param os represents output stream for writing changed PDF file
 * @param imagesData contains compressed images according to JBIG2 standard and informations about them
 * @throws PdfRecompressionException if version of PDF is lower than 1.4 or was catch DocumentException or IOException
 *///from   w ww  . ja  va 2 s  . c  om
public void replaceImageUsingIText(String pdfName, OutputStream os, Jbig2ForPdf imagesData)
        throws PdfRecompressionException {
    if (pdfName == null) {
        throw new NullPointerException("pdfName");
    }

    if (os == null) {
        throw new NullPointerException("os");
    }

    if (imagesData == null) {
        throw new NullPointerException("imagesData is null => nothing to recompress");
    }

    Map<PdfObjId, PdfImage> jbig2Images = imagesData.getMapOfJbig2Images();

    PdfReader pdf;
    PdfStamper stp = null;
    try {
        pdf = new PdfReader(pdfName);
        stp = new PdfStamper(pdf, os);
        PdfWriter writer = stp.getWriter();

        int version;
        if ((version = Integer.parseInt(String.valueOf(pdf.getPdfVersion()))) < 4) {
            writer.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
        }

        Iterator itImages = jbig2Images.values().iterator();
        String key;
        if (itImages.hasNext()) {
            PdfImage myImg = (PdfImage) itImages.next();
            key = myImg.getPdfImageInformation().getKey();
        } else {
            key = "im0";
        }

        for (int pageNum = 1; pageNum <= pdf.getNumberOfPages(); pageNum++) {

            PdfDictionary pg = pdf.getPageN(pageNum);
            PdfDictionary resPg = (PdfDictionary) PdfReader.getPdfObject(pg.get(PdfName.RESOURCES));

            PdfDictionary xobjResPg = (PdfDictionary) PdfReader.getPdfObject(resPg.get(PdfName.XOBJECT));

            PdfObject obj = null;
            if (xobjResPg != null) {
                for (Iterator it = xobjResPg.getKeys().iterator(); it.hasNext();) {
                    PdfObject pdfObjIndirect = xobjResPg.get((PdfName) it.next());
                    if (pdfObjIndirect.isIndirect()) {
                        PdfDictionary pdfObj2 = (PdfDictionary) PdfReader.getPdfObject(pdfObjIndirect);
                        PdfDictionary xobj2Res = (PdfDictionary) PdfReader
                                .getPdfObject(pdfObj2.get(PdfName.RESOURCES));
                        if (xobj2Res != null) {
                            for (Iterator it2 = xobj2Res.getKeys().iterator(); it2.hasNext();) {
                                PdfObject resObj = xobj2Res.get((PdfName) it2.next());
                            }
                            PdfDictionary xobj = (PdfDictionary) PdfReader
                                    .getPdfObject(xobj2Res.get(PdfName.XOBJECT));
                            if (xobj == null) {
                                continue;
                            }
                            obj = xobj.get(new PdfName(key));
                        } else {
                            obj = xobjResPg.get(new PdfName(key));
                            if (obj == null) {
                                obj = pdfObjIndirect;
                            }
                        }
                    }
                }
            }

            if ((obj != null) && (obj.isIndirect())) {

                PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj);
                if (tg == null) {
                    continue;
                }
                PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
                if (PdfName.IMAGE.equals(type)) {
                    PRIndirectReference ref = (PRIndirectReference) obj;
                    PdfObjId imId = new PdfObjId(ref.getNumber(), ref.getGeneration());
                    PdfImage jbImage = jbig2Images.get(imId);
                    if (jbImage == null) {
                        continue;
                    }
                    PdfImageInformation jbImageInfo = jbImage.getPdfImageInformation();
                    Image img = Image.getInstance(jbImageInfo.getWidth(), jbImageInfo.getHeight(),
                            jbImage.getImageData(), imagesData.getGlobalData());

                    PdfReader.killIndirect(obj);
                    Image maskImage = img.getImageMask();

                    if (maskImage != null) {
                        writer.addDirectImageSimple(maskImage);
                    }
                    writer.addDirectImageSimple(img, (PRIndirectReference) obj);
                }
            }
        }
        stp.close();
    } catch (IOException ioEx) {
        throw new PdfRecompressionException(ioEx);
    } catch (DocumentException dEx) {
        throw new PdfRecompressionException(dEx);
    } finally {
        Tools.deleteFilesFromList(imagesData.getJbFiles().toArray(new File[0]));
    }
}

From source file:cz.muni.pdfjbim.PdfImageReplacer.java

License:Apache License

/**
 * replace images by they recompressed version according to JBIG2 standard positions and image
 * data given in imagesData//ww  w  . j a v  a2  s  .c o  m
 *
 * @param originalPdf represents name of original PDF file
 * @param os represents output stream for writing changed PDF file
 * @param imagesData contains compressed images according to JBIG2 standard and informations
 * about them
 * @throws PdfRecompressionException if version of PDF is lower than 1.4 or was catch
 * DocumentException or IOException
 */
public void replaceImageUsingIText(InputStream originalPdf, OutputStream os, List<Jbig2ForPdf> imagesDataList)
        throws PdfRecompressionException {
    if (originalPdf == null) {
        throw new NullPointerException("pdfName");
    }

    if (os == null) {
        throw new NullPointerException("os");
    }

    if (imagesDataList == null) {
        throw new NullPointerException("imagesData is null => nothing to recompress");
    }

    log.info("Replacing old images in PDF with their equivalent encoded according to standard JBIG2");
    PdfReader pdf;
    PdfStamper stp = null;
    try {
        pdf = new PdfReader(originalPdf);
        stp = new PdfStamper(pdf, os);
        PdfWriter writer = stp.getWriter();

        int version;
        if ((version = Integer.parseInt(String.valueOf(pdf.getPdfVersion()))) < 4) {
            log.debug("PDF version of original PDF was {} => changing to PDF version 1.4", pdf.getPdfVersion());
            writer.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
        }

        for (Jbig2ForPdf imagesData : imagesDataList) {

            Map<PdfObjId, PdfImage> jbig2Images = imagesData.getMapOfJbig2Images();

            Iterator itImages = jbig2Images.values().iterator();
            String key;
            if (itImages.hasNext()) {
                PdfImage myImg = (PdfImage) itImages.next();
                key = myImg.getPdfImageInformation().getKey();
            } else {
                key = "im0";
            }

            for (int pageNum = 1; pageNum <= pdf.getNumberOfPages(); pageNum++) {

                PdfDictionary pg = pdf.getPageN(pageNum);
                PdfDictionary resPg = (PdfDictionary) PdfReader.getPdfObject(pg.get(PdfName.RESOURCES));

                PdfDictionary xobjResPg = (PdfDictionary) PdfReader.getPdfObject(resPg.get(PdfName.XOBJECT));

                PdfObject obj = null;
                if (xobjResPg != null) {
                    for (Iterator it = xobjResPg.getKeys().iterator(); it.hasNext();) {
                        PdfObject pdfObjIndirect = xobjResPg.get((PdfName) it.next());
                        if (pdfObjIndirect.isIndirect()) {
                            PdfDictionary pdfObj2 = (PdfDictionary) PdfReader.getPdfObject(pdfObjIndirect);
                            PdfDictionary xobj2Res = (PdfDictionary) PdfReader
                                    .getPdfObject(pdfObj2.get(PdfName.RESOURCES));
                            if (xobj2Res != null) {
                                for (Iterator it2 = xobj2Res.getKeys().iterator(); it2.hasNext();) {
                                    PdfObject resObj = xobj2Res.get((PdfName) it2.next());
                                }
                                PdfDictionary xobj = (PdfDictionary) PdfReader
                                        .getPdfObject(xobj2Res.get(PdfName.XOBJECT));
                                if (xobj == null) {
                                    continue;
                                }
                                obj = xobj.get(new PdfName(key));
                            } else {
                                obj = xobjResPg.get(new PdfName(key));
                                if (obj == null) {
                                    obj = pdfObjIndirect;
                                }
                            }
                        }
                    }
                }

                if ((obj != null) && (obj.isIndirect())) {

                    PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj);
                    if (tg == null) {
                        continue;
                    }
                    PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
                    if (PdfName.IMAGE.equals(type)) {
                        PRIndirectReference ref = (PRIndirectReference) obj;
                        PdfObjId imId = new PdfObjId(ref.getNumber(), ref.getGeneration());
                        PdfImage jbImage = jbig2Images.get(imId);
                        if (jbImage == null) {
                            continue;
                        }

                        log.debug("Replacing image {}", jbImage);
                        PdfImageInformation jbImageInfo = jbImage.getPdfImageInformation();
                        Image img = Image.getInstance(jbImageInfo.getWidth(), jbImageInfo.getHeight(),
                                jbImage.getImageData(), imagesData.getGlobalData());

                        PdfReader.killIndirect(obj);
                        Image maskImage = img.getImageMask();

                        if (maskImage != null) {
                            writer.addDirectImageSimple(maskImage);
                        }
                        writer.addDirectImageSimple(img, (PRIndirectReference) obj);
                    }
                }
            }
        }
    } catch (IOException ioEx) {
        throw new PdfRecompressionException(ioEx);
    } catch (DocumentException dEx) {
        throw new PdfRecompressionException(dEx);
    } finally {
        log.debug("Deleting temporary files created during process of PDF recompression");
        for (Jbig2ForPdf imagesData : imagesDataList) {
            Tools.deleteFilesFromList(imagesData.getJbFiles().toArray(new File[0]));
        }
        try {
            if (stp != null) {
                stp.close();
            }
        } catch (DocumentException ex) {
            log.error("Exception thrown while closing stream", ex);
        } catch (IOException ex) {
            log.error("Exception thrown while closing stream", ex);
        }
    }

}