Could somebody a non windows system and a printer please run this code that I created for issue PDFBOX-4011, and report what happens ? The code should do two things:

- create an image with a barcode-like pattern in the file output.png

- bring a print dialog box and then print the same.

This fails on windows (printed page is blank) but I wonder if this fails on other OS as well.

Tilman


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Tilman Hausherr
 */
public class PDFBox4010
{
    int width = 284;
    int height = 420;

    public static void main(String[] args) throws IOException, PrinterException
    {
        new PDFBox4010().doStuff();
    }

    void doStuff() throws IOException, PrinterException
    {
        BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) targetImage.getGraphics();

        new Renderer().renderStuff(g, targetImage.getWidth(), targetImage.getHeight());

        g.dispose();

        ImageIO.write(targetImage, "png", new File("output.png"));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new MyPageable(new Renderer(), width, height));
        if (job.printDialog())
        {
            job.print();
        }
    }

    class Renderer
    {
        public void renderStuff(Graphics2D g, int width, int height)
        {
            // background
            g.setColor(Color.white);
            g.fillRect(0, 0, width, height);

            BufferedImage img = new BufferedImage(303, 1, BufferedImage.TYPE_BYTE_BINARY);

            // barcode-like pattern on single pixel line
            for (int i = 0; i < img.getWidth(); ++i)
            {
                img.setRGB(i, 0, (i / 2 % 3) == 0 ? 0 : 0xFFFFFF);
            }

            AffineTransform at = new AffineTransform(0, -1, 1, 0, 100, 300);
            at.concatenate(new AffineTransform(109.08, 0, 0, 63, 0, 0));
            at.scale(1.0 / img.getWidth(), -1 / img.getHeight());
            at.translate(0, -img.getHeight());

            // draw our "barcode"
            g.drawImage(img, at, null);
        }
    }

    class MyPageable implements Pageable
    {
        private final Renderer renderer;
        private final int width;
        private final int height;

        MyPageable(Renderer renderer, int width, int height)
        {
            this.renderer = renderer;
            this.width = width;
            this.height = height;
        }

        @Override
        public int getNumberOfPages()
        {
            return 1;
        }

        @Override
        public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException
        {
            PageFormat format = new PageFormat();
            Paper paper = new Paper();
            paper.setSize(width, height);
            format.setPaper(paper);
            format.setOrientation(PageFormat.PORTRAIT);
            return format;
        }

        @Override
        public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException
        {
            if (pageIndex > 1)
            {
                throw new IndexOutOfBoundsException();
            }
            return new Printable()
            {
                @Override
                public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
                {
                    if (pageIndex < 0 || pageIndex > 1)
                    {
                        return NO_SUCH_PAGE;
                    }
                    renderer.renderStuff((Graphics2D) graphics, (int) pageFormat.getWidth(), (int) pageFormat.getHeight());
                    return PAGE_EXISTS;
                }
            };
        }
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to