[英]Print multiple PDF pages using Android 4.4 printing framework
I want ask for help. 我想请求帮助。 I want print/create pdf document.
我想要打印/创建pdf文档。 I can create one pdf page and write data to this page via canvas.
我可以创建一个pdf页面并通过画布将数据写入此页面。 Problem is I dont know how I can create another pdf page and continue writing to this second page.
问题是我不知道如何创建另一个pdf页面并继续写入第二页。 If somebody has any experiences I will very help full I spend many time with this.
如果有人有任何经验,我会非常乐于助人,我会花很多时间来做这件事。
I use this: https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html 我用这个: https : //developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html
Part of my code is there: 我的部分代码在那里:
private void doPrint(int _docNumber){
docNumber = _docNumber;
//get Printmanager instance
PrintManager printManager = (PrintManager)this.getSystemService(Context.PRINT_SERVICE);
// Set job name, which will be displayed in the print queue
String jobName = getString(R.string.app_name) + " dokument";
// Start a print job, passing in a PrintDocumentAdapter implementation
// to handle the generation of a print document
printManager.print(jobName, new MyPrintDocAdapter(), null);
Toast.makeText(getBaseContext(), "Príprava na tlač...", Toast.LENGTH_SHORT).show();
}
public class MyPrintDocAdapter extends PrintDocumentAdapter
{
Context context;
private int pageHeight;
private int pageWidth;
public PdfDocument myPdfDocument;
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
myPdfDocument = new PrintedPdfDocument(context, newAttributes);
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("Dokument_"+ docNumber + ".pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(totalpages);
PrintDocumentInfo info = builder.build();
callback.onLayoutFinished(info, true);
} else {
callback.onLayoutFailed("Počet strán je nula.");
}
}
@Override
public void onWrite(final PageRange[] pageRanges, final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal, final WriteResultCallback callback) {
for (int i = 0; i < totalpages; i++) {
if (pageInRange(pageRanges, i))
{
PdfDocument.PageInfo newPage = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, i).create();
PdfDocument.Page page = myPdfDocument.startPage(newPage);
if (cancellationSignal.isCanceled()) {
callback.onWriteCancelled();
myPdfDocument.close();
myPdfDocument = null;
return;
}
switch (docNumber) {
case 1: drawObjednavka(page, i);
break;
case 2: drawVykaz(page, i);
break;
default: Toast.makeText(MainActivity.this, "Bad file format",Toast.LENGTH_SHORT).show();
}
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<pageRanges.length; i++)
{
if ((page >= pageRanges[i].getStart()) &&
(page <= pageRanges[i].getEnd()))
return true;
}
return false;
}
private void drawObjednavka(PdfDocument.Page page, int pagenumber) {
int verticalPosY = 0;
canvas = page.getCanvas();
pagenumber++; // Make sure page numbers start at 1
Typeface tf = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL); // there you can change type of font family, if needed PF
PdfDocument.PageInfo pageInfo = page.getInfo();
Paint paint = new Paint();
paint.setTypeface(tf);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(1F); //set line thickness
paint.setTextSize(20);
paint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText("Objednávka", DEFAULT_LEFT_MARGIN_X, 30, paint);...
Since you are painting directly to a canvas, you can calculate the totalpages
you need. 由于您直接绘制到画布,因此可以计算所需的总
totalpages
。 When a new media size is selected by the user, you should redraw your pages according to the page dimensions and how you want to display the page. 当用户选择新的介质尺寸时,您应根据页面尺寸以及显示页面的方式重新绘制页面。
Your code is ok, you just need to calculate the totalpages
according to the media size selected by the user. 您的代码没问题,您只需根据用户选择的媒体大小计算总
totalpages
。 You have pageWidth
and pageHeight
, and also you can get the margins via newAttributes.getMinMargins()
. 你有
pageWidth
和pageHeight
,你也可以通过newAttributes.getMinMargins()
获得边距。 All the values you need are in newAttributes
. 您需要的所有值都在
newAttributes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.