生成pdf格式并追加数据
Generate pdf format and append data
我希望能够从 android 应用程序生成出勤 sheet。考勤sheet格式如下图。我正在从 firebase 数据库中获取数据(示例 50200)。还有更多像这样的数据 (50200),我需要将其附加到其余的框中。
这就是我从 firebase 获取 StudentId 数据的方式。我只想将这些学生 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 生成它时因设备而异。这样做的正确方法是什么。
看了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 文件的人有所帮助。问候大家..
我希望能够从 android 应用程序生成出勤 sheet。考勤sheet格式如下图。我正在从 firebase 数据库中获取数据(示例 50200)。还有更多像这样的数据 (50200),我需要将其附加到其余的框中。
这就是我从 firebase 获取 StudentId 数据的方式。我只想将这些学生 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 生成它时因设备而异。这样做的正确方法是什么。
看了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 文件的人有所帮助。问候大家..