提问者:小点点

使用多个活动数据创建pdf。itextpdfAndroid


我是Android的新手。现在正在做一个叫做学校报告的小项目。我喜欢从用户那里获得两个不同活动的输入,当用户点击createpdf按钮时,它应该在我的设备中创建PDF文件,其中包含来自两个活动的所有用户输入。这两个活动是报告和报告1,正如你在屏幕截图中看到的那样。问题是我不知道如何获取其他活动数据,例如:来自报告的数据。当我在报告活动中输入数据并单击保存时,创建包含所有数据的pdf文件。但我希望数据应该暂时存储在两个活动(报告、报告1)的设备中,当用户单击createpdf按钮(MainActivity)时,它应该使用两个活动数据创建pdf。

我正在使用itextpdf库。如果有人能重新排列java逻辑部分,那将是很大的帮助。提前谢谢你



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


    public void buttonone(View view) {
        Intent intent = new Intent(this, report.class);
        startActivity(intent);
    }
    public void buttontwo(View view) {
        Intent intent = new Intent(this, report1.class);
        startActivity(intent);
    }



    EditText et1,et2,et3,et4;
    Button Save;
    String edit1,edit2,edit3,edit4;
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1, cell2,cell3,cell4, cell5,cell6,cell7, cell8,cell9,cell10;
    File cacheDir;
    final Context context = this;

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

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Code Analyser");



        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();

        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        et4=(EditText)findViewById(R.id.editText4);


        Save=(Button)findViewById(R.id.button1);
        Save.setOnClickListener(reportClickListener);
    }
    OnClickListener reportClickListener= new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            edit1=et1.getText().toString();
            edit2=et2.getText().toString();
            edit3=et3.getText().toString();
            edit4=et4.getText().toString();

            String FILE = Environment.getExternalStorageDirectory().toString() + "/Code Analyser/" + "report.pdf";

            // Create New Blank Document
            Document document = new Document(PageSize.A4);

            // Create Pdf Writer for Writting into New Created Document
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                // Open Document for Writting into document
                document.open();
                // User Define Method
                addTitlePage(document);
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Close Document after writting all content
            document.close();

            Toast.makeText(report.this, "PDF File is Created."+FILE, Toast.LENGTH_LONG).show();
        }
    };
    // Set PDF document Properties
    public void addTitlePage(Document document) throws DocumentException
    {
        // Font Style for Document
        Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
        Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD| Font.UNDERLINE, BaseColor.GRAY);
        Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

        // Start New Paragraph
        Paragraph prHead = new Paragraph();
        // Set Font in this Paragraph
        prHead.setFont(titleFont);
        // Add item into Paragraph
        prHead.add("Code Analyzer\n");
        //prHead.add("\n");
        prHead.setAlignment(Element.ALIGN_CENTER);

        Paragraph cat = new Paragraph();
        cat.setFont(catFont);
        cat.add("\n");
        cat.add("Report\n");
        cat.add("\n");
        cat.setAlignment(Element.ALIGN_CENTER);

        // Add all above details into Document
        document.add(prHead);
        document.add(cat);
        document.add(table);

            /* Header values*/
        table = new PdfPTable(2);
        cell1 = new PdfPCell(new Phrase("Category"));
        cell2 = new PdfPCell(new Phrase("Values"));
        cell1.setVerticalAlignment(Element.ALIGN_LEFT);
        cell2.setVerticalAlignment(Element.ALIGN_LEFT);

        cell1.setBorder(Rectangle.NO_BORDER);
        cell1.setPadding(5);

        cell2.setBorder(Rectangle.NO_BORDER);
        cell2.setPadding(5);

        cell1.setBackgroundColor(BaseColor.GRAY);
        cell2.setBackgroundColor(BaseColor.GRAY);

            /*Table values*/
        cell3 = new PdfPCell(new Phrase("Name"));
        cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell3.setBorder(Rectangle.NO_BORDER);
        cell3.setPadding(5);

        cell4 = new PdfPCell(new Phrase(edit1));
        cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell4.setBorder(Rectangle.NO_BORDER);
        cell4.setPadding(5);

        cell5 = new PdfPCell(new Phrase("Mobile Number"));
        cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell5.setBorder(Rectangle.NO_BORDER);
        cell5.setPadding(5);

        cell6 = new PdfPCell(new Phrase(edit2));
        cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell6.setBorder(Rectangle.NO_BORDER);
        cell6.setPadding(5);

        cell7 = new PdfPCell(new Phrase("Mail Id"));
        cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell7.setBorder(Rectangle.NO_BORDER);
        cell7.setPadding(5);

        cell8 = new PdfPCell(new Phrase(edit3));
        cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell8.setBorder(Rectangle.NO_BORDER);
        cell8.setPadding(5);

        cell9 = new PdfPCell(new Phrase("City"));
        cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell9.setBorder(Rectangle.NO_BORDER);
        cell9.setPadding(5);

        cell10 = new PdfPCell(new Phrase(edit4));
        cell10.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell10.setBorder(Rectangle.NO_BORDER);
        cell10.setPadding(5);

        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
        table.addCell(cell7);
        table.addCell(cell8);
        table.addCell(cell9);
        table.addCell(cell10);

        // add table into document
        document.add(table);

        // Create new Page in PDF
        document.newPage();
        //Toast.makeText(this, "PDF File is Created.", Toast.LENGTH_LONG).show();
    }



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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Personal Details"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="buttonone"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Student Details"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:onClick="buttontwo"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create PDF"
    android:id="@+id/button3"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="createpdf"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:hint="Name"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mobile number"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mail id"
    android:ems="10" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="10dp"
    android:hint="City"
    android:ems="10" />


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/button9"
    android:layout_alignStart="@+id/button9" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button send"
    android:onClick="sendMessage"
    android:layout_marginBottom="136dp"
    android:id="@+id/button9"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:hint="Student Name"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mobile number"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:hint="Mail id"
    android:ems="10" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="10dp"
    android:hint="School name"
    android:ems="10" />


<Button
    android:id="@+id/buttonsave2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

截图


共1个答案

匿名用户

你可以通过几种不同的方式做到这一点:

1)使用“全局”数据概念存储应用程序全局数据

2)使用捆绑包将附加内容传递到Activity如何创建携带数据的android Intent?

3)写入文件和从文件读取

4)单独创建每个PDF文件。然后使用iTextPDF组合它们。