Hey everyone so I am trying to build a small sample printing app on android and can't seem to print an existing pdf. There is plenty of documentation on creating a custom document with the canvas but I already have the document. Basically I just want to be a able to read in a pdf document and send it as a file output stream directly to the printer to be printed. Any help is appreciated.
Choose “File” > “Print”. Choose “Adobe PDF” from the list of printers in the print dialog box. Click "Print" to use the Acrobat PDF printer. Click “OK” and enter a new file name for your PDF. Save to your desired location.
Here's how to save as PDF on Android: Open the file or Web page you need to print to PDF. Tap the three vertical dots icon on the top-right. Tap Print.
We can simply achieve this by creating a custom PrintDocumentAdapter
PdfDocumentAdapter.java
public class PdfDocumentAdapter extends PrintDocumentAdapter {
Context context = null;
String pathName = "";
public PdfDocumentAdapter(Context ctxt, String pathName) {
context = ctxt;
this.pathName = pathName;
}
@Override
public void onLayout(PrintAttributes printAttributes, PrintAttributes printAttributes1, CancellationSignal cancellationSignal, LayoutResultCallback layoutResultCallback, Bundle bundle) {
if (cancellationSignal.isCanceled()) {
layoutResultCallback.onLayoutCancelled();
}
else {
PrintDocumentInfo.Builder builder=
new PrintDocumentInfo.Builder(" file name");
builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN)
.build();
layoutResultCallback.onLayoutFinished(builder.build(),
!printAttributes1.equals(printAttributes));
}
}
@Override
public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor parcelFileDescriptor, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) {
InputStream in=null;
OutputStream out=null;
try {
File file = new File(pathName);
in = new FileInputStream(file);
out=new FileOutputStream(parcelFileDescriptor.getFileDescriptor());
byte[] buf=new byte[16384];
int size;
while ((size=in.read(buf)) >= 0
&& !cancellationSignal.isCanceled()) {
out.write(buf, 0, size);
}
if (cancellationSignal.isCanceled()) {
writeResultCallback.onWriteCancelled();
}
else {
writeResultCallback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
}
}
catch (Exception e) {
writeResultCallback.onWriteFailed(e.getMessage());
Logger.logError( e);
}
finally {
try {
in.close();
out.close();
}
catch (IOException e) {
Logger.logError( e);
}
}
}}
Now call print by using PrintManager
PrintManager printManager=(PrintManager) getActivityContext().getSystemService(Context.PRINT_SERVICE);
try
{
PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(Settings.sharedPref.context,filePath );
}
printManager.print("Document", printAdapter,new PrintAttributes.Builder().build());
}
catch (Exception e)
{
Logger.logError(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With