PDF打印视图问题

时间:2018-03-06 06:50:45

标签: android printing

我试过两种方式,

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

https://developers.google.com/youtube/v3/docs/videos#resource

它没有完整的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();
    }


}

注意:

After printing am getting like this

  • 有些时候加载pdf时没有显示。

2)我尝试过使用pdf view lib,

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

但与webview相比,那段时间我的观点变得更好了。问题是只有可见视图在画布上绘制。打印视图不清晰。它不可读。我已经给出了页数,所以根据页数重复页面但打印视图与第一页相同。以下视图在打印时获得。 https://developer.android.com/training/printing/html-docs.html

这是我的示例代码

enter image description here

如果有人知道请帮助我。

1 个答案:

答案 0 :(得分:5)

上述程序非常难。即使我没有得到解决方案。之后我想出了一个解决方案,它对我来说非常合适。 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();
    }
}

在清单中,

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