PDFBox - PageDrawer在PdfPagePanel之外绘制

时间:2013-01-08 16:12:44

标签: java pdfbox

我正在使用PDFBox在我的应用程序中显示生成的pdf文件。 我将PdfPagePanel添加到JScrollPane。当我缩小窗口并显示滚动条时,PageDrawer将绘制到滚动条上。

PDFReader(PDFBox的可执行示例)中也会出现此效果。 打开pdf文件 - >减小窗口的大小 - > pdf-content得到了滚动条

有人知道解决这个问题的方法吗?

环境: PDFBox 1.7.1,Windows 7,Java 1.6.31

提前致谢, 的Sascha

3 个答案:

答案 0 :(得分:1)

我在1.8.1中仍有同样的奇怪问题。

我的解决方案是将页面转换为BufferedImage,如下所示:

    PDDocument document = PDDocument.load(new File("C:\\Temp\\stuff.pdf"));             
    List<PDPage> allPages = document.getDocumentCatalog().getAllPages();

    PDPage firstPage = allPages.get(0);
    BufferedImage bi = firstPage.convertToImage();

然后创建一个普通的JPanel并在那里显示图像...

我希望有所帮助。虽然奇怪的问题!祝你好运!

答案 1 :(得分:0)

您可以使用裁剪框。

page.setCropBox(new PDRectangle(panel.getWidth(), panel.getHeight()));

但这意味着每次面板更改大小时都需要设置裁剪框。 我无法使用滚动窗格正常工作。

答案 2 :(得分:0)

PageDrawer的编写者正在使用setClip()而不与先前的剪辑设置交叉(例如来自Swing)。这就是为什么他们使用PageDrawer在窗口的右侧和底部写下所有其他元素。我写了一个Graphics委托作为解决方法。它很长,但很容易使用:

    PageDrawer drawer = new PageDrawer();

    try
    {
        drawer.drawPage(new StrictClip(g2D), m_page, dim);
    }
    finally
    {
        drawer.dispose();
    }

现在是StrictClip类本身:

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.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.text.AttributedCharacterIterator;
import java.util.Map;

public class StrictClip extends Graphics2D
{
    protected Graphics2D m_g2D;

    public StrictClip(Graphics2D g2D)
    {
        m_g2D = g2D;
    }

    @Override
    public void draw(Shape s)
    {
        m_g2D.draw(s);
    }

    @Override
    public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
    {
        return m_g2D.drawImage(img, xform, obs);
    }

    @Override
    public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
    {
        m_g2D.drawImage(img, op, x, y);
    }

    @Override
    public void drawRenderedImage(RenderedImage img, AffineTransform xform)
    {
        m_g2D.drawRenderedImage(img, xform);
    }

    @Override
    public void drawRenderableImage(RenderableImage img, AffineTransform xform)
    {
        m_g2D.drawRenderableImage(img, xform);
    }

    @Override
    public void drawString(String str, int x, int y)
    {
        m_g2D.drawString(str, x, y);
    }

    @Override
    public void drawString(String str, float x, float y)
    {
        m_g2D.drawString(str, x, y);
    }

    @Override
    public void drawString(AttributedCharacterIterator iterator, int x, int y)
    {
        m_g2D.drawString(iterator, x, y);
    }

    @Override
    public void drawString(AttributedCharacterIterator iterator, float x, float y)
    {
        m_g2D.drawString(iterator, x, y);
    }

    @Override
    public void drawGlyphVector(GlyphVector g, float x, float y)
    {
        m_g2D.drawGlyphVector(g, x, y);
    }

    @Override
    public void fill(Shape s)
    {
        m_g2D.fill(s);
    }

    @Override
    public boolean hit(Rectangle rect, Shape s, boolean onStroke)
    {
        return m_g2D.hit(rect, s, onStroke);
    }

    @Override
    public GraphicsConfiguration getDeviceConfiguration()
    {
        return m_g2D.getDeviceConfiguration();
    }

    @Override
    public void setComposite(Composite comp)
    {
        m_g2D.setComposite(comp);
    }

    @Override
    public void setPaint(Paint paint)
    {
        m_g2D.setPaint(paint);
    }

    @Override
    public void setStroke(Stroke s)
    {
        m_g2D.setStroke(s);
    }

    @Override
    public void setRenderingHint(Key hintKey, Object hintValue)
    {
        m_g2D.setRenderingHint(hintKey, hintValue);
    }

    @Override
    public Object getRenderingHint(Key hintKey)
    {
        return m_g2D.getRenderingHint(hintKey);
    }

    @Override
    public void setRenderingHints(Map<?, ?> hints)
    {
        m_g2D.setRenderingHints(hints);
    }

    @Override
    public void addRenderingHints(Map<?, ?> hints)
    {
        m_g2D.addRenderingHints(hints);
    }

    @Override
    public RenderingHints getRenderingHints()
    {
        return m_g2D.getRenderingHints();
    }

    @Override
    public void translate(int x, int y)
    {
        m_g2D.translate(x, y);
    }

    @Override
    public void translate(double tx, double ty)
    {
        m_g2D.translate(tx, ty);
    }

    @Override
    public void rotate(double theta)
    {
        m_g2D.rotate(theta);
    }

