我有以下代码用于使用 Android 打印框架打印 PDF 文件:
这是我的 PrintDocumentAdapter 类:
@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();
}
}
}
}
这是我调用 PrintManager 打印命令的方法:
@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);
}
我要打印的 PDF 是横向的。出于某种原因,当发送它进行打印时,PDF 文件会横向旋转并被剪裁。我想知道,有什么办法可以解决这个问题吗?
更新:我刚刚做了一个测试打印,它实际上打印正确。但是,预览显示它横向旋转。