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.
I use this: 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. 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. You havepageWidth
andpageHeight
, and also you can get the margins vianewAttributes.getMinMargins()
. All the values you need are innewAttributes
.