⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfgraphics2d.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright 2002 by Jim Moore <jim@scolamoore.com>. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Composite;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Paint;import java.awt.GradientPaint;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.Shape;import java.awt.Stroke;import java.awt.Transparency;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.font.TextLayout;import java.awt.geom.AffineTransform;import java.awt.geom.Arc2D;import java.awt.geom.Area;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.awt.geom.PathIterator;import java.awt.geom.Rectangle2D;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.awt.image.ColorModel;import java.awt.image.ImageObserver;import java.awt.image.RenderedImage;import java.awt.image.WritableRaster;import java.awt.image.renderable.RenderableImage;import java.awt.RenderingHints.Key;import java.awt.geom.NoninvertibleTransformException;import java.awt.geom.Point2D;import java.text.CharacterIterator;import java.text.AttributedCharacterIterator;import java.util.Map;import java.util.HashMap;import java.util.Hashtable;import java.util.ArrayList;public class PdfGraphics2D extends Graphics2D {        private static final int FILL = 1;    private static final int STROKE = 2;    private static final int CLIP = 3;        private static AffineTransform IDENTITY = new AffineTransform();        private Font font;    private BaseFont baseFont;    private float fontSize;    private AffineTransform transform;    private Paint paint;    private Color background;    private float width;    private float height;        private Area clip;        private RenderingHints rhints = new RenderingHints(null);        private Stroke stroke;        private PdfContentByte cb;        /** Storage for BaseFont objects created. */    private HashMap baseFonts;        private boolean disposeCalled = false;        private FontMapper fontMapper;        private PdfFontMetrics fontMetrics;        private ArrayList kids;        private boolean kid = false;        private Graphics dg2 = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB).getGraphics();        private boolean onlyShapes = false;    private PdfGraphics2D() {    }        /**     * Constructor for PDFGraphics2D.     *     */    PdfGraphics2D(PdfContentByte cb, float width, float height, FontMapper fontMapper) {        super();        this.transform = new AffineTransform();        this.baseFonts = new HashMap();        this.fontMapper = fontMapper;        this.kids = new ArrayList();        if (this.fontMapper == null)            this.fontMapper = new DefaultFontMapper();        paint = Color.black;        background = Color.white;        setFont(new Font("sanserif", Font.PLAIN, 12));        stroke = new BasicStroke(1);        this.cb = cb;        cb.saveState();        this.width = width;        this.height = height;        clip = new Area(new Rectangle2D.Float(0, 0, width, height));        clip(clip);        cb.saveState();    }        PdfGraphics2D(PdfContentByte cb, float width, float height) {        super();        this.onlyShapes = true;        this.transform = new AffineTransform();        this.kids = new ArrayList();        paint = Color.black;        background = Color.white;        setFont(new Font("sanserif", Font.PLAIN, 12));        stroke = new BasicStroke(1);        this.cb = cb;        cb.saveState();        this.width = width;        this.height = height;        clip = new Area(new Rectangle2D.Float(0, 0, width, height));        clip(clip);        cb.saveState();    }        /**     * @see Graphics2D#draw(Shape)     */    public void draw(Shape s) {        followPath(s, STROKE);    }        /**     * @see Graphics2D#drawImage(Image, AffineTransform, ImageObserver)     */    public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {        return drawImage(img, null, xform, null, obs);    }        /**     * @see Graphics2D#drawImage(BufferedImage, BufferedImageOp, int, int)     */    public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {        BufferedImage result = op.createCompatibleDestImage(img, img.getColorModel());        result = op.filter(img, result);        drawImage(result, x, y, null);    }        /**     * @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform)     */    public void drawRenderedImage(RenderedImage img, AffineTransform xform) {        BufferedImage image = null;        if (img instanceof BufferedImage) {            image = (BufferedImage)img;        } else {            ColorModel cm = img.getColorModel();            int width = img.getWidth();            int height = img.getHeight();            WritableRaster raster = cm.createCompatibleWritableRaster(width, height);            boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();            Hashtable properties = new Hashtable();            String[] keys = img.getPropertyNames();            if (keys!=null) {                for (int i = 0; i < keys.length; i++) {                    properties.put(keys[i], img.getProperty(keys[i]));                }            }            BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);            img.copyData(raster);        }        drawImage(image, xform, null);    }        /**     * @see Graphics2D#drawRenderableImage(RenderableImage, AffineTransform)     */    public void drawRenderableImage(RenderableImage img, AffineTransform xform) {        drawRenderedImage(img.createDefaultRendering(), xform);    }        /**     * @see Graphics#drawString(String, int, int)     */    public void drawString(String s, int x, int y) {        drawString(s, (float)x, (float)y);    }        /**     * @see Graphics2D#drawString(String, float, float)     */    public void drawString(String s, float x, float y) {        if (onlyShapes) {            TextLayout tl = new TextLayout(s, this.font, new FontRenderContext(new AffineTransform(), false, true));            tl.draw(this, x, y);        }        else {            AffineTransform at = getTransform();            AffineTransform at2 = getTransform();            at2.translate(x, y);            at2.concatenate(font.getTransform());            setTransform(at2);            AffineTransform inverse = this.normalizeMatrix();            AffineTransform flipper = AffineTransform.getScaleInstance(1,-1);            inverse.concatenate(flipper);            double[] mx = new double[6];            inverse.getMatrix(mx);            cb.beginText();            cb.setFontAndSize(baseFont, fontSize);            cb.setTextMatrix((float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]);            cb.showText(s);            cb.endText();            setTransform(at);        }    }    /**     * @see Graphics#drawString(AttributedCharacterIterator, int, int)     */    public void drawString(AttributedCharacterIterator iterator, int x, int y) {        drawString(iterator, (float)x, (float)y);    }        /**     * @see Graphics2D#drawString(AttributedCharacterIterator, float, float)     */    public void drawString(AttributedCharacterIterator iter, float x, float y) {        StringBuffer sb = new StringBuffer();        for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {            sb.append(c);        }        drawString(sb.toString(),x,y);            }        /**     * @see Graphics2D#drawGlyphVector(GlyphVector, float, float)     */    public void drawGlyphVector(GlyphVector g, float x, float y) {        Shape s = g.getOutline(x, y);        fill(s);    }        /**     * @see Graphics2D#fill(Shape)     */    public void fill(Shape s) {        followPath(s, FILL);    }        /**     * @see Graphics2D#hit(Rectangle, Shape, boolean)     */    public boolean hit(Rectangle rect, Shape s, boolean onStroke) {        if (onStroke) {            s = stroke.createStrokedShape(s);        }        s = transform.createTransformedShape(s);        Area area = new Area(s);        if (clip != null)            area.intersect(clip);        return area.intersects(rect.x, rect.y, rect.width, rect.height);    }        /**     * @see Graphics2D#getDeviceConfiguration()     */    public GraphicsConfiguration getDeviceConfiguration() {        throw new UnsupportedOperationException();    }        /**     * @see Graphics2D#setComposite(Composite)     */    public void setComposite(Composite comp) {            }        /**     * @see Graphics2D#setPaint(Paint)     */    public void setPaint(Paint paint) {        setPaint(paint, false, 0, 0);    }        /**     * @see Graphics2D#setStroke(Stroke)     */    public void setStroke(Stroke s) {        this.stroke = s;    }        /**     * @see Graphics2D#setRenderingHint(Key, Object)     */    public void setRenderingHint(Key arg0, Object arg1) {        rhints.put(arg0, arg1);    }        /**     * @see Graphics2D#getRenderingHint(Key)     */    public Object getRenderingHint(Key arg0) {        return rhints.get(arg0);    }        /**     * @see Graphics2D#setRenderingHints(Map)     */    public void setRenderingHints(Map hints) {        rhints.clear();        rhints.putAll(hints);    }        /**     * @see Graphics2D#addRenderingHints(Map)     */    public void addRenderingHints(Map hints) {        rhints.putAll(hints);    }        /**     * @see Graphics2D#getRenderingHints()     */    public RenderingHints getRenderingHints() {        return rhints;    }        /**     * @see Graphics#translate(int, int)     */    public void translate(int x, int y) {        translate((double)x, (double)y);    }        /**     * @see Graphics2D#translate(double, double)     */    public void translate(double tx, double ty) {        transform.translate(tx,ty);    }        /**     * @see Graphics2D#rotate(double)     */    public void rotate(double theta) {        transform.rotate(theta);    }        /**     * @see Graphics2D#rotate(double, double, double)     */    public void rotate(double theta, double x, double y) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -