android.graphics.pdf.PdfDocument Java Examples
The following examples show how to use
android.graphics.pdf.PdfDocument.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDFUtil.java From PDFCreatorAndroid with MIT License | 7 votes |
/** * Writes given PDFDocument using content views. * * @param pdfDocument PDFDocument to be written. */ private void writePDFDocument(final PdfDocument pdfDocument) { for (int i = 0; i < mContentViews.size(); i++) { //Get Content View. View contentView = mContentViews.get(i); // crate a page description PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo. Builder((int) PDF_PAGE_WIDTH, (int) PDF_PAGE_HEIGHT, i + 1).create(); // start a page PdfDocument.Page page = pdfDocument.startPage(pageInfo); // draw view on the page Canvas pageCanvas = page.getCanvas(); pageCanvas.scale(1f, 1f); int pageWidth = pageCanvas.getWidth(); int pageHeight = pageCanvas.getHeight(); int measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY); contentView.measure(measureWidth, measuredHeight); contentView.layout(0, 0, pageWidth, pageHeight); contentView.draw(pageCanvas); // finish the page pdfDocument.finishPage(page); } }
Example #2
Source File: PDFUtil.java From PDFCreatorAndroid with MIT License | 6 votes |
/** * Do In Background. * * @param params Params * @return TRUE if PDF successfully generated else FALSE. */ @Override protected File doInBackground(Void... params) { try { // Create PDF Document. PdfDocument pdfDocument = new PdfDocument(); // Write content to PDFDocument. writePDFDocument(pdfDocument); // Save document to file. return savePDFDocumentToStorage(pdfDocument); } catch (Exception exception) { exception.printStackTrace(); return null; } }
Example #3
Source File: RescueCodePrintDocumentAdapter.java From secure-quick-reliable-login with MIT License | 6 votes |
@Override public void onWrite( final PageRange[] pageRanges, final ParcelFileDescriptor destination, final CancellationSignal cancellationSignal, final WriteResultCallback callback ) { PdfDocument.Page page = mPdfDocument.startPage(0); if (cancellationSignal.isCanceled()) { callback.onWriteCancelled(); mPdfDocument.close(); mPdfDocument = null; return; } drawPage(page); mPdfDocument.finishPage(page); try { mPdfDocument.writeTo(new FileOutputStream( destination.getFileDescriptor())); } catch (IOException e) { callback.onWriteFailed(e.toString()); return; } finally { mPdfDocument.close(); mPdfDocument = null; } callback.onWriteFinished(pageRanges); }
Example #4
Source File: IdentityPrintDocumentAdapter.java From secure-quick-reliable-login with MIT License | 5 votes |
@Override public void onWrite( final PageRange[] pageRanges, final ParcelFileDescriptor destination, final CancellationSignal cancellationSignal, final WriteResultCallback callback ) { PdfDocument.Page page = mPdfDocument.startPage(0); if (cancellationSignal.isCanceled()) { callback.onWriteCancelled(); mPdfDocument.close(); mPdfDocument = null; return; } drawPage(page); mPdfDocument.finishPage(page); try { mPdfDocument.writeTo(new FileOutputStream( destination.getFileDescriptor())); } catch (IOException e) { callback.onWriteFailed(e.toString()); return; } finally { mPdfDocument.close(); mPdfDocument = null; } callback.onWriteFinished(pageRanges); }
Example #5
Source File: StorageController.java From slide with MIT License | 5 votes |
@Override protected Boolean doInBackground(Void... params) { PdfDocument document = new PdfDocument(); ParcelFileDescriptor pfd = null; try { PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(640, 640 * 9 / 16, 1).create(); for (Slide slide : store.getState().slides()) { PdfDocument.Page page = document.startPage(pageInfo); page.getCanvas().drawColor(Style.COLOR_SCHEMES[store.getState().colorScheme()][1]); slide.render(mContext, page.getCanvas(), page.getCanvas().getWidth(), page.getCanvas().getHeight(), Style.SLIDE_FONT, Style.COLOR_SCHEMES[App.getState().colorScheme()][0], Style.COLOR_SCHEMES[App.getState().colorScheme()][1], true); document.finishPage(page); } pfd = mContext.getContentResolver().openFileDescriptor(uri, "w"); if (pfd != null) { FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor()); document.writeTo(fos); return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); return false; } finally { document.close(); if (pfd != null) { try { pfd.close(); } catch (IOException ignored) {} } } }
Example #6
Source File: IdentityPrintDocumentAdapter.java From secure-quick-reliable-login with MIT License | 4 votes |
private void drawPage(PdfDocument.Page page) { SQRLStorage storage = SQRLStorage.getInstance(activity); Canvas canvas = page.getCanvas(); int marginLeftRight = 35; int marginTop = 65; int lastBlockY = marginTop; TextPaint headlineTextPaint = new TextPaint(); headlineTextPaint.setTextSize(16); headlineTextPaint.setFakeBoldText(true); TextPaint bodyTextPaint = new TextPaint(); bodyTextPaint.setTextSize(12); TextPaint identityTextTextPaint = new TextPaint(bodyTextPaint); identityTextTextPaint.setTypeface(Typeface.MONOSPACE); identityTextTextPaint.setFakeBoldText(true); String identityTitle = "\"" + identityName + "\" SQRL Identity"; lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, identityTitle, Layout.Alignment.ALIGN_CENTER, headlineTextPaint, marginTop, marginLeftRight); lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, activity.getString(R.string.print_identity_desc1), Layout.Alignment.ALIGN_NORMAL, bodyTextPaint, lastBlockY + 20, marginLeftRight) + 20; byte[] saveData; if(this.withoutPassword) { saveData = storage.createSaveDataWithoutPassword(); } else { saveData = storage.createSaveData(); } int canvasMiddle = canvas.getWidth() / 2; QrCode qrCode = QrCode.encodeBinary(saveData, QrCode.Ecc.MEDIUM); Bitmap bitmap = qrCode.toImage(3, 0); int bitmapWidth = bitmap.getScaledWidth(canvas); canvas.drawBitmap(bitmap, canvasMiddle - (bitmapWidth / 2), lastBlockY + 20, new Paint()); lastBlockY += bitmap.getScaledHeight(canvas) + 20; lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, activity.getString(R.string.print_identity_desc2), Layout.Alignment.ALIGN_NORMAL, bodyTextPaint, lastBlockY + 20, marginLeftRight) + 30; int i = 0; try { Utils.refreshStorageFromDb(activity); for (String identityTextBlock : storage.getVerifyingRecoveryBlock().split("\n")) { lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, identityTextBlock, Layout.Alignment.ALIGN_CENTER, identityTextTextPaint, lastBlockY + 3, marginLeftRight) + 3; i++; } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } DocumentPrintUtils.drawTextBlock( canvas, activity.getString(R.string.print_identity_desc3), Layout.Alignment.ALIGN_NORMAL, bodyTextPaint, lastBlockY + 20, marginLeftRight); DocumentPrintUtils.drawPrintPageFooter(activity, canvas); }
Example #7
Source File: RescueCodePrintDocumentAdapter.java From secure-quick-reliable-login with MIT License | 4 votes |
private void drawPage(PdfDocument.Page page) { SQRLStorage storage = SQRLStorage.getInstance(activity); Canvas canvas = page.getCanvas(); int marginTop = 65; int marginLeft = 35; int lastBlockY = marginTop; TextPaint headlineText = new TextPaint(); headlineText.setAntiAlias(true); headlineText.setTextSize(24); headlineText.setFakeBoldText(true); TextPaint redBoldText = new TextPaint(); redBoldText.setAntiAlias(true); redBoldText.setTextSize(24); redBoldText.setFakeBoldText(true); redBoldText.setColor(Color.RED); TextPaint stdText = new TextPaint(); stdText.setAntiAlias(true); stdText.setTextSize(14); String headline = activity.getResources().getString(R.string.rescue_code_page_headline); String warning = "!! " + activity.getResources().getString(R.string.rescue_code_page_warning).toUpperCase() + " !!"; String description = activity.getResources().getString(R.string.rescue_code_page_description); lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, headline, Layout.Alignment.ALIGN_CENTER, headlineText, lastBlockY, marginLeft); lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, warning, Layout.Alignment.ALIGN_CENTER, redBoldText, lastBlockY + 40, marginLeft) + 40; lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, description, Layout.Alignment.ALIGN_NORMAL, stdText, lastBlockY + 20, marginLeft) + 20; List<String> rescueCode = storage.getTempShowableRescueCode(); StringBuilder sb = new StringBuilder(); boolean first = true; for (String s : rescueCode) { if (!first) sb.append("-"); sb.append(s); first = false; } String rescueCodeOutput = sb.toString(); lastBlockY += DocumentPrintUtils.drawTextBlock( canvas, rescueCodeOutput, Layout.Alignment.ALIGN_CENTER, redBoldText, lastBlockY + 60, marginLeft) + 60; String idName = activity.getResources().getString(R.string.txt_identity_name_hint) + ": __________________________________"; DocumentPrintUtils.drawTextBlock( canvas, idName, Layout.Alignment.ALIGN_CENTER, stdText, lastBlockY + 60, marginLeft); DocumentPrintUtils.drawPrintPageFooter(activity, canvas); }
Example #8
Source File: PdfPrinter.java From ViewPrinter with Apache License 2.0 | 4 votes |
/** * Prints the current view to a PDF file, in the given directory and with the given * filename. If the file exists, it will be deleted. * * @param printId an (optional) identifier for the process * @param directory a directory where the file will be saved * @param filename the output file name */ @Override public void print(final String printId, @NonNull final File directory, @NonNull String filename) { Context context = mDocument.getContext(); if (!checkPermission(context, directory)) return; if (!checkPreview(printId, directory, filename)) return; if (!filename.toLowerCase().endsWith(".pdf")) filename += ".pdf"; final File file = new File(directory, filename); if (!checkFile(printId, file)) return; if (mDocument.getPageCount() == 0) return; DocumentPage firstPage = mDocument.getPageAt(0); PrintSize size = mDocument.getPrintSize(); // Create doc PrintAttributes attrs = new PrintAttributes.Builder() .setColorMode(PrintAttributes.COLOR_MODE_COLOR) .setMediaSize(size.toMediaSize(firstPage)) .setMinMargins(PrintAttributes.Margins.NO_MARGINS) .build(); final PrintedPdfDocument doc = new PrintedPdfDocument(context, attrs); // Print page // Page canvas is passed in PostScript points. In order not to break View drawing, // we must scale that up back to pixels. dispatchOnPrePrint(mDocument); for (int i = 0; i < mDocument.getPageCount(); i++) { PdfDocument.Page page = doc.startPage(i); Canvas canvas = page.getCanvas(); float pixelsToPoints = PrintSize.PIXELS_TO_INCHES(context) * PrintSize.INCHES_TO_POINTS; canvas.scale(pixelsToPoints, pixelsToPoints, 0, 0); DocumentPage view = mDocument.getPageAt(i); Drawable background = null; if (!mPrintBackground) { background = view.getBackground(); view.setBackground(null); } view.draw(canvas); if (!mPrintBackground) { view.setBackground(background); } doc.finishPage(page); } dispatchOnPostPrint(mDocument); // I am not sure if the above would work with any view. Some views might be checking for // canvas.getWidth() or canvas.tryGetHeight(), which now are not consistent. If errors show up, // we might have to draw on a separate canvas and then scale the bitmap. // This has other drawbacks: the original pdf canvas, for example, takes text as text and // makes it selectable in the final PDF. // Print in a separate thread. // Since we're API 19 we can use try with resources. final Handler ui = new Handler(); new Thread(new Runnable() { @Override public void run() { try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) { doc.writeTo(stream); ui.post(new Runnable() { @Override public void run() { mCallback.onPrint(printId, file); } }); } catch (final IOException e) { ui.post(new Runnable() { @Override public void run() { mCallback.onPrintFailed(printId, new RuntimeException("Invalid file: " + file, e)); } }); } } }, TAG + "Worker").start(); }
Example #9
Source File: ExportController.java From prayer-times-android with Apache License 2.0 | 4 votes |
public static void exportPDF(Context ctx, Times times, @NonNull LocalDate from, @NonNull LocalDate to) throws IOException { PdfDocument document = new PdfDocument(); PdfDocument.PageInfo pageInfo; int pw = 595; int ph = 842; pageInfo = new PdfDocument.PageInfo.Builder(pw, ph, 1).create(); PdfDocument.Page page = document.startPage(pageInfo); Drawable launcher = Drawable.createFromStream(ctx.getAssets().open("pdf/launcher.png"), null); Drawable qr = Drawable.createFromStream(ctx.getAssets().open("pdf/qrcode.png"), null); Drawable badge = Drawable.createFromStream(ctx.getAssets().open("pdf/badge_" + LocaleUtils.getLanguage("en", "de", "tr", "fr", "ar") + ".png"), null); launcher.setBounds(30, 30, 30 + 65, 30 + 65); qr.setBounds(pw - 30 - 65, 30 + 65 + 5, pw - 30, 30 + 65 + 5 + 65); int w = 100; int h = w * badge.getIntrinsicHeight() / badge.getIntrinsicWidth(); badge.setBounds(pw - 30 - w, 30 + (60 / 2 - h / 2), pw - 30, 30 + (60 / 2 - h / 2) + h); Canvas canvas = page.getCanvas(); Paint paint = new Paint(); paint.setARGB(255, 0, 0, 0); paint.setTextSize(10); paint.setTextAlign(Paint.Align.CENTER); canvas.drawText("com.metinkale.prayer", pw - 30 - w / 2f, 30 + (60 / 2f - h / 2f) + h + 10, paint); launcher.draw(canvas); qr.draw(canvas); badge.draw(canvas); paint.setARGB(255, 61, 184, 230); canvas.drawRect(30, 30 + 60, pw - 30, 30 + 60 + 5, paint); if (times.getSource().drawableId != 0) { Drawable source = ctx.getResources().getDrawable(times.getSource().drawableId); h = 65; w = h * source.getIntrinsicWidth() / source.getIntrinsicHeight(); source.setBounds(30, 30 + 65 + 5, 30 + w, 30 + 65 + 5 + h); source.draw(canvas); } paint.setARGB(255, 0, 0, 0); paint.setTextSize(40); paint.setTextAlign(Paint.Align.LEFT); canvas.drawText(ctx.getText(R.string.appName).toString(), 30 + 65 + 5, 30 + 50, paint); paint.setTextAlign(Paint.Align.CENTER); paint.setFakeBoldText(true); canvas.drawText(times.getName(), pw / 2.0f, 30 + 65 + 50, paint); paint.setTextSize(12); int y = 30 + 65 + 5 + 65 + 30; int p = 30; int cw = (pw - p - p) / 7; canvas.drawText(ctx.getString(R.string.date), 30 + (0.5f * cw), y, paint); canvas.drawText(FAJR.getString(), 30 + (1.5f * cw), y, paint); canvas.drawText(Vakit.SUN.getString(), 30 + (2.5f * cw), y, paint); canvas.drawText(Vakit.DHUHR.getString(), 30 + (3.5f * cw), y, paint); canvas.drawText(Vakit.ASR.getString(), 30 + (4.5f * cw), y, paint); canvas.drawText(Vakit.MAGHRIB.getString(), 30 + (5.5f * cw), y, paint); canvas.drawText(Vakit.ISHAA.getString(), 30 + (6.5f * cw), y, paint); paint.setFakeBoldText(false); do { y += 20; canvas.drawText((from.toString("dd.MM.yyyy")), 30 + (0.5f * cw), y, paint); canvas.drawText(times.getTime(from, FAJR.ordinal()).toLocalTime().toString(), 30 + (1.5f * cw), y, paint); canvas.drawText(times.getTime(from, SUN.ordinal()).toLocalTime().toString(), 30 + (2.5f * cw), y, paint); canvas.drawText(times.getTime(from, DHUHR.ordinal()).toLocalTime().toString(), 30 + (3.5f * cw), y, paint); canvas.drawText(times.getTime(from, ASR.ordinal()).toLocalTime().toString(), 30 + (4.5f * cw), y, paint); canvas.drawText(times.getTime(from, MAGHRIB.ordinal()).toLocalTime().toString(), 30 + (5.5f * cw), y, paint); canvas.drawText(times.getTime(from, ISHAA.ordinal()).toLocalTime().toString(), 30 + (6.5f * cw), y, paint); } while (!(from = from.plusDays(1)).isAfter(to)); document.finishPage(page); File outputDir = ctx.getCacheDir(); if (!outputDir.exists()) outputDir.mkdirs(); File outputFile = new File(outputDir, times.getName().replace(" ", "_") + ".pdf"); if (outputFile.exists()) outputFile.delete(); FileOutputStream outputStream = new FileOutputStream(outputFile); document.writeTo(outputStream); document.close(); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("application/pdf"); Uri uri = FileProvider.getUriForFile(ctx, ctx.getString(R.string.FILE_PROVIDER_AUTHORITIES), outputFile); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); ctx.startActivity(Intent.createChooser(shareIntent, ctx.getResources().getText(R.string.export))); }
Example #10
Source File: ExportController.java From prayer-times-android with Apache License 2.0 | 4 votes |
public static void exportPDF(Context ctx, Times times, @NonNull LocalDate from, @NonNull LocalDate to) throws IOException { PdfDocument document = new PdfDocument(); PdfDocument.PageInfo pageInfo; int pw = 595; int ph = 842; pageInfo = new PdfDocument.PageInfo.Builder(pw, ph, 1).create(); PdfDocument.Page page = document.startPage(pageInfo); Drawable launcher = Drawable.createFromStream(ctx.getAssets().open("pdf/launcher.png"), null); Drawable qr = Drawable.createFromStream(ctx.getAssets().open("pdf/qrcode.png"), null); Drawable badge = Drawable.createFromStream(ctx.getAssets().open("pdf/badge_" + LocaleUtils.getLanguage("en", "de", "tr", "fr", "ar") + ".png"), null); launcher.setBounds(30, 30, 30 + 65, 30 + 65); qr.setBounds(pw - 30 - 65, 30 + 65 + 5, pw - 30, 30 + 65 + 5 + 65); int w = 100; int h = w * badge.getIntrinsicHeight() / badge.getIntrinsicWidth(); badge.setBounds(pw - 30 - w, 30 + (60 / 2 - h / 2), pw - 30, 30 + (60 / 2 - h / 2) + h); Canvas canvas = page.getCanvas(); Paint paint = new Paint(); paint.setARGB(255, 0, 0, 0); paint.setTextSize(10); paint.setTextAlign(Paint.Align.CENTER); canvas.drawText("com.metinkale.prayer", pw - 30 - w / 2f, 30 + (60 / 2f - h / 2f) + h + 10, paint); launcher.draw(canvas); qr.draw(canvas); badge.draw(canvas); paint.setARGB(255, 61, 184, 230); canvas.drawRect(30, 30 + 60, pw - 30, 30 + 60 + 5, paint); if (times.getSource().drawableId != 0) { Drawable source = ctx.getResources().getDrawable(times.getSource().drawableId); h = 65; w = h * source.getIntrinsicWidth() / source.getIntrinsicHeight(); source.setBounds(30, 30 + 65 + 5, 30 + w, 30 + 65 + 5 + h); source.draw(canvas); } paint.setARGB(255, 0, 0, 0); paint.setTextSize(40); paint.setTextAlign(Paint.Align.LEFT); canvas.drawText(ctx.getText(R.string.appName).toString(), 30 + 65 + 5, 30 + 50, paint); paint.setTextAlign(Paint.Align.CENTER); paint.setFakeBoldText(true); canvas.drawText(times.getName(), pw / 2.0f, 30 + 65 + 50, paint); paint.setTextSize(12); int y = 30 + 65 + 5 + 65 + 30; int p = 30; int cw = (pw - p - p) / 7; canvas.drawText(ctx.getString(R.string.date), 30 + (0.5f * cw), y, paint); canvas.drawText(FAJR.getString(), 30 + (1.5f * cw), y, paint); canvas.drawText(Vakit.SUN.getString(), 30 + (2.5f * cw), y, paint); canvas.drawText(Vakit.DHUHR.getString(), 30 + (3.5f * cw), y, paint); canvas.drawText(Vakit.ASR.getString(), 30 + (4.5f * cw), y, paint); canvas.drawText(Vakit.MAGHRIB.getString(), 30 + (5.5f * cw), y, paint); canvas.drawText(Vakit.ISHAA.getString(), 30 + (6.5f * cw), y, paint); paint.setFakeBoldText(false); do { y += 20; canvas.drawText((from.toString("dd.MM.yyyy")), 30 + (0.5f * cw), y, paint); canvas.drawText(times.getTime(from, FAJR.ordinal()).toLocalTime().toString(), 30 + (1.5f * cw), y, paint); canvas.drawText(times.getTime(from, SUN.ordinal()).toLocalTime().toString(), 30 + (2.5f * cw), y, paint); canvas.drawText(times.getTime(from, DHUHR.ordinal()).toLocalTime().toString(), 30 + (3.5f * cw), y, paint); canvas.drawText(times.getTime(from, ASR.ordinal()).toLocalTime().toString(), 30 + (4.5f * cw), y, paint); canvas.drawText(times.getTime(from, MAGHRIB.ordinal()).toLocalTime().toString(), 30 + (5.5f * cw), y, paint); canvas.drawText(times.getTime(from, ISHAA.ordinal()).toLocalTime().toString(), 30 + (6.5f * cw), y, paint); } while (!(from = from.plusDays(1)).isAfter(to)); document.finishPage(page); File outputDir = ctx.getCacheDir(); if (!outputDir.exists()) outputDir.mkdirs(); File outputFile = new File(outputDir, times.getName().replace(" ", "_") + ".pdf"); if (outputFile.exists()) outputFile.delete(); FileOutputStream outputStream = new FileOutputStream(outputFile); document.writeTo(outputStream); document.close(); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("application/pdf"); Uri uri = FileProvider.getUriForFile(ctx, ctx.getString(R.string.FILE_PROVIDER_AUTHORITIES), outputFile); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); ctx.startActivity(Intent.createChooser(shareIntent, ctx.getResources().getText(R.string.export))); }