打印Textviews和webview从片段



我正在使用这样的代码从webview创建pdf

PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE); 
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter("PDF");
printManager.print("PDF", printAdapter,
new PrintAttributes.Builder().build());

我应该怎么做,添加到这个pdf创建一些TextView,因为我需要添加额外的字段到我的片段?

了解textg,但我需要免费的

选择一个在webview中实现字段作为html的选项

你可以从webview中获得一个位图,然后使用位图画布在位图上绘制textview。

从那里,您可以将位图转换为pdf并共享pdf。

下面是将webview转换为位图的代码,也将位图转换为pdf,然后共享,要求用户选择其打印机应用程序:

public static void sharePdfFile(WebView webView, Context context)
{
Bitmap bitmap = webviewToBitmap( webView );
PrintedPdfDocument pdf =  bitmapToPdf( bitmap, context );
File file = pdfToFile( pdf, context );
shareFile( file,"application/pdf", context );
}
private static void shareFile(File file, String contentType, Context context)
{
Uri uri = FileProvider.getUriForFile(
context,
context.getPackageName() + ".fileprovider",
file);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType(contentType);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Toast.makeText(
context,
"Choose your printer app",
Toast.LENGTH_LONG
).show();
context.startActivity( shareIntent );
}
private static File pdfToFile(PrintedPdfDocument printedPdfDocument, Context context)
{
File file = new File(context.getFilesDir(), "share.pdf");
try {
FileOutputStream outputStream = new FileOutputStream(file);
printedPdfDocument.writeTo(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
printedPdfDocument.close();
return file;
}

private static PrintedPdfDocument bitmapToPdf(Bitmap bitmap, Context context)
{
PrintAttributes printAttributes = new PrintAttributes.Builder()
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.setResolution(new PrintAttributes.Resolution("1", "label", 300, 300))
.build();
PrintedPdfDocument printedPdfDocument = new PrintedPdfDocument(context, printAttributes);
PdfDocument.Page pdfDocumentPage = printedPdfDocument.startPage(1);
Canvas pdfCanvas = pdfDocumentPage.getCanvas();
bitmap = scaleBitmapToHeight(bitmap, pdfCanvas.getHeight());
pdfCanvas.drawBitmap(bitmap, 0f, 0f, null);
printedPdfDocument.finishPage(pdfDocumentPage);
return printedPdfDocument;
}
private static Bitmap webviewToBitmap(WebView webView) {
webView.measure(
View.MeasureSpec.makeMeasureSpec(
0,
View.MeasureSpec.UNSPECIFIED
),
View.MeasureSpec.makeMeasureSpec(
0,
View.MeasureSpec.UNSPECIFIED
)
);
int webViewWidth = webView.getMeasuredWidth();
int webViewHeight = webView.getMeasuredHeight();
webView.layout(0,0, webViewWidth, webViewHeight );
Bitmap bitmap = Bitmap.createBitmap(webViewWidth, webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
return bitmap;
}
private static Bitmap scaleBitmapToHeight(Bitmap bitmap, int maxHeight) {
int height = bitmap.getHeight();
if(height > maxHeight) {
int width = bitmap.getWidth();
float scalePercentage = ((float)maxHeight) / height;
return Bitmap.createScaledBitmap(bitmap, (int) (width * scalePercentage), maxHeight, false);
}
return bitmap;
}

最新更新