Example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream getStream

List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream getStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceStream getStream.

Prototype

public final PDStream getStream() 

Source Link

Document

Returns the stream.

Usage

From source file:airviewer.TextInAnnotationReplacer.java

License:Apache License

static void replaceText(PDDocument document, PDAnnotation anAnnotation, String newContents) {

    if (null != anAnnotation.getAppearance() && null != anAnnotation.getAppearance().getNormalAppearance()) {
        try {/* www .  ja  v a 2s. c  om*/
            PDAppearanceStream annotationAppearanceStream = anAnnotation.getAppearance().getNormalAppearance()
                    .getAppearanceStream();

            PDFStreamParser parser = new PDFStreamParser(annotationAppearanceStream);
            parser.parse();
            List<Object> tokens = parser.getTokens();
            for (int j = 0; j < tokens.size(); j++) {
                Object next = tokens.get(j);
                if (next instanceof Operator) {
                    Operator op = (Operator) next;
                    //Tj and TJ are the two operators that display strings in a PDF
                    if (op.getName().equals("Tj")) {
                        // Tj takes one operand and that is the string to display so lets update that operator
                        COSString previous = (COSString) tokens.get(j - 1);
                        previous.setValue(newContents.getBytes(Charset.forName("UTF-8")));
                    } else if (op.getName().equals("TJ")) {
                        COSArray previous = (COSArray) tokens.get(j - 1);
                        for (int k = 0; k < previous.size(); k++) {
                            Object arrElement = previous.getObject(k);
                            if (arrElement instanceof COSString) {
                                COSString cosString = (COSString) arrElement;
                                cosString.setValue(newContents.getBytes(Charset.forName("UTF-8")));
                            }
                        }
                    }
                }
            }

            try (OutputStream out = annotationAppearanceStream.getStream().createOutputStream()) {
                ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
                tokenWriter.writeTokens(tokens);
            }

            anAnnotation.getAppearance().setNormalAppearance(annotationAppearanceStream);
        } catch (IOException ex) {
            Logger.getLogger(TextInAnnotationReplacer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.VectorGraphicsExtractor.java

License:Apache License

@Override
public MPage extract(PDPage pageToExtract, MPage pageContent) {
    try {/*w  w w.  j av a 2  s . co m*/
        if (pageToExtract.getContents() == null) {
            throw new ExtractorException("Contents is null.");
        }

        pageSize = pageToExtract.findMediaBox().createDimension();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        // initialize the used stroke with CAP_BUTT instead of CAP_SQUARE
        graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
        // Only if there is some content, we have to process it.
        // Otherwise we are done here and we will produce an empty page

        PDResources resources = pageToExtract.findResources();
        processStream(pageToExtract, resources, pageToExtract.getContents().getStream());

        List<PDAnnotation> annotations = pageToExtract.getAnnotations();
        for (PDAnnotation annotation : annotations) {
            PDAnnotation annot = (PDAnnotation) annotation;
            PDRectangle rect = annot.getRectangle();
            String appearanceName = annot.getAppearanceStream();
            PDAppearanceDictionary appearDictionary = annot.getAppearance();
            if (appearDictionary != null) {
                if (appearanceName == null) {
                    appearanceName = "default";
                }
                Map<String, PDAppearanceStream> appearanceMap = appearDictionary.getNormalAppearance();
                if (appearanceMap != null) {
                    PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
                    if (appearance != null) {
                        Point2D point = new Point2D.Float(rect.getLowerLeftX(), rect.getLowerLeftY());
                        Matrix matrix = appearance.getMatrix();
                        if (matrix != null) {
                            // transform the rectangle using the given matrix
                            AffineTransform at = matrix.createAffineTransform();
                            at.transform(point, point);
                        }
                        graphics.translate((int) point.getX(), -(int) point.getY());
                        processSubStream(pageToExtract, appearance.getResources(), appearance.getStream());
                        graphics.translate(-(int) point.getX(), (int) point.getY());
                    }
                }
            }
        }
        return handleResult(graphics, pageContent);
    } catch (IOException ex) {
        throw new ExtractorException("Failed to extract vector graphics.", ex);
    }
}

From source file:org.apache.pdflens.views.pagesview.PageDrawer.java

License:Apache License

/**
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.//from   w  ww .j  av a  2  s  .c o m
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Graphics g, PDPage p, Dimension pageDimension) throws IOException {
    graphics = (Graphics2D) g;
    page = p;
    pageSize = pageDimension;
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // Only if there is some content, we have to process it. 
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }
    List annotations = page.getAnnotations();
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        PDRectangle rect = annot.getRectangle();
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map appearanceMap = appearDictionary.getNormalAppearance();
            PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
            if (appearance != null) {
                g.translate((int) rect.getLowerLeftX(), (int) -rect.getLowerLeftY());
                processSubStream(page, appearance.getResources(), appearance.getStream());
                g.translate((int) -rect.getLowerLeftX(), (int) +rect.getLowerLeftY());
            }
        }
    }

}

From source file:org.xmlcml.pdf2svg.PDFPage2SVGConverter.java

License:Apache License

/**
 * DUPLICATE OF SUPER SO WE CAN DEBUG//from   w w w .ja  v  a2 s . co  m
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Graphics g, PDPage p, Dimension pageDimension) throws IOException {
    super.drawPage(g, p, pageDimension);
    // cannot use this because private
    // graphics = (Graphics2D)g;
    Graphics2D g2d = (Graphics2D) g;
    //           g2d = (Graphics2D)g;
    page = p;
    pageSize = pageDimension;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // Only if there is some content, we have to process it. 
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }
    List annotations = page.getAnnotations();
    if (annotations.size() > 0) {
        throw new RuntimeException("ANNOTATIONS");
    }
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        PDRectangle rect = annot.getRectangle();
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map appearanceMap = appearDictionary.getNormalAppearance();
            if (appearanceMap != null) {
                PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
                if (appearance != null) {
                    g.translate((int) rect.getLowerLeftX(), (int) -rect.getLowerLeftY());
                    processSubStream(page, appearance.getResources(), appearance.getStream());
                    g.translate((int) -rect.getLowerLeftX(), (int) +rect.getLowerLeftY());
                }
            }
        }
    }
}

From source file:pdfimport.pdfbox.PageDrawer.java

License:Apache License

/**
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.// ww w . j  a  v a 2  s  . co m
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(GraphicsProcessor g, PDPage p) throws IOException {
    graphics = g;
    page = p;
    // Only if there is some content, we have to process it.
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }

    List<?> annotations = page.getAnnotations();
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map<?, ?> appearanceMap = appearDictionary.getNormalAppearance();
            if (appearanceMap != null) {
                PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
                if (appearance != null) {
                    processSubStream(page, appearance.getResources(), appearance.getStream());
                }
            }
        }
    }

}