2011-07-20 8 views
0

コンパイルして実行した後、「no pdf printer available」と表示されます。これを解決する方法は?バイト配列としてPDFファイルを読むには?

私はc:\ print.pdf(PHP TCPDFを使用)でファイルを作成しました。私は黙って印刷のいずれかのポップアップを表示せず それを印刷できるように、私は は、バイト配列で、そのファイルを読み込むしようとしているなど

私は、それが動作しますカント誰がどのように ファイルを読むためにガイドを表示してくださいすることができますバイト配列で?次の操作を行うには:

import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.ObjectInputStream; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.print.Doc; 
import javax.print.DocFlavor; 
import javax.print.DocPrintJob; 
import javax.print.PrintException; 
import javax.print.PrintService; 
import javax.print.PrintServiceLookup; 
import javax.print.SimpleDoc; 

public class print 
{ 
    private static Object pdfBytes; 

    // Byte array reader 
    public static byte[] getBytesFromFile(File file) throws IOException { 
     InputStream is = new FileInputStream(file); 
     long length = file.length(); 
     if (length > Integer.MAX_VALUE) {} 
     byte[] bytes = new byte[(int)length]; 

     int offset = 0; 
     int numRead = 0; 
     while (offset < bytes.length 
       && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
      offset += numRead; 
     } 
     if (offset < bytes.length) { 
      throw new IOException("Could not completely read file "+file.getName()); 
     } 
     is.close(); 
     return bytes; 
    }  

    // Convert Byte array to Object 
    public static Object toObject(byte[] bytes) 
    { 
     Object obj = null; 
     try { 
      ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 
      ObjectInputStream ois = new ObjectInputStream (bis); 
      obj = ois.readObject(); 
     } catch (IOException ex) { 

     } catch (ClassNotFoundException ex) { 

     } 
     return obj; 
    } 

    private static File fl = new File("c:\\print.pdf");  
    public static void main(String argc[]) 
    { 
     DocFlavor flavor = DocFlavor.BYTE_ARRAY.PDF; 
     PrintService[] services = 
       PrintServiceLookup.lookupPrintServices(flavor, 
       null); 
     //Object pdfBytes = null; 
     try { 
      byte[] abc = getBytesFromFile(fl); 
      pdfBytes =toObject(abc); 
     } catch (IOException ex) { 
      Logger.getLogger(print.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     if (services.length>0) 
     { 
      DocPrintJob printJob = services[0].createPrintJob(); 
      Doc document = new SimpleDoc(pdfBytes,flavor,null); 
      try { 
       printJob.print(document, null); 
      } catch (PrintException ex) { 
       Logger.getLogger(print.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } else { 
      System.out.println("no pdf printer available"); 

     } 
    } 

} 

を私はこれを試してみました、それは私のサイレント印刷解き:https://gist.github.com/1094612

+0

これはPDFと特に関係ありません。タイトルは「バイト配列にファイルを読み込む方法」でなければなりません。印刷は別の問題でもあり、それは分離する必要があります。 – Mzn

答えて

1

はここで[]バイトにファイルを読み取る方法の例です:

// Returns the contents of the file in a byte array. 
public static byte[] getBytesFromFile(File file) throws IOException { 
    InputStream is = new FileInputStream(file); 

    // Get the size of the file 
    long length = file.length(); 

    // You cannot create an array using a long type. 
    // It needs to be an int type. 
    // Before converting to an int type, check 
    // to ensure that file is not larger than Integer.MAX_VALUE. 
    if (length > Integer.MAX_VALUE) { 
     // File is too large 
    } 

    // Create the byte array to hold the data 
    byte[] bytes = new byte[(int)length]; 

    // Read in the bytes 
    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length 
      && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 

    // Ensure all the bytes have been read in 
    if (offset < bytes.length) { 
     throw new IOException("Could not completely read file "+file.getName()); 
    } 

    // Close the input stream and return bytes 
    is.close(); 
    return bytes; 
} 
+0

コンパイルして実行した後、「no pdf printer available」と表示されます。これを解決する方法は?上記の新しいアップデートをご覧ください。 – YumYumYum

0
import java.io.*; 
import java.util.*; 
import com.lowagie.text.*; 
import com.lowagie.text.pdf.*; 

public class ReadPDFFile { 
     public static void main(String[] args) throws IOException { 
       try { 
         Document document = new Document(); 
         document.open(); 
         PdfReader reader = new PdfReader("file.pdf"); 
         PdfDictionary dictionary = reader.getPageN(1); 
         PRIndirectReference reference = (PRIndirectReference) dictionary 
             .get(PdfName.CONTENTS); 
         PRStream stream = (PRStream) PdfReader.getPdfObject(reference); 
         byte[] bytes = PdfReader.getStreamBytes(stream); 
         PRTokeniser tokenizer = new PRTokeniser(bytes); 
         StringBuffer buffer = new StringBuffer(); 
         while (tokenizer.nextToken()) { 
           if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) { 
             buffer.append(tokenizer.getStringValue()); 
           } 
         } 
         String test = buffer.toString(); 
         System.out.println(test); 
       } catch (Exception e) { 
       } 
     } 
} 
+1

私はサードパーティ製のライブラリラだと思いますか?コンパイルして実行した後、「no pdf printer available」と表示されます。上記の新しいアップデートを参照してください。 – YumYumYum

1

コンパイルして実行した後、「pdfプリンタを使用できません」と表示されます。

ドキュメントherehereを読んだところ、DocFlavourでドキュメントを印刷する方法を理解しているプリントサービスプロバイダを設定していないという問題があります。

解決策の1つは、PDFドキュメント用のPrinterService SPIを実装したJARファイルを見つけてクラスパスに追加することです。 Google検索で例が表示されます。 (私はこれを使用する必要がないので、特定のSPを推薦することはできません。あなたのために働くものを見つけるためには、何らかの調査/テストが必要です)。

関連する問題