- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这个布局,我想将屏幕上的所有内容打印成 PDF。包括 ScrollView 中的所有内容。稍后我还将尝试使用 IText 来执行此操作,但现在我想看看我是否可以使用 PrintedPdfDocument 和 View.draw 来执行此操作。我生成的 PDF 但只有部分布局在 PDF 上可见。
完整项目在:<强> https://github.com/ammar5001/PrintPDF2
这是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:id="@+id/textView"
android:textSize="24dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="Auther Name\nTitle\nContacts"
android:textSize="16dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Company\nStreet Address\nCity, State\nzip code"
android:id="@+id/textView3"
android:layout_below="@+id/textView"
android:layout_alignParentEnd="true"
android:textSize="16dp"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Print PDF"
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="printPDF" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_below="@id/textView3"
android:layout_above="@id/button1"
android:layout_marginTop="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="For some applications, such as drawing apps, page layout apps and other apps that focus on graphic output, creating beautiful printed pages is a key feature. In this case, it is not enough to print an image or an HTML document. The print output for these types of applications requires precise control of everything that goes into a page, including fonts, text flow, page breaks, headers, footers, and graphic elements."
android:id="@+id/textView4"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Creating print output that is completely customized for your application requires more programming investment than the previously discussed approaches. You must build components that communicate with the print framework, adjust to printer settings, draw page elements and manage printing on multiple pages. This lesson shows you how you connect with the print manager, create a print adapter and build content for printing."
android:id="@+id/textView5"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="When your application manages the printing process directly, the first step after receiving a print request from your user is to connect to the Android print framework and obtain an instance of the PrintManager class. This class allows you to initialize a print job and begin the printing lifecycle. The following code example shows how to get the print manager and start the printing process"
android:id="@+id/textView6"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The example code above demonstrates how to name a print job and set an instance of the PrintDocumentAdapter class which handles the steps of the printing lifecycle. The implementation of the print adapter class is discussed in the next section."
android:id="@+id/textView7"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
这是我正在使用的方法:
public void printPDF(View view){
if(isExternalStorageWritable()) {
String filename = getFileName();
File file = new File(getAlbumStorageDir("PDF"), filename);
try {
FileOutputStream outputStream = new FileOutputStream(file);
createPDF(outputStream);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
private void createPDF(FileOutputStream outputStream) throws IOException {
PrintedPdfDocument document = new PrintedPdfDocument(this,
getPrintAttributes());
// start a page
PdfDocument.Page page = document.startPage(1);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
//. . .
// add more pages
//. . .
// write the document content
document.writeTo(outputStream);
//close the document
document.close();
}
private View getContentView() {
return findViewById(R.id.relativeLayout);
}
最佳答案
解决方法是创建一个与 View 大小相同的位图,将其放在 Canvas 中并传递给 View.draw()。然后您可以缩放位图并将其绘制到 pdf Canvas 上。您甚至可以将其分成多个页面。
请记住, View 以像素为单位,而 pdf Canvas 以点或 1/72 英寸为单位。
明天我会在我的答案中添加一些代码。
编辑:这里有一些工作代码可以查看 pdf 或打印机:
public void printPDF(View view) {
PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
printManager.print("print_any_view_job_name", new ViewPrintAdapter(this,
findViewById(R.id.relativeLayout)), null);
}
public class ViewPrintAdapter extends PrintDocumentAdapter {
private PrintedPdfDocument mDocument;
private Context mContext;
private View mView;
public ViewPrintAdapter(Context context, View view) {
mContext = context;
mView = view;
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback, Bundle extras) {
mDocument = new PrintedPdfDocument(mContext, newAttributes);
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo.Builder builder = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(1);
PrintDocumentInfo info = builder.build();
callback.onLayoutFinished(info, true);
}
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
CancellationSignal cancellationSignal,
WriteResultCallback callback) {
// Start the page
PdfDocument.Page page = mDocument.startPage(0);
// Create a bitmap and put it a canvas for the view to draw to. Make it the size of the view
Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mView.draw(canvas);
// create a Rect with the view's dimensions.
Rect src = new Rect(0, 0, mView.getWidth(), mView.getHeight());
// get the page canvas and measure it.
Canvas pageCanvas = page.getCanvas();
float pageWidth = pageCanvas.getWidth();
float pageHeight = pageCanvas.getHeight();
// how can we fit the Rect src onto this page while maintaining aspect ratio?
float scale = Math.min(pageWidth/src.width(), pageHeight/src.height());
float left = pageWidth / 2 - src.width() * scale / 2;
float top = pageHeight / 2 - src.height() * scale / 2;
float right = pageWidth / 2 + src.width() * scale / 2;
float bottom = pageHeight / 2 + src.height() * scale / 2;
RectF dst = new RectF(left, top, right, bottom);
pageCanvas.drawBitmap(bitmap, src, dst, null);
mDocument.finishPage(page);
try {
mDocument.writeTo(new FileOutputStream(
destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
mDocument.close();
mDocument = null;
}
callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
}
}
关于android - 使用 PrintedPdfDocument 和 View.draw 从 Android 生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975339/
在 PrintedPdfDocument Canvas 中绘制 View 时,PDF 的字节大小会显着增加,尤其是当 View 包含位图(例如 ImageView)时。 减少最终大小的一种方法应该是
我正在尝试根据用户在我的 Android 应用程序中显示的表单创建 PDF 文档。我使用以下代码创建 PDF 文件,然后将其附加到电子邮件中: public static File generate(
我正在使用Android的内置PrintedPdfDocument使用 Canvas 绘制 PDF,如 this 中所述文档的示例代码: private void drawPage(PdfDocume
我正在尝试将 bmp 图像保存为 pdf: @Override protected void onActivityResult(int requestCode, int resultCode,
在遇到许多问题之后,我终于能够使用 PrintedPDFClass 生成具有内容且未损坏的 PDF 文档。 但是,问题是 PDF 中的内容似乎与我给它呈现的 View 没有任何关系...... 我要渲
有没有办法用一些模板打开现有的 PDF 文件并继续在其上绘图? 我正在使用https://developer.android.com/reference/android/print/pdf/Print
我经历过这个link创建一个包含文本的 sample.pdf 文件。 但由于此 API 提供了仅使用 Canvas 类来编写文本的方法。所以我无法从生成的 sample.pdf 文件(因为它被视为图像
我正在做Android的打印自定义文件,在PrintedPdfDocument的开发者网站上有一些疑惑 // open a new document PrintedPdfDocument doc
我正在尝试在 Android 上创建多页 PDF 文件。这是导致问题的我的代码的一部分: document.finishPage(page); // new page page
我有这个布局,我想将屏幕上的所有内容打印成 PDF。包括 ScrollView 中的所有内容。稍后我还将尝试使用 IText 来执行此操作,但现在我想看看我是否可以使用 PrintedPdfDocum
我是一名优秀的程序员,十分优秀!