gpt4 book ai didi

android - PDF 打印 View 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:57 25 4
gpt4 key购买 nike

我试过两种方式,

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

After printing am getting like this

它不是完整的 A4 纸 View 。任何人都可以帮忙,我使用了以下代码,

    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

  • 有时在加载 pdf 时不显示。

2) 我试过使用 pdf View 库,

 compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

但与 webview 相比,那个时候我的视野越来越好。问题是只有可见 View 在 Canvas 上绘制。打印 View 不清晰。不可读。我已经给出了页数,因此根据页数重复页面但打印 View 与第一页相同。打印时正在获取以下 View 。 enter image description here

这是我的示例代码,

code

如果有人知道请帮助我。

最佳答案

上述过程非常困难。甚至我都没有找到解决方案。之后我想出了一个解决方案,它对我来说非常有效。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", "----------");
}

查看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();
}
}

在 list 中,

  <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 查看器时,会出现打印菜单。只需从那里打印即可。

关于android - PDF 打印 View 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49124930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com