我正在尝试使用 android API PdfDocument 创建 PDF 文件。
如果内容较少(在屏幕的可见视图内) - 我的代码工作正常。
如果 recyclerView 包含更多项目(超过屏幕高度) - 所有内容都不会以 PDF 格式打印。
这是我的代码:
public void exportDataPDF(View view) {
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.ISO_A2).
setResolution(new PrintAttributes.Resolution("MyPrinterId", PRINT_SERVICE, 50, 50)).
setMinMargins(new PrintAttributes.Margins(5,5,5,5)). //PrintAttributes.Margins.NO_MARGINS
build();
PdfDocument pdfDocument = new PrintedPdfDocument(this, printAttrs);
int pageNumber = 1;
//Get Content View.
View contentView = findViewById(R.id.recyclerView);
// crate a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(contentView.getWidth(), contentView.getHeight(), pageNumber).create();
// create a new page from the PageInfo
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
// draw view on the page
Canvas pageCanvas = page.getCanvas();
pageCanvas.scale(1f, 1f);
int pageWidth = pageCanvas.getWidth();
int pageHeight = pageCanvas.getHeight();
int measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY);
contentView.measure(measureWidth, measuredHeight);
contentView.layout(0, 0, pageWidth, pageHeight);
contentView.draw(pageCanvas);
// do final processing of the page
pdfDocument.finishPage(page);
try {
FileOutputStream fileOutputStream = openFileOutput(exportFileName, MODE_PRIVATE);
pdfDocument.writeTo(fileOutputStream);
fileOutputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
有人可以帮我以 PDF 格式打印 recyclerView 项目的所有内容。我不想使用 iText 或其他外部库。