在JAVA中将PDF打印为字节数组
我需要为我的打印机打印一个pdf文件。使用这段代码,我已经将pdf转换为bytearray,但我被卡住了,不知道如何将其发送到打印机。有人能帮我吗在JAVA中将PDF打印为字节数组,java,android,pdf,zebra-printers,Java,Android,Pdf,Zebra Printers,我需要为我的打印机打印一个pdf文件。使用这段代码,我已经将pdf转换为bytearray,但我被卡住了,不知道如何将其发送到打印机。有人能帮我吗 File file = new File("java.pdf"); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new by
File file = new File("java.pdf");
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
System.out.println("ERROR!");
}
byte[] bytes = bos.toByteArray();
提前感谢。另一种方法是使用发送pdf文件,下面是 示例代码:
Intent prnIntent = new Intent(Intent.ACTION_SEND);
prnIntent.putExtra(Intent.EXTRA_STREAM, uri);
prnIntent.setType("application/pdf");
startActivity(Intent.createChooser(prnIntent , "Send pdf using:"));
使用这种方法,不需要使用缓冲区,但可以将pdf文件直接发送到打印机 另一种方法是使用发送pdf文件,下面是 示例代码:
Intent prnIntent = new Intent(Intent.ACTION_SEND);
prnIntent.putExtra(Intent.EXTRA_STREAM, uri);
prnIntent.setType("application/pdf");
startActivity(Intent.createChooser(prnIntent , "Send pdf using:"));
使用这种方法,不需要使用缓冲区,但可以将pdf文件直接发送到打印机 要遵循的步骤:
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
// First identify the default print service on the system
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// prepare the flvaor you are intended to print
DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF;
// prepare the print job
DocPrintJob printJob = printService.createPrintJob();
// prepare the document, with default attributes, ready to print
Doc docToPrint = new SimpleDoc( bais, docFlavor, null );
// now send the doc to print job, with no attributes to print
printJob.print( docToPrint, null );
要遵循的步骤:
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
// First identify the default print service on the system
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// prepare the flvaor you are intended to print
DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF;
// prepare the print job
DocPrintJob printJob = printService.createPrintJob();
// prepare the document, with default attributes, ready to print
Doc docToPrint = new SimpleDoc( bais, docFlavor, null );
// now send the doc to print job, with no attributes to print
printJob.print( docToPrint, null );
使用
ByteArrayInputStream
抛出java.lang.IllegalArgumentException
,但常规字节数组对我有效。修改以下示例:
使用
ByteArrayInputStream
抛出java.lang.IllegalArgumentException
,但常规字节数组对我有效。修改以下示例:
看看我的答案。这项工作可以用sockets完成谢谢你的回答,但是还有其他的可能性,比如说没有sockets?请看我的答案。这项工作可以用sockets完成谢谢你的回答,但是还有其他的可能性,比如没有sockets?是的,但是我已经将我的pdf转换为bytearray,在你的例子中,我将bytearray发送到打印机?在这个例子中,你发送了一个pdf文件。因此,您必须首先以bytearray的身份完成工作,然后将其转换为pdf文件,然后使用此解决方案将其发送到打印机。Android方法在标准java应用程序中有何帮助?你是对的,这只是一个android解决方案,但我认为这个问题是针对android的。所以,我认为问题中缺少了标记android。是的,但我已将我的pdf转换为bytearray,在您的示例中,我将bytearray发送到打印机?在这个示例中,您将发送一个pdf文件。因此,您必须首先以bytearray的身份完成工作,然后将其转换为pdf文件,然后使用此解决方案将其发送到打印机。Android方法在标准java应用程序中有何帮助?你是对的,这只是一个android解决方案,但我认为这个问题是针对android的。因此,我认为这个问题中缺少了标记android。它抛出了一个“数据不是声明的类型”exception@whizzzkey: ??? 哪些数据???它在哪里????发布您的查询,并提供适当的详细信息,说明您在哪里发现了…“哪些数据?”-只是简单的pdf文档,“它在哪里?”-在线文档doctorprit=new SimpleDoc(bais,docFlavor,null);。我按照你发布的方式做所有事情,我有一个类似于TS的代码,用于从我的pdf文件中获取bytearray。正如我建议的,你最好发布你的代码,exception stacktrace,并将你的查询作为一个新问题。仅仅通过注释很难识别问题并给出解决方案。此代码将始终引发异常。您应该将字节数组而不是IS传递给SimpleDoc obj(不需要IS更清楚)。否则,您应该将doc flavor更改为DocFlavor.INPUT\u流才能工作。它抛出一个“数据不是声明的类型”exception@whizzzkey: ??? 哪些数据???它在哪里????发布您的查询,并提供适当的详细信息,说明您在哪里发现了…“哪些数据?”-只是简单的pdf文档,“它在哪里?”-在线文档doctorprit=new SimpleDoc(bais,docFlavor,null);。我按照你发布的方式做所有事情,我有一个类似于TS的代码,用于从我的pdf文件中获取bytearray。正如我建议的,你最好发布你的代码,exception stacktrace,并将你的查询作为一个新问题。仅仅通过注释很难识别问题并给出解决方案。此代码将始终引发异常。您应该将字节数组而不是IS传递给SimpleDoc obj(不需要IS更清楚)。否则,您应该将doc flavor更改为DocFlavor.INPUT\u流才能工作。