Android Question How to Print PDF Using Android Printing Framework

nadhiras

Member
Licensed User
Longtime User
Hi all.
I read this post :
https://www.b4x.com/android/forum/threads/android-printing-framework.38796/
on that post describe how to print using android printing framework. but we need printing pdf
and as we know webview cannot load pdf.
i know there is many ways to print in here.
but i want my apps print using android printing framework.

if found some code form net to use this framework. but in java . i don't understand java :(:(
so anyone can converting java to b4a ..

and this code i found :
B4X:
public class PrintPDFAdapter extends PrintDocumentAdapter {
    private File pdfFile;
    private String fileName;

    public PrintPDFAdapter(File pdfFile, String fileName) {
        this.pdfFile = pdfFile;
        this.fileName = fileName;
    }


    @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(fileName).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

        callback.onLayoutFinished(pdi, true);
    }

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

        try {

            input = new FileInputStream(pdfFile);
            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();
            }
        }
    }
}
B4X:
private void doPDFPrint(File pdfFile, String filename) {
    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
    String jobName = this.getString(R.string.app_name) + " Report";
    PrintPDFAdapter pda = new PrintPDFAdapter(pdfFile, filename);
    PrintAttributes attrib = new PrintAttributes.Builder().
            setMediaSize(PrintAttributes.MediaSize.NA_LETTER.asLandscape()).
            setMinMargins(PrintAttributes.Margins.NO_MARGINS).
            build();
    printManager.print(jobName, pda, attrib);
}

Sorry for my bad English...
 

Tayfur

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   File.Copy(File.DirAssets, "myfile.pdf", File.DirRootExternal, "1.pdf")
   PrintPDF(File.DirRootExternal, "1.pdf")
End Sub

Sub PrintPDF(Dir As String, FileName As String)
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim jPrintManager As JavaObject = ctxt.RunMethod("getSystemService", Array As Object("print"))
   Dim jPrintAdapter As JavaObject
   jPrintAdapter.InitializeNewInstance(Application.PackageName & ".main$PrintPDFAdapter", Array (File.Combine(Dir, FileName), FileName))
   Dim jobName As String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, Null))
End Sub

#if Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
public static class PrintPDFAdapter extends PrintDocumentAdapter {
    private String pdfFile;
    private String fileName;

    public PrintPDFAdapter(String pdfFile, String fileName) {
    this.pdfFile = pdfFile;
    this.fileName = fileName;
    }


    @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(fileName).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

    callback.onLayoutFinished(pdi, true);
    }

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

    try {

    input = new FileInputStream(pdfFile);
    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 (Exception e) {
    throw new RuntimeException(e);
    } finally {
    try {
    input.close();
    output.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
   }
#end if

hi @Erel

How can i use code in other modoles /activities. it work in main module.

B4X:
  jPrintAdapter.InitializeNewInstance(Application.PackageName & ".main$PrintPDFAdapter", Array (File.Combine(Dir, FileName), FileName))

i changed like this

B4X:
  jPrintAdapter.InitializeNewInstance(Application.PackageName & ".MyModuleName$PrintPDFAdapter", Array (File.Combine(Dir, FileName), FileName))
it coukld not work
 
Upvote 0
Top