问题
I'm using PDFBox to show a generated pdf-file in my application. I added the PdfPagePanel to a JScrollPane. When I make the window smaller and the scrollbars appeare, the PageDrawer draws onto the scrollbars.
This effects also occurs in the PDFReader (executable example of PDFBox). open a pdf-file -> reduce size of window -> pdf-content get's drawn over the scrollbars
Does anybody know a way to fix this?
Enviroment: PDFBox 1.7.1, Windows 7, Java 1.6.31
Thanks in advance, Sascha
回答1:
I still have the same weird problem in 1.8.1.
My solution is to convert the page into a BufferedImage like this:
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();
Then create a normal JPanel and display the image there as you like it...
I hope that helps. Weird issue though! Good luck!
回答2:
You can use the crop box for this.
page.setCropBox(new PDRectangle(panel.getWidth(), panel.getHeight()));
But is means you need to set the crop box everytime your panel changes size. And i can't get it to work correctly with scrollpanes.
回答3:
The writers of PageDrawer are using setClip() without intersection with previous clip settings (e.g. from Swing). That's why they write over all other elements at the right and at the bottom of the window using PageDrawer. I wrote a Graphics delegate as a workaround. It is pretty long, but easy to use that way:
PageDrawer drawer = new PageDrawer();
try
{
drawer.drawPage(new StrictClip(g2D), m_page, dim);
}
finally
{
drawer.dispose();
}
And now the StrictClip class itself:
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();
}
}
Hope this helps.
Regards Hans
来源:https://stackoverflow.com/questions/14219276/pdfbox-pagedrawer-draws-outside-the-pdfpagepanel