首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印通过使用iTextG创建的PDF。打印机是否连接在以太网上?

打印通过使用iTextG创建的PDF。打印机是否连接在以太网上?
EN

Stack Overflow用户
提问于 2017-11-22 14:58:50
回答 1查看 36关注 0票数 0

我已经用iTextG库创建了一个pdf。现在我想通过以太网打印它。我该如何实现这一点呢?使用以下几行代码,我可以调用默认的打印服务,但它没有显示我的打印机连接到以太网。在我的电脑上安装驱动程序后,我可以使用Google Cloud打印服务打印pdf。有人能在这方面帮我吗?

目前我使用的代码如下:

代码语言:javascript
复制
public class MyPrintHelper {

    private Context context;

    public MyPrintHelper(Context context) {
        this.context = context;
    }

    public void print() {
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
        // Set job name, which will be displayed in the print queue
        String jobName = context.getString(R.string.app_name) + " Document";
        // Start a print job, passing in a PrintDocumentAdapter implementation
        // to handle the generation of a print document
        printManager.print(jobName, new MyPrintDocumentAdapter(), null);
    }

    private class MyPrintDocumentAdapter extends PrintDocumentAdapter {

        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            InputStream input = null;
            OutputStream output = null;

            try {

                input = new FileInputStream(new File(context.getExternalCacheDir(), PDF_FILE_NAME));
                output = new FileOutputStream(destination.getFileDescriptor());

                byte[] buf = new byte[1024];
                int bytesRead;

                while ((bytesRead = input.read(buf)) > 0) {
                    output.write(buf, 0, bytesRead);
                }

                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    input.close();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }


            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

            callback.onLayoutFinished(pdi, true);
        }

    }
}

为了使用上面的helper类,我按如下方式调用它:

代码语言:javascript
复制
MyPrintHelper helper = new MyPrintHelper(getActivity());
helper.print();

我正在使用以下打印机:

https://www.amazon.in/Heyday-Thermal-Receipt-Printer-GP-U80300I/dp/B011BWM95E

EN

回答 1

Stack Overflow用户

发布于 2017-11-30 04:14:09

您的设备应与打印机位于同一网络中。此外,您还需要确保您使用的打印机是Mopria-certified:“集成打印支持与所有Mopria认证的打印机兼容,这些打印机占全球销售的打印机的97%。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47428592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档