0

ファイルを取得してから印刷するプロジェクトに取り組んでいdesktop.print(file); ますが、このファイルが非常に大きい場合 (たとえば 50 ページ)、ジョブをプリンターに送信したくありません。

ジョブを開始する前に、ファイルの印刷に必要なページ数を取得する方法はありますか?

これが私がこれを行うコードの一部です:

File file = new File(filePath);         
try {
    desktop.print(file);
} catch (IOException e) {
    System.out.println("error in trying to print");
    e.printStackTrace();
}
4

1 に答える 1

0

ファイル サイズを取得して、50 ページのファイル サイズよりも小さいか大きいかを確認してみてください。この方法では、ファイル サイズに基づいて、印刷するかどうかを決定できます。ファイル サイズを取得する方法は次のとおりです。

    public static void main(String[] args)
    {   
        File file =new File("c:\\java_xml_logo.jpg");

        if(file.exists()){

            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
            double gigabytes = (megabytes / 1024);
            double terabytes = (gigabytes / 1024);
            double petabytes = (terabytes / 1024);
            double exabytes = (petabytes / 1024);
            double zettabytes = (exabytes / 1024);
            double yottabytes = (zettabytes / 1024);

            System.out.println("bytes : " + bytes);
            System.out.println("kilobytes : " + kilobytes);
            System.out.println("megabytes : " + megabytes);
            System.out.println("gigabytes : " + gigabytes);
            System.out.println("terabytes : " + terabytes);
            System.out.println("petabytes : " + petabytes);
            System.out.println("exabytes : " + exabytes);
            System.out.println("zettabytes : " + zettabytes);
            System.out.println("yottabytes : " + yottabytes);
        }else{
             System.out.println("File does not exists!");
        }

    }
}

ソース

于 2013-06-07T18:50:54.110 に答える