In ra nội dung ngoài một bức ảnh đơn giản trên Android yêu cầu phải soạn văn bản và đồ họa trong tài liệu in. Khung Android cung cấp cách sử dụng HTML để soạn tài liệu và in tài liệu đó với ít mã nhất
Trong Android4. 4 [API cấp 19], lớp
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 đã được cập nhật để cho phép in nội dung HTML. Lớp này cho phép bạn tải tài nguyên HTML cục bộ hoặc tải xuống một trang từ web, tạo lệnh in và gửi nó cho các dịch vụ in của AndroidTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Bài học này chỉ cho bạn cách tạo nhanh một tài liệu HTML có chứa văn bản và đồ họa và sử dụng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 để in tài liệu đóTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Tải một tài liệu HTML
In tài liệu HTML bằng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 liên quan đến việc tải tài nguyên HTML hoặc xây dựng tài liệu HTML dưới dạng chuỗi. Phần này mô tả cách xây dựng một chuỗi HTML và tải nó vào mộtTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 để inTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Đối tượng dạng xem này thường được sử dụng như một phần của bố cục hoạt động. Tuy nhiên, nếu ứng dụng của bạn không sử dụng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8, bạn có thể tạo một thể hiện của lớp cụ thể cho mục đích in ấn. Các bước chính để tạo chế độ xem in tùy chỉnh này làTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
- Tạo một
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "
3 bắt đầu lệnh in sau khi tải tài nguyên HTMLTest Content
Testing, " + "testing, testing...
- Tải tài nguyên HTML vào đối tượng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "
8Test Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Mẫu mã sau minh họa cách tạo một
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "3 đơn giản và tải một tài liệu HTML được tạo một cách nhanh chóngTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Testing, testing, testing... Testing, " +
"testing, testing...Kotlin
private var mWebView: WebView? = null
private fun doWebViewPrint[] {
// Create a WebView object specifically for printing
val webView = WebView[activity]
webView.webViewClient = object : WebViewClient[] {
override fun shouldOverrideUrlLoading[view: WebView, request: WebResourceRequest] = false
override fun onPageFinished[view: WebView, url: String] {
Log.i[TAG, "page finished loading $url"]
createWebPrintJob[view]
mWebView = null
}
}
// Generate an HTML document on the fly:
val htmlDocument =
"
Test Content
Java
private WebView mWebView;
private void doWebViewPrint[] {
// Create a WebView object specifically for printing
WebView webView = new WebView[getActivity[]];
webView.setWebViewClient[new WebViewClient[] {
public boolean shouldOverrideUrlLoading[WebView view, String url] {
return false;
}
@Override
public void onPageFinished[WebView view, String url] {
Log.i[TAG, "page finished loading " + url];
createWebPrintJob[view];
mWebView = null;
}
}];
// Generate an HTML document on the fly:
String htmlDocument = "
Test Content
Ghi chú. Đảm bảo lệnh gọi tạo lệnh in của bạn diễn ra theo phương thức
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "6 củaTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "3 bạn đã tạo trong phần trước. Nếu bạn không đợi cho đến khi quá trình tải trang kết thúc, bản in ra có thể không đầy đủ hoặc trống hoặc có thể bị lỗi hoàn toànTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Ghi chú. Đoạn mã ví dụ trên chứa một thể hiện của đối tượng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 để nó không bị thu gom rác trước khi lệnh in được tạo. Đảm bảo rằng bạn cũng làm như vậy trong quá trình triển khai của riêng mình, nếu không quá trình in có thể không thành côngTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Nếu bạn muốn đưa đồ họa vào trang, hãy đặt các tệp đồ họa vào thư mục
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "9 của dự án của bạn và chỉ định một URL cơ sở trong tham số đầu tiên của phương thứcTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
webView.loadDataWithBaseURL[ "file:///android_asset/images/", htmlBody, "text/HTML", "UTF-8", null ]0, như được hiển thị trong ví dụ mã sau
Kotlin
webView.loadDataWithBaseURL[
"file:///android_asset/images/",
htmlBody,
"text/HTML",
"UTF-8",
null
]
Java
webView.loadDataWithBaseURL["file:///android_asset/images/", htmlBody,
"text/HTML", "UTF-8", null];
Bạn cũng có thể tải một trang web để in bằng cách thay thế phương pháp
webView.loadDataWithBaseURL[ "file:///android_asset/images/", htmlBody, "text/HTML", "UTF-8", null ]0 bằng phương pháp
webView.loadDataWithBaseURL[ "file:///android_asset/images/", htmlBody, "text/HTML", "UTF-8", null ]2 như hình bên dưới
Testing, " +
"testing, testing... Testing, " +
"testing, testing...Kotlin
private WebView mWebView;
private void doWebViewPrint[] {
// Create a WebView object specifically for printing
WebView webView = new WebView[getActivity[]];
webView.setWebViewClient[new WebViewClient[] {
public boolean shouldOverrideUrlLoading[WebView view, String url] {
return false;
}
@Override
public void onPageFinished[WebView view, String url] {
Log.i[TAG, "page finished loading " + url];
createWebPrintJob[view];
mWebView = null;
}
}];
// Generate an HTML document on the fly:
String htmlDocument = "
2Test Content
Java
private WebView mWebView;
private void doWebViewPrint[] {
// Create a WebView object specifically for printing
WebView webView = new WebView[getActivity[]];
webView.setWebViewClient[new WebViewClient[] {
public boolean shouldOverrideUrlLoading[WebView view, String url] {
return false;
}
@Override
public void onPageFinished[WebView view, String url] {
Log.i[TAG, "page finished loading " + url];
createWebPrintJob[view];
mWebView = null;
}
}];
// Generate an HTML document on the fly:
String htmlDocument = "
3Test Content
Khi sử dụng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 để tạo tài liệu in, bạn nên lưu ý những hạn chế sauTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
- Bạn không thể thêm đầu trang hoặc chân trang, kể cả số trang, vào tài liệu
- Ví dụ, các tùy chọn in cho tài liệu HTML không bao gồm khả năng in các phạm vi trang. In trang 2 đến 4 của tài liệu HTML 10 trang không được hỗ trợ
- Phiên bản của
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "
8 chỉ có thể xử lý một lệnh in tại một thời điểmTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; } - Không hỗ trợ tài liệu HTML chứa thuộc tính in CSS, chẳng hạn như thuộc tính ngang
- Bạn không thể sử dụng JavaScript trong tài liệu HTML để kích hoạt in
Ghi chú. Nội dung của một đối tượng
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 được bao gồm trong bố cục cũng có thể được in sau khi nó đã tải tài liệuTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
Nếu bạn muốn tạo bản in đầu ra tùy chỉnh hơn và có toàn quyền kiểm soát nội dung vẽ trên trang in, hãy chuyển sang bài học tiếp theo. In một bài học tài liệu tùy chỉnh
Tạo lệnh in
Sau khi tạo một
private WebView mWebView; private void doWebViewPrint[] { // Create a WebView object specifically for printing WebView webView = new WebView[getActivity[]]; webView.setWebViewClient[new WebViewClient[] { public boolean shouldOverrideUrlLoading[WebView view, String url] { return false; } @Override public void onPageFinished[WebView view, String url] { Log.i[TAG, "page finished loading " + url]; createWebPrintJob[view]; mWebView = null; } }]; // Generate an HTML document on the fly: String htmlDocument = "8 và tải nội dung HTML của bạn, ứng dụng của bạn gần như đã hoàn thành một phần của quy trình in. Các bước tiếp theo là truy cậpTest Content
Testing, " + "testing, testing...
"; webView.loadDataWithBaseURL[null, htmlDocument, "text/HTML", "UTF-8", null]; // Keep a reference to WebView object until you pass the PrintDocumentAdapter // to the PrintManager mWebView = webView; }
webView.loadDataWithBaseURL[ "file:///android_asset/images/", htmlBody, "text/HTML", "UTF-8", null ]7, tạo bộ điều hợp in và cuối cùng là tạo lệnh in. Ví dụ sau minh họa cách thực hiện các bước này
Testing, " +
"testing, testing... Testing, " +
"testing, testing...Kotlin
private WebView mWebView;
private void doWebViewPrint[] {
// Create a WebView object specifically for printing
WebView webView = new WebView[getActivity[]];
webView.setWebViewClient[new WebViewClient[] {
public boolean shouldOverrideUrlLoading[WebView view, String url] {
return false;
}
@Override
public void onPageFinished[WebView view, String url] {
Log.i[TAG, "page finished loading " + url];
createWebPrintJob[view];
mWebView = null;
}
}];
// Generate an HTML document on the fly:
String htmlDocument = "
5Test Content
Java
private WebView mWebView;
private void doWebViewPrint[] {
// Create a WebView object specifically for printing
WebView webView = new WebView[getActivity[]];
webView.setWebViewClient[new WebViewClient[] {
public boolean shouldOverrideUrlLoading[WebView view, String url] {
return false;
}
@Override
public void onPageFinished[WebView view, String url] {
Log.i[TAG, "page finished loading " + url];
createWebPrintJob[view];
mWebView = null;
}
}];
// Generate an HTML document on the fly:
String htmlDocument = "
6Test Content
Ví dụ này lưu một thể hiện của đối tượng
webView.loadDataWithBaseURL[ "file:///android_asset/images/", htmlBody, "text/HTML", "UTF-8", null ]8 để ứng dụng sử dụng, điều này không bắt buộc. Ứng dụng của bạn có thể sử dụng đối tượng này để theo dõi tiến trình của lệnh in khi nó đang được xử lý. Phương pháp này hữu ích khi bạn muốn theo dõi trạng thái của lệnh in trong ứng dụng của mình để hoàn thành, thất bại hoặc hủy bỏ của người dùng. Không cần tạo thông báo trong ứng dụng vì khung in sẽ tự động tạo thông báo hệ thống cho lệnh in