我试过两种方法,
1)正在创建一个WebView并加载我的pdf文档,我的应用程序几乎完成了打印过程的一部分。但在这方面正面临着印刷问题。

它没有完整的A4工作表view.Can任何人请帮助,下面的代码我使用过,
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();
}
}注意:
https://developer.android.com/training/printing/html-docs.html
2)我尝试过使用with with库,
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'但与webview相比,这段时间的浏览效果更好。问题是,只有可见视图是在canvas.The上绘制的,打印视图不是clear.Its不可读的,我已经给出了页面计数,所以根据页面计数,它重复页面,但是打印视图与第一次page.The中相同,下面的视图是在打印时获取的。

这是我的代码样本
如果有人知道请帮帮我。
发布于 2018-05-03 10:29:18
上面的过程是非常hard.Even没有得到that.After的解决方案,我想出了一个解决方案,它完全适合我。1)查看PDF文件,无需加载webview或外部pdf libraries.Just,下载pdf文件,并在我使用过的代码下面使用默认pdf viewer.The查看它,
要下载文件,
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", "----------");
}查看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();
}
}在清单中,
<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>而当它的开放默认pdf查看器,将有打印menu.Just打印从那里。
https://stackoverflow.com/questions/49124930
复制相似问题