android pdf转为打印机语言,Android Pdf文档的生成、显示与打印
Android 6.0带来了好多功能,PDF的读取展示就是其中一项一.PDF文档的生成DisplayMetricsdisplayMetrics=getResources().getDisplayMetrics();//createanewdocumentPdfDocumentdocument=newPdfDocument();//当然,我们也可以使用PrintedPdfDoc...
Android 6.0带来了好多功能,PDF的读取展示就是其中一项
一.PDF文档的生成
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
// create a new document
PdfDocument document = new PdfDocument();//当然,我们也可以使用PrintedPdfDocument,后者继承自前者
// crate a page description
for(int i=0;i
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels), (i+1)).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
//将View的UI会知道Canvas上
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
}
FileOutputStream fos = new FileOutStream("/storage/cache/mypage.pdf");
document.writeTo(fos );
// close the document
document.close();
二.PDF文档读取与显示(配合ViewPager使用)
ParcelFileDescriptor pfdesc = ParcelFileDescriptor.open(new File('/storage/cache/mypage.pdf',ParcelFileDescriptor.MODE_READ_ONLY));
PdfRender pdfRender = new PdfRender(pfdesc);
List mlist = new ArrayList(); //此处职位示例,正式项目请使用懒加载,否则可能导致OOM
if(pdfRender.getPageCount()>0)
{
for(int i=0;i
{
PdfRender.Page page = pdfRender.openPage(i);
Bitmap bmp = Bitmap.createBitmap(page.getWidth(),
page.getHeight(), Bitmap.Config.RGB_565)
page.render(bmp, null,new Matrix() ,PdfRender.Page.RENDER_MODE_FOR_DISPLAY);
page.close();
mlist.add(bmp);
}
pdfRender.close();
}
三.打印PDF文档
打印pdf先要生成pdf打印文档,其次需要使用打印管理器
PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
printManager.print(String printJobName, PrintDocumentAdapter documentAdapter, PrintAttributes attributes)
当然,我们需要完成PrintDocumentAdapter
public class MyPrintDocumentAdapter extends PrintDocumentAdapter
{
Context context;
private int pageHeight;
private int pageWidth;
public PdfDocument myPdfDocument;
public int totalpages = 4;
public MyPrintDocumentAdapter(Context context)
{
this.context = context;
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal,
LayoutResultCallback callback,
Bundle metadata) {
myPdfDocument = new PrintedPdfDocument(context, newAttributes); //创建可打印PDF文档对象
pageHeight =
newAttributes.getMediaSize().getHeightMils()/1000 * 72; //设置尺寸
pageWidth =
newAttributes.getMediaSize().getWidthMils()/1000 * 72;
if (cancellationSignal.isCanceled() ) {
callback.onLayoutCancelled();
return;
}
if (totalpages > 0) {
PrintDocumentInfo.Builder builder = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(totalpages); //构建文档配置信息
PrintDocumentInfo info = builder.build();
callback.onLayoutFinished(info, true);
} else {
callback.onLayoutFailed("Page count is zero.");
}
}
@Override
public void onWrite(final PageRange[] pageRanges,final ParcelFileDescriptor destination,final CancellationSignal cancellationSignal,
final WriteResultCallback callback) {
for (int i = 0; i
if (pageInRange(pageRanges, i)) //保证页码正确
{
PageInfo newPage = new PageInfo.Builder(pageWidth,
pageHeight, i).create();
PdfDocument.Page page =
myPdfDocument.startPage(newPage); //创建新页面
if (cancellationSignal.isCanceled()) { //取消信号
callback.onWriteCancelled();
myPdfDocument.close();
myPdfDocument = null;
return;
}
drawPage(page, i); //将内容绘制到页面Canvas上
myPdfDocument.finishPage(page);
}
}
try {
myPdfDocument.writeTo(new FileOutputStream(
destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
myPdfDocument.close();
myPdfDocument = null;
}
callback.onWriteFinished(pageRanges);
}
private boolean pageInRange(PageRange[] pageRanges, int page)
{
for (int i = 0; i
{
if ((page >= pageRanges[i].getStart()) &&
(page <= pageRanges[i].getEnd()))
return true;
}
return false;
}
//页面绘制(渲染)
private void drawPage(PdfDocument.Page page,
int pagenumber) {
Canvas canvas = page.getCanvas();
//这里是页码。页码不能从0开始
pagenumber++;
int titleBaseLine = 72;
int leftMargin = 54;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText(
"Test Print Document Page " + pagenumber,
leftMargin,
titleBaseLine,
paint);
paint.setTextSize(14);
canvas.drawText("Android PDF 文档打印", leftMargin, titleBaseLine + 35, paint);
if (pagenumber % 2 == 0)
paint.setColor(Color.RED);
else
paint.setColor(Color.GREEN);
PageInfo pageInfo = page.getInfo();
canvas.drawCircle(pageInfo.getPageWidth()/2,
pageInfo.getPageHeight()/2,
150,
paint);
}
}
.
更多推荐
所有评论(0)