我希望能够从android应用程序生成考勤表。考勤表格式如下图所示。我正在从Firebase数据库中获取数据(示例50200)。还有更多类似此数据(50200)的数据,我需要在其余的框中添加这些数据。
这是我从Firebase中获取学生ID的数据的方式。我只想以这些格式将这些学生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的屏幕截图。除pdf视图外,使用RecyclerView生成设备时,设备之间也会有所不同。这样做是正确的方法。
答案 0 :(得分: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文件。
我确信这将帮助尝试从android设备格式化pdf文件或使用其他语言创建具有特定格式的pdf文件的人们。谢谢大家。