无边框打印

3

我正在尝试编写一个照片展示程序,但是我很难实现无边框打印。我已经接近成功,但图像不能填满4"x6"的打印纸张。希望能得到有关实现无边框打印的任何提示。

谢谢!

    final BufferedImage img = ImageIO.read(new File(image));

    // Assuming that images are going to be 300 DPI
    PrinterResolution pr = new PrinterResolution(300, 300,
        PrinterResolution.DPI);

    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(pr);

    // Set print job so the image name shows (in the print queue)
    this.pj.setJobName(new File(image).getName());

    PageFormat pf = this.pj.getPageFormat(null);
    Paper paper = pf.getPaper();
    paper.setSize(4 * 72, 6 * 72);
    paper.setImageableArea(
        0.0, 0.0,
        paper.getWidth(), paper.getHeight()
    );

    if(img.getWidth(null) > img.getHeight(null))
        pf.setOrientation(PageFormat.LANDSCAPE);
    else
        pf.setOrientation(PageFormat.PORTRAIT);

    pf.setPaper(paper);

    // Create the page
    this.pj.setPrintable(new Printable() {
        public int print(Graphics g, PageFormat pf, int i) throws 
            PrinterException {
            if (i != 0)
                return NO_SUCH_PAGE;

            double width = img.getWidth(null);
            double height = img.getHeight(null);

            double w = Math.floor(pf.getImageableWidth() - 
                pf.getImageableX()) / (width * 1.0);

            double h = Math.floor(pf.getImageableHeight() - 
                pf.getImageableY()) / (height * 1.0);

            double scale = Math.min(w, h);

            Graphics2D g2 = (Graphics2D) g;
            g2.translate(0, 0);
            g2.scale(scale, scale);
            g2.drawImage(img, 0, 0, (int)width, (int)height, null);

            return PAGE_EXISTS;
        }
    }, this.pj.validatePage(pf));

    // Get number of copies
    int nCopies = SetPrintQuantity.getPrintQuantity(new File(image));

    // Print
    if(nCopies != 0)
        for(int i = 0; i < nCopies; i++)
            this.pj.print(pras);

    System.out.println(nCopies + ((nCopies == 1) ? " copy" : " copies"));

this.pj = PrinterJob


除非打印机支持全出血打印,否则可能不可能实现。 - trashgod
我不确定打印机是否支持全出血打印,但我知道可以通过在Windows图像查看器中使用打印功能来获得无边框打印。所以我想我的目标是实现类似的效果。 - Johnny
我有相同的情况,但在Mac上。从命令行中使用"lp"打印全出血4x6没有问题,但是Java会在每一侧放置巨大的边距,而"setImageableArea"似乎不能去除它们。 - Alexander Ljungberg
1个回答

2

我已经一段时间在解决同样的问题了。这是我的解决方案:

确定您打印机的实际页面大小:

final PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(ps);
final PageFormat pf = printJob.defaultPage();
System.out.println("Printer Page width=" + pf.getWidth() + " height=" + pf.getHeight());

对于我的HiTi P720L照片打印机和4"x6"纸张,实际尺寸为4.133"x6.147"。

创建并设置一个新的纸张对象,包括完整的页面尺寸和完整的可打印区域:

final Paper paper = new Paper();
paper.setSize(pageWidth * 72.0f, pageHeight * 72.0f);
paper.setImageableArea(0.0, 0.0, paper.getWidth(), paper.getHeight());
pf.setPaper(paper);

最后一个技巧是将坐标绘制到范围之外的x/y坐标。在我的情况下,我需要将x坐标缩放10%并将其平移-55(将其放置在负x值中)。

printJob.setPrintable(new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) graphics;
    final double xScale = 1.1;
    final double xTranslate = -55;
    final double yScale = 1;
    final double yTranslate = 0;
    final double widthScale = (pageFormat.getWidth() / image.getWidth()) * xScale;
    final double heightScale = (pageFormat.getHeight() / image.getHeight()) * yScale;
    System.out.println("Setting scale to " + widthScale + "x" + heightScale);
    final AffineTransform at = AffineTransform.getScaleInstance(widthScale, heightScale);
    System.out.println("Setting translate to " + xTranslate + "x" + yTranslate);
    at.translate(xTranslate, yTranslate);
    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    g2.drawRenderedImage(image, at);
    return PAGE_EXISTS;
}
}, pf);

你从哪里得到了10% / -55个点?是通过试错吗? - Matthieu
@Matthieu 你用过这段代码吗?我也在想同样的10% / -55的问题。对我来说,图像大小可以是4x8或4x5。因此,我真的希望有一些东西,使我不必为不同的图像大小不断更改逻辑。而且图像大小可能会因不同的客户而变化。 - coder771
@coder771 不,我没有这样做,原因就在于页面大小是用户相关的,而那看起来像是试错法,需要针对每种纸张大小进行更改。而且由于它们不是公制单位,整个计算过程变得晦涩难懂,我不想逆向工程它。不幸的是,user1224399不在这里了,所以我想我们永远不会知道了... - Matthieu
@Matthieu,你已经为此实现了什么吗? - coder771
@coder771 不,我只是来这里查找有关如何处理边距的信息。我想你需要像我一样玩转PDF打印和页面。 - Matthieu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接