LoginSignup
9
9

More than 5 years have passed since last update.

Apache PDFBoxでPDFを印刷する

Last updated at Posted at 2016-06-11

Apache PDFBoxが日本語出力できるようになったのもあり、今まで全然使ったことなかったけどPDFBoxどんなことできるの?っていうのを少しずつ調べています。

今回はPDFの印刷の方法です。

下記の公式Documentを参考にしました。(というか下記を見たらこのページ見る意味ないですけど)
https://pdfbox.apache.org/2.0/migration.html#pdf-printing

PDFファイルの印刷

    public static void main(String[] args) throws IOException, PrinterException {
        try (InputStream in = new FileInputStream("pdf.pdf")) {
            print(in);
        }
    }

    public static void print(InputStream in) throws IOException, PrinterException {
        try (PDDocument doc = PDDocument.load(in)) {

            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(doc));
            //プリンターのダイアログを開く
            if (job.printDialog()) {
                job.print();//印刷
            }
        }
    }

実行結果

ダイアログが開いて。OK押したら。
image

印刷できた様子。(今回はxpsで作成)
image

PDFBoxで自作してPDFファイルを作らずに印刷

書き方見れば当然できると思うんですけど、一応確認してみました。

    public static void print2() throws IOException, PrinterException {
        try (PDDocument doc = new PDDocument()) {
            float fontSize = 20;
            PDRectangle rectangle = PDRectangle.A6;

            PDFont font = loadFont(doc);
            float fontHeight = getFontHeight(font, fontSize);

            PDPage page = new PDPage(rectangle);
            doc.addPage(page);

            try (PDPageContentStream contents = new PDPageContentStream(doc, page)) {
                contents.setLineWidth(1F);
                contents.beginText();

                contents.setNonStrokingColor(Color.RED);
                contents.setStrokingColor(Color.BLUE);

                contents.setFont(font, fontSize);
                contents.setLeading(fontHeight);

                contents.setTextMatrix(Matrix.getTranslateInstance(10, rectangle.getHeight() - fontHeight - 10));
                contents.appendRawCommands(RenderingMode.FILL.intValue() + " Tr ");
                contents.showText("テキスト FILL");
                contents.newLine();
                contents.appendRawCommands(RenderingMode.STROKE.intValue() + " Tr ");
                contents.showText("テキスト STROKE");
                contents.newLine();
                contents.appendRawCommands(RenderingMode.FILL_STROKE.intValue() + " Tr ");
                contents.showText("テキスト FILL_STROKE");
                contents.endText();
            }

            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(doc));
            //プリンターのダイアログを開く
            if (job.printDialog()) {
                job.print();//印刷
            }
        }
    }

実行結果

印刷できた。

image

あとがき

PrinterJobって明るくないのですが、ダイアログ出さずに印刷もできますよね?
なのでちゃんと扱えば、サーバーサイドでサイレントに印刷したりも出来ると思ってます。

ところでiTextにも印刷機能ってありましたっけ?

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9