Example usage for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceDictionary getNormalAppearance

List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAppearanceDictionary getNormalAppearance

Introduction

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

Prototype

public PDAppearanceEntry getNormalAppearance() 

Source Link

Document

This will return a list of appearances.

Usage

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

License:Apache License

@Override
public MPage extract(PDPage pageToExtract, MPage pageContent) {
    try {//ww w  .j  ava2 s.  c  o  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 w  w  .  ja  va2  s  . c om*/
 * @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 ww  . jav a  2s  .  c o  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.//from  ww  w.  ja  v a2 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(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());
                }
            }
        }
    }

}