Reputation: 802
I have the following code for to print a PDF file using the Android Printing Framework:
Here is my PrintDocumentAdapter class:
@TargetApi(Build.VERSION_CODES.KITKAT)
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();
}
}
}
}
Here is my method that calls the PrintManager print command:
@TargetApi(Build.VERSION_CODES.KITKAT)
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);
}
The PDF that I am trying to print is in landscape orientation. For some reason, when sending it to print, the PDF file gets rotated sideways and gets clipped. I would like to know, is there anything I can do to get around this?
UPDATE: I just did a test print, and it actually prints properly. However, the preview shows it rotated sideways.
Upvotes: 1
Views: 1933
Reputation: 992
In my case it was because I'm using my own transform, and when supplying a transform, the native renderer does not apply the page rotation specified in the PDF page's attributes. There is no way to get this attribute via the Android API right now, so I used iText:
private PdfReader _iTextReader;
...
_iTextReader = new PdfReader(file.getAbsolutePath());
...
// iText uses 1-indexed pages
PdfDictionary iTextPage = _iTextReader.getPageN(pageNumber + 1);
PdfNumber rotate = iTextPage.getAsNumber(PdfName.ROTATE);
if (rotate != null && rotate.intValue() % 360 != 0) {
// getPageSize returns media box
float mediaBoxWidth = _iTextReader.getPageSize(pageNumber + 1).getWidth();
float mediaBoxHeight = _iTextReader.getPageSize(pageNumber + 1).getHeight();
float iTextCenterX = mediaBoxWidth / 2;
float iTextCenterY = mediaBoxHeight / 2;
Util.Log("Applying " + rotate.intValue() + " degree rotation per PDF page attributes.");
pdfMatrix.postRotate(rotate.intValue(), iTextCenterX, iTextCenterY);
/*
* Transform it back to the top-left corner.
* For the life of me, I do not know why these translations are right. But I've
* test both portrait->landscape and landscape->portrait on all rotation angles.
*/
if (rotate.intValue() == 90) {
pdfMatrix.postTranslate(iTextCenterX - iTextCenterY, iTextCenterX - iTextCenterY);
} else if (rotate.intValue() == 270) {
pdfMatrix.postTranslate(3 * iTextCenterY - 3 * iTextCenterX, iTextCenterX - iTextCenterY);
}
}
Here's the link to the source code of the native PDF renderer: https://github.com/android/platform_frameworks_base/blob/master/core/jni/android/graphics/pdf/PdfRenderer.cpp
Upvotes: 0