    @Override
    public void rotate(double theta, double x, double y)
    {
        m_g2D.rotate(theta, x, y);
    }

    @Override
    public void scale(double sx, double sy)
    {
        m_g2D.scale(sx, sy);
    }

    @Override
    public void shear(double shx, double shy)
    {
        m_g2D.shear(shx, shy);
    }

    @Override
    public void transform(AffineTransform Tx)
    {
        m_g2D.transform(Tx);
    }

    @Override
    public void setTransform(AffineTransform Tx)
    {
        m_g2D.setTransform(Tx);
    }

    @Override
    public AffineTransform getTransform()
    {
        return m_g2D.getTransform();
    }

    @Override
    public Paint getPaint()
    {
        return m_g2D.getPaint();
    }

    @Override
    public Composite getComposite()
    {
        return m_g2D.getComposite();
    }

    @Override
    public void setBackground(Color color)
    {
        m_g2D.setBackground(color);
    }

    @Override
    public Color getBackground()
    {
        return m_g2D.getBackground();
    }

    @Override
    public Stroke getStroke()
    {
        return m_g2D.getStroke();
    }

    @Override
    public FontRenderContext getFontRenderContext()
    {
        return m_g2D.getFontRenderContext();
    }

    @Override
    public Graphics create()
    {
        return new StrictClip((StrictClip)m_g2D.create());
    }

    @Override
    public Color getColor()
    {
        return m_g2D.getColor();
    }

    @Override
    public void setColor(Color c)
    {
        m_g2D.setColor(c);
    }

    @Override
    public void setPaintMode()
    {
        m_g2D.setPaintMode();
    }

    @Override
    public void setXORMode(Color c1)
    {
        m_g2D.setXORMode(c1);
    }

    @Override
    public Font getFont()
    {
        return m_g2D.getFont();
    }

    @Override
    public void setFont(Font font)
    {
        m_g2D.setFont(font);
    }

    @Override
    public FontMetrics getFontMetrics(Font f)
    {
        return m_g2D.getFontMetrics(f);
    }

    @Override
    public Rectangle getClipBounds()
    {
        return m_g2D.getClipBounds();
    }

    @Override
    public Shape getClip()
    {
        return m_g2D.getClip();
    }

    @Override
    public void clip(Shape clip)
    {
        m_g2D.clip(clip);
    }

    @Override
    public void setClip(Shape clip)
    {
        Shape clipDev = m_g2D.getClip();
        m_g2D.setClip(null);

        if (clip != null && clipDev != null)
        {
            Rectangle rectClip = clipDev.getBounds(),
                rectClipUser = clip.getBounds();

            m_g2D.clip(rectClip.createIntersection(rectClipUser));
        }

        m_g2D.setClip(clipDev);
    }

    @Override
    public void clipRect(int x, int y, int width, int height)
    {
        m_g2D.clipRect(x, y, width, height);
    }

    @Override
    public void setClip(int x, int y, int width, int height)
    {
        setClip(new Rectangle(x, y, width, height));
    }

    @Override
    public void copyArea(int x, int y, int width, int height, int dx, int dy)
    {
        m_g2D.copyArea(x, y, width, height, dx, dy);
    }

    @Override
    public void drawLine(int x1, int y1, int x2, int y2)
    {
        m_g2D.drawLine(x1, y1, x2, y2);
    }

    @Override
    public void fillRect(int x, int y, int width, int height)
    {
        m_g2D.fillRect(x, y, width, height);
    }

    @Override
    public void clearRect(int x, int y, int width, int height)
    {
        m_g2D.clearRect(x, y, width, height);
    }

    @Override
    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
    {
        m_g2D.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
    }

    @Override
    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
    {
        m_g2D.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
    }

    @Override
    public void drawOval(int x, int y, int width, int height)
    {
        m_g2D.drawOval(x, y, width, height);
    }

    @Override
    public void fillOval(int x, int y, int width, int height)
    {
        m_g2D.fillOval(x, y, width, height);
    }

    @Override
    public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
    {
        m_g2D.drawArc(x, y, width, height, startAngle, arcAngle);
    }

    @Override
    public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
    {
        m_g2D.fillArc(x, y, width, height, startAngle, arcAngle);
    }

    @Override
    public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
    {
        m_g2D.drawPolyline(xPoints, yPoints, nPoints);
    }

    @Override
    public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
    {
        m_g2D.drawPolygon(xPoints, yPoints, nPoints);
    }

    @Override
    public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
    {
        m_g2D.fillPolygon(xPoints, yPoints, nPoints);
    }

    @Override
    public boolean drawImage(Image img, int x, int y, ImageObserver observer)
    {
        return m_g2D.drawImage(img, x, y, observer);
    }

    @Override
    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
    {
        return m_g2D.drawImage(img, x, y, width, height, observer);
    }

    @Override
    public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
    {
        return m_g2D.drawImage(img, x, y, bgcolor, observer);
    }

    @Override
    public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
    {
        return m_g2D.drawImage(img, x, y, width, height, bgcolor, observer);
    }

    @Override
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
    {
        return m_g2D.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
    }

    @Override
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
    {
        return m_g2D.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
    }

    @Override
    public void dispose()
    {
        m_g2D.dispose();
    }

}

希望这有帮助。

此致 汉斯