簡體   English   中英

如何獲得PDF中的完整活動

[英]How can I get My Full Activity in PDF

首先,我是法國人,我會盡力用一口好英語來解釋我的問題。

因此,我想在該屏幕上轉換為PDF: listeTrouActivity.java

但正如您所看到的,有滾動視圖,並且“孔”列下降到18個孔。

所以我遵循了一些教程:

http://valokafor.com/how-to-convert-android-view-to-pdf/#comment-1018

我開始使用ITextG庫

但是我的問題是,當我將位圖轉換為PDF時,它會給我一個僅具有屏幕大小的PDF。 我的活動被完全切斷了。

因此,我希望你們能為我提供幫助,在我的活動ListTrouActivity.java中,有一些代碼將位圖轉換為PDF:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    CoursePdf coursePdf = new CoursePdf();
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == R.id.saveHasPDF) {
        View mRootView = findViewById(R.id.RootView);
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)){
            Toast.makeText(ListTrouActivity.this, "OK", Toast.LENGTH_SHORT).show();
        }

        File pdfDir = new File(DEFAULT_PATH, "MyApp");
        if (!pdfDir.exists()){
            pdfDir.mkdirs();
        }

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        LinearLayout root = (LinearLayout) inflater.inflate(R.layout.liste_trou_activity, null); //RelativeLayout is root view of my UI(xml) file.
        root.setDrawingCacheEnabled(true);
        Bitmap screen = coursePdf.getBitmapFromView2(this.getWindow().findViewById(R.id.RootView));

        // here give id of our root layout (here its my RelativeLayout's id)

        File pdfFile = new File(pdfDir, "myPdfFile.pdf");

        try {
            Rectangle pagesize = new Rectangle(1720f, 3200f);
            Document document = new Document(pagesize, 36f, 72f, 108f, 180f);

            PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
            document.open();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            coursePdf.addImage(document,byteArray);
            document.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(new File(pdfDir,  "myPdfFile.pdf"));
        intent.setDataAndType(uri, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

CoursePDF.java中的getBitmapFromView的代碼:

    public static Bitmap getBitmapFromView2(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null)
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

我的CoursePDF.java中的addImage代碼:

public static void addImage(Document document, byte[] byteArray)
{
    Image image = null;
    try
    {
        image = Image.getInstance(byteArray);
    }
    catch (BadElementException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (MalformedURLException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // image.scaleAbsolute(150f, 150f);
    try
    {
        document.add(image);
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

不幸的是,我無法在此處粘貼liste_trou_activity.xml代碼,否則我將達到157 000個字符。

如果你們真的需要它,請詢問,我會再試一次。

希望大家都明白我要的是什么,如果沒有,請再問一次,我會盡力再次解釋。

感謝您的閱讀,希望你們能幫助我。

看看我能想到的最好的辦法是拍攝完整的可滾動活動的快照,然后動態地將其內容作為快照編寫pdf。 對於快照,請選擇[為可滾動活動拍攝快照]

然后寫動態pdf。 你會得到你想要的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM