- PR -

JDK1.4: javax.printによるPDF印刷

1
投稿者投稿内容
hidekazu
会議室デビュー日: 2002/05/10
投稿数: 9
投稿日時: 2002-05-10 10:23
JDK1.4からの新機能でjavax.printが追加されたのですが、
この機能でPDFを印刷したいのですが、job.printのところで
Exceptionが出て印刷できません。
誰か解決策をご存知の方はいませんか?

ちなみにソースは以下の通りです。
(classファイルと同じフォルダにある"test.pdf"というファイルを印刷する)

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
import java.io.*;
import java.awt.*;

public class PrintPDF {
public static void main (String args[]){
// Input the file
System.out.println("Start");
FileInputStream textstream;
textstream = null;
try
{

textstream = new FileInputStream("test.pdf");
}
catch (FileNotFoundException ffne)
{
System.out.println("FileOpenError!!");
}
if (textstream == null)
{
return;
}
// Set the document type
DocFlavor myFormat = DocFlavor.INPUT_STREAM.PDF;
// Create a Doc
Doc myDoc = new SimpleDoc(textstream, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
// aset.add(MediaSizeName.ISO_A4);
// aset.add(Sides.ONE_SIDED);
// discover the printers that can print the format according to the
// instructions in the attribute set
PrintService PrintService =
PrintServiceLookup.lookupDefaultPrintService();
// Create a print job from one of the printDefaultServices
if (PrintService != null)
{
DocPrintJob job = PrintService.createPrintJob();

try
{
job.print(myDoc, null);
}
catch (PrintException pe)
{
System.out.println(pe);
System.out.println(PrintService);
}
}
else
{
System.out.println("No Printers!!");
}
}
}
Kissinger
ぬし
会議室デビュー日: 2002/04/30
投稿数: 428
お住まい・勤務地: 愛知県
投稿日時: 2002-05-14 07:54
hidekazuさんはじめまして。
Exception について詳しく書かれていないませんが、invalid flavor ではありませんか?
これは DocPrintJobに使用した PrintService が、指定した DocFlavor に対応していないということだと思われます。
試しに、myFormat と aset を指定して lookupPrintServices で使用可能なプリンタの一覧を取り出して( println で確認するなどして)lookupDefaultPrintService で取得したサービスが含まれているかどうか調べてください。
もし含まれていないか、そもそも一覧がゼロ件の場合は印刷できないのでしょう。
私もやってみましたが、EPSON PM900C, Windows 98, Java 2 SE SDK 1.4.0 で GIF は印刷できましたが、PDF や TEXT_PLAIN_US_ASCII では該当するプリントサービスが見つかりませんでした。
hidekazu さんのところで発生している現象が invalid flavor でなければこの限りではありません。
Kissinger
ぬし
会議室デビュー日: 2002/04/30
投稿数: 428
お住まい・勤務地: 愛知県
投稿日時: 2002-05-14 07:56
私の前の投稿で i println とあるのは、小括弧はじめ println と打ったつもりが化けてしまったようです。
1

スキルアップ/キャリアアップ(JOB@IT)