我试过两种方式,
1)创建WebView并加载我的pdf文档,我的应用程序几乎完成了部分打印过程.但在那面临印刷问题.
它没有完整的A4纸张视图.任何人都可以帮助,我使用的代码如下,
public void createWebPagePrint(WebView webView) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
printAdapter = webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes.Builder builder = null;
builder = new PrintAttributes.Builder();
builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
PrintJob printJob = null;
printJob = printManager.print(jobName, printAdapter, builder.build());
if (printJob.isCompleted()) {
Toast.makeText(getApplicationContext(), "Print Complete", Toast.LENGTH_LONG).show();
} else if (printJob.isFailed()) {
Toast.makeText(getApplicationContext(), "Print Failed", Toast.LENGTH_LONG).show();
}
builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 1024, 720))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
https://developer.android.com/training/printing/html-docs.html
2)我尝试过使用pdf view lib,
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
Run Code Online (Sandbox Code Playgroud)
但与webview相比,那段时间我的观点越来越好.问题是只有可见视图在画布上绘制.打印视图不清晰.它不可读.我已经给出了页数,所以根据页数重复页面但打印视图与第一页相同.以下视图在打印时获得.

这是我的示例代码,
如果有人知道请帮助我.
上面的程序非常难.即使我没有得到解决方案.之后我想出了一个解决方案,它的工作对我来说非常好.1)要查看PDF文件,无需使用webview或外部pdf库加载.只需下载pdf文件并使用默认的pdf查看器查看.以下代码我使用过,
要下载文件,
import android.app.Activity;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class FileDownloader {
private static final int MEGABYTE = 1024 * 1024;
public static void downloadFile(String fileUrl, File directory, Activity activity){
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
//urlConnection.setRequestMethod("GET");
//urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer))>0 ){
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class DownloadFile extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0];
String fileName = strings[1];
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "Test");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try {
pdfFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile,InventoryStockActivity.this);
return null;
}
}
public void download(String viewUrl) {
new DownloadFile().execute(viewUrl, "Test.pdf");
Log.d("Download complete", "----------");
}
Run Code Online (Sandbox Code Playgroud)
查看pdf文件;
public void view() {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Test/" + "Test.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(InventoryStockActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
在清单中,
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)
当它的打开默认pdf查看器,将有打印菜单.从那里打印.
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |