我想请求帮助。我想要打印/创建pdf文档。我可以创建一个pdf页面并通过画布将数据写入此页面。问题是我不知道如何创建另一个pdf页面并继续写入第二页。如果有人有任何经验,我会非常乐于助人,我花了很多时间。
我用这个:https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html
我的部分代码在那里:
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);...
答案 0 :(得分:0)
由于您直接绘制到画布,因此可以计算出所需的totalpages
。当用户选择新的介质尺寸时,您应该根据页面尺寸以及显示页面的方式重新绘制页面。
您的代码没问题,您只需根据用户选择的媒体大小计算totalpages
即可。您有pageWidth
和pageHeight
,还可以通过newAttributes.getMinMargins()
获得边距。您需要的所有值都在newAttributes
。