簡體   English   中英

如何從Android設備打印PDF,圖像和HTML文件到wifi上的打印機?

[英]How to print PDF, image and HTML documents from android device to a printer on wifi?

我需要在我的Android應用程序中實現文檔打印功能。 我能夠使用下面提到的android打印框架代碼打印圖像文件:

private void doPhotoPrint(Bitmap bigbitmap) 
{
    PrintHelper photoPrinter = new PrintHelper(MainActivity.this);
    photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    photoPrinter.printBitmap("droids.jpg - test print", bigbitmap);
}

我經歷了討論同一個問題的現有主題,但沒有一個幫助太多。

我面臨的問題是其他文檔類型,如PDF和HLML。 如果有人可以提供相同的見解會很有幫助。

你可以查看這段代碼

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
        String jobName = this.getString(R.string.app_name) + " Document";



        PrintDocumentAdapter pda = new PrintDocumentAdapter() {

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

                try {
                    AssetManager assetManager = getAssets();
                    File file = new File(getFilesDir(), "fact_sheet.pdf");                 
                    input = assetManager.open("fact_sheet.pdf");                        
                    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 (FileNotFoundException ee) {
                    //Catch exception
                } catch (Exception e) {
                    //Catch exception
                } 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;
                }

                // int pages = computePageCount(newAttributes);

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

                callback.onLayoutFinished(pdi, true);
            }
        };
      printManager.print(jobName, pda, null);  
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM