title: Print multiple PDF pages using Android 4.4 printing framework
body: <p>I want ask for help. I want print/create pdf document. I can create one pdf page and write data to this page via canvas. Problem is I dont know how I can create another pdf page and continue writing to this second page. If somebody has any experiences I will very help full I spend many time with this.</p>
  
  <p>I use this: <a href="https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html" rel="nofollow noreferrer">https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html</a></p>
  
  <p>Part of my code is there:</p>
  
  <pre><code> 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 &gt; 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 &lt; 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&lt;pageRanges.length; i++)
          {
              if ((page &gt;= pageRanges[i].getStart()) &amp;&amp;
                      (page &lt;= 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);...
  </code></pre>