2

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

这种效果也出现在 PDFReader(PDFBox 的可执行示例)中。打开一个pdf文件->减小窗口大小->在滚动条上绘制pdf内容

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

环境:PDFBox 1.7.1,Windows 7,Java 1.6.31

在此先感谢,萨沙

4

3 回答 3

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 并在那里显示你喜欢的图像......

我希望这会有所帮助。不过奇怪的问题!祝你好运!

于 2013-05-14T14:08:06.843 回答
0

您可以为此使用裁剪框。

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

但这意味着您每次面板更改大小时都需要设置裁剪框。而且我无法让它与滚动窗格一起正常工作。

于 2013-09-27T13:42:25.623 回答
0

PageDrawer 的编写者使用 setClip() 与之前的剪辑设置(例如来自 Swing)没有交集。这就是他们使用 PageDrawer 覆盖窗口右侧和底部的所有其他元素的原因。我写了一个图形委托作为一种解决方法。它很长,但很容易使用:

    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();
    }

}

希望这可以帮助。

问候汉斯

于 2013-12-20T17:08:52.390 回答