安卓:PdfDocument产生空PDFPdfDocument、PDF

2023-09-06 14:54:20 作者:月下琉璃绝

        PdfDocument document = new PdfDocument();
        // crate a page description
        PageInfo pageInfo = new PageInfo.Builder(300, 300, 1).create();
        // create a new page from the PageInfo
        Page page = document.startPage(pageInfo);
        // repaint the user's text into the page
        View content = findViewById(R.id.textarea);
        content.draw(page.getCanvas());
        // do final processing of the page
        document.finishPage(page);
        try {
             File f = getPDFPath();
             FileOutputStream fos = new FileOutputStream(f);
             document.writeTo(fos);
             document.close();
             fos.close();

        } catch (IOException e) {
            throw new RuntimeException("Error generating file", e);
        }

其中, findViewById(R.id.textarea); 指的是的TextView 有一些文字,但以上code只生成空的PDF格式。什么是问题?

Where findViewById(R.id.textarea); refers to a TextView with some text, but the above code generates only empty pdf. What can be the issue?

在那里已经工作采用Android原生API生成PDF样本有任何联系?

is there any link that have working sample of generating pdf using Android native API?

推荐答案

我有有,但很多的测试后,我意识到,我的观点是0 heigth和0的宽度,因为我是用一个TextView。 所以我设法等到视图(TextView的)会加载后开始创建文档,看看在code,希望你能解决这个问题:

i have the have, but after a lot of test, i realise that my View was with 0 heigth and 0 width, since i was using a TextView. So i managed to wait till view (TextView) will load and after start creating document, take a look at the code, hope you will fix it:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

     final TextView tv = (TextView) findViewById(R.id.textView1);
        ViewTreeObserver vto = tv.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 
                Toast.makeText(MainActivity.this, tv.getWidth() + " x " + tv.getHeight(), Toast.LENGTH_LONG).show();    

                try {
                    File file1 = new File("/mnt/sdcard/test/");
                    if(!file1.exists()){
                        file1.mkdirs();
                    }

                    File file = new File("/mnt/sdcard/test", "filename"+System.currentTimeMillis()+".pdf");
                    PrintAttributes printAttrs = new PrintAttributes.Builder().
                            setColorMode(PrintAttributes.COLOR_MODE_COLOR).
                            setMediaSize(PrintAttributes.MediaSize.ISO_A4).
                            setResolution(new Resolution("zooey", PRINT_SERVICE, 450, 700)).
                            setMinMargins(Margins.NO_MARGINS).
                            build();
                    PdfDocument document = new PrintedPdfDocument(MainActivity.this, printAttrs);
                     PageInfo pageInfo = new PageInfo.Builder(450, 700, 1).create();
                     Page page = document.startPage(pageInfo);

                     if (page != null) {

                           View view = findViewById(R.id.textView1);//getContentView();                          
                           view.layout(0, 0, view.getWidth(),
                                   view.getHeight());
                           Log.i("draw view", " content size: "+view.getWidth()+" / "+view.getHeight());
                           view.draw(page.getCanvas());
                           // Move the canvas for the next view.
                           page.getCanvas().translate(0, view.getHeight());
                       }    

                     document.finishPage(page);
                     os = new FileOutputStream(file);
                            document.writeTo(os);
                            document.close();
                            os.close();
                            Log.i("done", file.getAbsolutePath().toString());

                        } catch (IOException e) {
                            throw new RuntimeException("Error generating file", e);
                        }

                tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } 
        });

}

魔法里面的:

the magic inside:

 final TextView tv = (TextView) findViewById(R.id.textView1);
        ViewTreeObserver vto = tv.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 
         // create document here
} 
        });