0

如何使用特定的 Printerservice 配置 AWT-Printerjob 以进行彩色打印?

我的目标是打印一个包含灰色背景颜色的 PDF 文件。为此,我将带有 pdfbox 的 PDF 转换为缓冲图像,并设置了自己的可打印类,将打印方法覆盖到打印机作业。

现在,即使我在 print 方法中绘制了一个红色矩形,它也会被忽略。打印黑色矩形。

将我的 PDF-Bufferedimage 保存到 png 文件时,一切看起来都有效且良好。

import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;

import java.awt.print.PrinterJob;


import javax.imageio.ImageIO;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Chromaticity;
import javax.print.attribute.standard.Copies;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;



public class Printingissue {
    public static void main(String[] args) throws PrinterException, IOException {
        File file = new File("resources/tmp/1380731960.pdf");

        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));
        pras.add(Chromaticity.COLOR);

        PrintService pss[] = PrintServiceLookup.lookupPrintServices(null, pras);

        PrintService service = pss[0];


        PrinterJob printJob = PrinterJob.getPrinterJob();

        final PDDocument document = PDDocument.load(file);
        printJob.setPageable( document );
        printJob.setCopies(1);

        final PageFormat pf1 = printJob.defaultPage();
        Paper paper = new Paper();
        double margin = 36; // half inch
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
                - margin * 2);
        pf1.setPaper(paper);
        printJob.setPrintService( service );
        final List<PDPage> pdfPages = document.getDocumentCatalog().getAllPages();
        printJob.setPrintable(new Printable()
        {
          @Override
          public int print( Graphics g, PageFormat pf, int page ) throws PrinterException
          {
            if (page >= pdfPages.size())
              return NO_SUCH_PAGE;
            try
            {
                ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);

                ColorConvertOp op = new ColorConvertOp(cs, null);
                g.setColor(Color.RED);
                g.drawRect(100, 100, 10, 200);
                pdfPages.get(page).convertToImage();

//                g.drawImage( op.filter( pdfPages.get(page).convertToImage() , null)
//                          ,(int)pf.getImageableX()
//                          ,(int)pf.getImageableY()
//                          ,(int)pf.getImageableWidth()
//                          ,(int)pf.getImageableHeight()
//                          ,null);
            }
            catch (IOException e)
            {

            }
            return PAGE_EXISTS;
          }
        }, pf1);
        printJob.print( pras );
    }
}
4

0 回答 0