我尝试直接打印文件,但是它不起作用,因此我从url下载了pdf文件,并将其保存在本地目录中,然后尝试在其上应用“打印文档适配器”,但同时也会引发无法打印受密码保护的Pdf的错误。我尝试在加载到Webview后打印它,但是它只打印屏幕上显示的内容,而不打印Webview中加载的整个URL。是否有任何方法可以打印受密码保护的pdf文档?使用此代码可以完美地打印没有密码保护的PDF。 >
MyActivity
class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
File pdfTempDir = new File(PreviewPdfActivity.this.getFilesDir().getAbsolutePath() + File.separator + "PDFPrint");
if (!pdfTempDir.exists()) {
pdfTempDir.mkdir();
}
fileTemp = new File(pdfTempDir, filePath);
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(fileTemp, true);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (!downloadTask.isCancelled()) {
total += count;
// publishing the progress....
if (fileLength > 0) {
// only if total length is known
publishProgress((int) (total * 100 / fileLength));
}
output.write(data, 0, count);
}
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onPostExecute(String result) {
if (result != null)
Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
else {
if(fileTemp.exists())
{
PrintDocumentAdapter pda = new PrintDocumentAdapter(){
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(fileTemp);
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
} catch (FileNotFoundException ee){
//Catch exception
} catch (Exception e) {
//Catch exception
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras){
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
PrintManager printManager = (PrintManager) PreviewPdfActivity.this.getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
}
}
}
}