我是新的创建pdf文件在android从sqlite数据库我要创建一个pdf文件从sqlite数据库,下面的代码我已经为此目的做了,问题是,它只创建一个页面,忽略其他数据在sqlite,我想从所有的数据创建多个页面.
这是我为此而做的代码
try {
FontFactory.registerDirectories();
Rectangle pagesize = new Rectangle(8.5f * 72, 11 * 72);
Document document = new Document(pagesize, 72, 72, 72, 72);
File pdffile = new File(docFolder.getAbsolutePath(), ed.getText().toString() + ".pdf");
OutputStream outputStream = new FileOutputStream(pdffile);
PdfWriter writer=PdfWriter.getInstance(document,outputStream);
document.open();
Font font = FontFactory.getFont("assets/Tahoma.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 770, 569, 36);
column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
Cursor cr=dbh.getSpecialOutcome(vendor);
StringBuffer stringBuffer=new StringBuffer();
int num=1;
if(cr!=null && cr.getCount()>0) {
stringBuffer.append("تشریحات" + "\n\n");
int count=cr.getCount();
int pageto=count/4;
for (int j=1;j<=pageto;j++) {
int to=4;
int i;
for( i=num;i<=to;i++){
stringBuffer.append("شماره: " + cr.getString(0) + "\n");
stringBuffer.append("تشریحات: " + cr.getString(1) + "\n");
stringBuffer.append("مقدار: " + cr.getString(2) + "\n");
stringBuffer.append("د پیسی ډول: " + cr.getString(3) + "\n");
stringBuffer.append("مصرفوونکی: " + cr.getString(4) + "\n");
stringBuffer.append("\n\n");
cr.moveToNext();
if(i==4){
num=i+4;
to=to+4;
break;
}
}
column.addElement(new Paragraph(stringBuffer.toString(), font));
column.go();
document.newPage();
}
column.addElement(new Paragraph(stringBuffer.toString(), font));
column.go();
}
document.close();
Toast.makeText(getApplicationContext(), "File Created",Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
1条答案
按热度按时间ujv3wf0j1#
这里pFileList是图像文件列表,这个方法返回pdf文件,就像你输入图像文件列表一样
我使用
implementation 'com.itextpdf:itextg:5.5.10'
这个库来创建pdf文件。