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 :
Sorry for my bad English...
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...