我有以下代码用于使用Android Printing Framework打印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文件会侧向旋转并被剪裁。我想知道,有什么办法可以解决这个问题吗?
更新:我刚刚进行了测试打印,实际打印正确。但是,预览显示它会侧向旋转。
答案 0 :(得分:0)
在我的情况下,这是因为我正在使用自己的变换,并且在提供变换时,本机渲染器不应用PDF页面属性中指定的页面旋转。目前无法通过Android API获取此属性,因此我使用了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);
}
}
以下是原生PDF渲染器源代码的链接:https://github.com/android/platform_frameworks_base/blob/master/core/jni/android/graphics/pdf/PdfRenderer.cpp