4

我想寻求帮助。我想要打印/创建 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);...
4

1 回答 1

0

由于您直接在画布上绘画,因此您可以计算出totalpages您需要的量。当用户选择新的媒体尺寸时,您应该根据页面尺寸和您希望如何显示页面来重新绘制页面。

你的代码没问题,你只需要totalpages根据用户选择的媒体大小计算。你有pageWidthand pageHeight,你也可以通过newAttributes.getMinMargins(). 您需要的所有值都在newAttributes.

于 2014-07-23T12:28:38.460 回答