PDF打印视图问题 [英] PDF printing view issue

查看:167
本文介绍了PDF打印视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种方式,

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

它不具有完整的A4图纸视图.任何人都可以帮忙,我使用了以下代码,

  public void createWebPagePrint(WebView webView){PrintManager printManager =(PrintManager)getSystemService(Context.PRINT_SERVICE);PrintDocumentAdapter printAdapter = null;如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.KITKAT){printAdapter = webView.createPrintDocumentAdapter();字符串jobName = getString(R.string.app_name)+"Document";PrintAttributes.Builder builder = null;builder =新的PrintAttributes.Builder();builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);PrintJob printJob = null;printJob = printManager.print(jobName,printAdapter,builder.build());如果(printJob.isCompleted()){Toast.makeText(getApplicationContext(),打印完成",Toast.LENGTH_LONG).show();}否则,如果(printJob.isFailed()){Toast.makeText(getApplicationContext(),打印失败",Toast.LENGTH_LONG).show();}builder.setMediaSize(PrintAttributes.MediaSize.ISO_A4).setResolution(新的PrintAttributes.Resolution("id",Context.PRINT_SERVICE,1024,720)).setColorMode(PrintAttributes.COLOR_MODE_COLOR).setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();}} 

注意:

这是我的示例代码,

代码

如果有人知道,请帮助我.

解决方案

上面的过程非常困难.即使没有解决方案,之后我也提出了一个解决方案,它对我来说是完美的.1)要查看PDF文件,无需加载webview或外部pdf库.只需下载pdf文件并使用默认的pdf查看器即可查看.我使用了以下代码,

要下载文件,

  import android.app.Activity;导入android.util.Log;导入java.io.File;导入java.io.FileNotFoundException;导入java.io.FileOutputStream;导入java.io.IOException;导入java.io.InputStream;导入java.net.HttpURLConnection;导入java.net.MalformedURLException;导入java.net.URL;公共类FileDownloader {私有静态最终int MEGABYTE = 1024 * 1024;公共静态无效downloadFile(String fileUrl,File directory,Activity activity){尝试 {URL url =新URL(fileUrl);HttpURLConnection urlConnection =(HttpURLConnection)url.openConnection();//urlConnection.setRequestMethod("GET);//urlConnection.setDoOutput(true);urlConnection.connect();InputStream inputStream = urlConnection.getInputStream();FileOutputStream fileOutputStream =新的FileOutputStream(目录);int totalSize = urlConnection.getContentLength();byte []缓冲区=新的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();}}}私有类DownloadFile扩展了AsyncTask< String,Void,Void>{@Override受保护的Void doInBackground(String ... strings){字符串fileUrl =字符串[0];字符串fileName =字符串[1];字符串extStorageDirectory = Environment.getExternalStorageDirectory().toString();文件夹=新文件(extStorageDirectory,测试");folder.mkdir();File pdfFile = new File(folder,fileName);尝试 {pdfFile.createNewFile();} catch(IOException e){e.printStackTrace();}FileDownloader.downloadFile(fileUrl,pdfFile,InventoryStockActivity.this);返回null;}}公共无效下载(字符串viewUrl){新的DownloadFile().execute(viewUrl,"Test.pdf");Log.d(下载完成","----------");} 

要查看pdf文件;

  public void view(){文件pdfFile =新文件(Environment.getExternalStorageDirectory()+"/Test/" +"Test.pdf");Uri路径= Uri.fromFile(pdfFile);Intent pdfIntent =新的Intent(Intent.ACTION_VIEW);pdfIntent.setDataAndType(path,"application/pdf");pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);尝试 {startActivity(pdfIntent);} catch(ActivityNotFoundException e){Toast.makeText(InventoryStockActivity.this,没有可用的应用程序来查看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查看器时,将出现打印菜单.只需从那里打印即可.

I have tried in two ways,

1) Am creating a WebView and loading my pdf document, and my application is almost done with its part of the printing process. But in that am facing printing issue.

Its not with full A4 sheet view.Can anyone please help,The following code i have used,

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


}

Note:

https://developer.android.com/training/printing/html-docs.html

  • And some times while loading pdf its not displaying.

2) I have tried using with pdf view lib ,

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

But that time am getting better view compared to webview. The problem is only visible view is drawing on canvas.The print view is not clear.Its not readable.I have given the page count, So according to the page count its repeating the pages but print view is same as in first page.The following view am getting while printing.

This is my sample code,

code

If anyone know please help me.

解决方案

The above procedure is very hard.Even am not getting solution for that.After that i come up with a solution and its working perfectly for me. 1) To view PDF file no need to load with webview or external pdf libraries.Just download the pdf file and view it with default pdf viewer.The below code i have used,

To download a file,

    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", "----------");
}

To view a pdf file;

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

In manifest,

  <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>

And when its open default pdf viewer, there will be print menu.Just print from there.

这篇关于PDF打印视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