生成pdf格式并附加数据

问题描述 投票:0回答:1

我想能够从Android应用程序生成一个考勤表。考勤表格式如下图所示。我从firebase数据库中获取数据(示例50200)。有更多相同的数据(50200),我需要在其余的框中附加。

This is how my Attendance sheet should look like

这就是我从firebase获取StudentIds数据的方法。我只想将这些学生ID以这些格式编写到pdf中。

    if (dataSnapshot.exists()) {
                RVStudent.setVisibility(View.VISIBLE);
                EmptyViewStudent.setVisibility(View.GONE);

                int i = 1;
                for (DataSnapshot dataSnapshot1 : dataSnapshot.child(userID).child("Course").child(CourseCode).child("Students").getChildren()) {
                    studentId[i]= dataSnapshot1.getKey();
                    studentName[i]=dataSnapshot.child(userID).child("Course").child(CourseCode).child("Students").child(studentId[i]).child("StudentName").getValue(String.class);
                    studentserial[i]= String.valueOf(i);

                    listStudent.add(new StudentModel(studentId[i],studentName[i], studentserial[i],CourseCode,CourseName, UserProfileImageUrl));
                    i++;
                }

                if(listStudent.size()==0){
                    RVStudent.setVisibility(View.GONE);
                    EmptyViewStudent.setVisibility(View.VISIBLE);
                }


            }else{
                RVStudent.setVisibility(View.GONE);
                EmptyViewStudent.setVisibility(View.VISIBLE);
            }

我尝试了我的RecyclerView它有点工作,但没有提供所需的结果和pdf在某种程度上看起来很奇怪,因为它是RecyclerView的截图。除了使用RecyclerView生成它之外,pdf视图会改变设备到设备。这是正确的做法。

android pdf-generation
1个回答
0
投票

我在阅读了ITEXT文件后找到了解决方案。虽然我不知道它是否是最佳方式,但它对我来说非常有用。

 public void onDataChange(DataSnapshot dataSnapshot) {
            String studentId[] = new String[50];

            listStudentId.clear();
            if (dataSnapshot.exists()) {
             //   EmptyViewStudent.setVisibility(View.GONE);
                int i = 1;
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    studentId[i]= dataSnapshot1.getKey();
                    listStudentId.add(new StudentIdModel(studentId[i]));
                    i++;
                }

                if(listStudentId.size()==0){

                }
            }else{

            }

            try {
                Document document = new Document();
                File root = new File(Environment.getExternalStorageDirectory(),
                        "/sams_images/AttendanceSheet");
                root.mkdir();
                String sdcardhtmlpath = root.getPath().toString() + "/"+CourseCode+ ".pdf";
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(sdcardhtmlpath));

                document.open();
                PdfContentByte canvas = writer.getDirectContent();
                ColumnText ct = new ColumnText(canvas);
                ct.setSimpleColumn(200,750,400,780);
                String documentTitle = CourseCode + " " + CourseName;
                Paragraph paragraph = new Paragraph(new Phrase(2,documentTitle, FontFactory.getFont(FontFactory.TIMES_BOLD, 15)));//add ur string here
                ct.addElement(paragraph);
                ct.setAlignment(Element.ALIGN_CENTER);
                ct.go();

                writeData(writer, document);

            } catch (Exception e) {
                Log.v("PDFcreation", e.toString());
            }

        }

然后我实现了一个循环,将逐个创建每一组视图..

public void writeData(PdfWriter writer, Document document) throws DocumentException {

    int lrectllx = 70;
    int lrectlly = 720;
    int lrecturx = 280;
    int lrectury = 750;

    int rrectllx = 320;
    int rrectlly = 720;
    int rrecturx = 530;
    int rrectury = 750;

    String StudentId[] = new String[50];

    double cr= 9.50;

    for(int i=0; i<listStudentId.size(); i++){

        /* if dataIndex [i] is even number will create box at left side and for odd number create box at right side*/

        if (i%2 == 0){
            PdfContentByte canvas = writer.getDirectContent();
            /*-------------------------- Creating circle -------------------------------------*/
            canvas.circle(lrecturx- 30, (lrectlly+lrectury)/2, cr);
            canvas.setColorStroke(BaseColor.BLACK);

            /*-------------------------- Creating Rectangle -------------------------------------*/
            Rectangle rectangle = new Rectangle(lrectllx, lrectlly, lrecturx, lrectury);
            rectangle.setBorder(Rectangle.BOX);
            canvas.setColorStroke(BaseColor.BLACK);
            rectangle.setBorderWidth(1);
            canvas.rectangle(rectangle);

            /*--------------------------- Appending Text -------------------------------------*/
            ColumnText ct = new ColumnText(canvas);
            ct.setSimpleColumn(rectangle);
            StudentId[i] = listStudentId.get(i).getStudentId();
            Paragraph studentid = new Paragraph(new Phrase(20,StudentId[i], FontFactory.getFont(FontFactory.TIMES, 15)));//add ur string here
            studentid.setAlignment(Element.ALIGN_LEFT);
            studentid.setIndentationLeft(20);
            ct.addElement(studentid);
            ct.go();

            lrectlly = lrectlly - 42;
            lrectury = lrectury - 42;
           // lcx = lrecturx- 30 ;
           // lcy = (lrectlly+lrectury)/2;

        }else{
            PdfContentByte canvas = writer.getDirectContent();

            /*-------------------------- Creating circle -------------------------------------*/
            canvas.circle(rrecturx- 30, (rrectlly+rrectury)/2, cr);
            canvas.setColorStroke(BaseColor.BLACK);

            /*-------------------------- Creating Rectangle -------------------------------------*/
            Rectangle rectangle = new Rectangle(rrectllx, rrectlly, rrecturx, rrectury);
            rectangle.setBorder(Rectangle.BOX);
            rectangle.setBorderWidth(1);
            canvas.setColorStroke(BaseColor.BLACK);
            canvas.rectangle(rectangle);

            /*-------------------------- Appending Text ---------------------------------------*/
            ColumnText ct = new ColumnText(canvas);
            ct.setSimpleColumn(rectangle);
            StudentId[i] = listStudentId.get(i).getStudentId();
            Paragraph studentid = new Paragraph(new Phrase(20,StudentId[i], FontFactory.getFont(FontFactory.TIMES, 15)));//add ur string here
            studentid.setAlignment(Element.ALIGN_LEFT);
            studentid.setIndentationLeft(20);
            ct.addElement(studentid);
            ct.go();

            rrectlly = rrectlly - 42;
            rrectury = rrectury - 42;
        //    rcx = rrecturx- 30 ;
        //    rcy = (rrectlly+rrectury)/2;
        }
    }

    document.close();

}

最后这是我得到的pdf文件..

I did not have to sacrifice anything. As with RecyclerView there is always concern about the quality and how different mobile devices will capture different type of view according to the view attributes.

我相信这将有助于尝试从Android设备格式化pdf文件或使用其他语言创建具有特定格式的pdf文件的人数。问候......每个人。

© www.soinside.com 2019 - 2024. All rights reserved.