如何在Android中创建多页PDF文件从Android中SQLite数据?

3phpmpom  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(103)

我是新的创建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();
            }

        }



    }
ujv3wf0j

ujv3wf0j1#

这里pFileList是图像文件列表,这个方法返回pdf文件,就像你输入图像文件列表一样

fun createPdfFileFromFileArray(pFileList: ArrayList<File>, pPdfFileStorageDir: File, pPdfFileName: String): File {
val file = File(pPdfFileStorageDir.toString() + File.separator + pPdfFileName + ".pdf")
val document = Document()
PdfWriter.getInstance(document, FileOutputStream(file.absolutePath))
val documentRect: Rectangle = document.pageSize
document.open()
pFileList.forEachIndexed { _, filee ->
    document.newPage()
    val image: Image = Image.getInstance(filee.path)
    val pageWidth = document.pageSize.width
    val pageHeight = document.pageSize.height
    image.compressionLevel = PdfStream.BEST_SPEED
    image.scaleToFit(pageWidth, pageHeight)
    image.setAbsolutePosition((documentRect.width - image.scaledWidth) / 2, (documentRect.height - image.scaledHeight) / 2)
    document.add(image)
}
document.close()
return file}

我使用implementation 'com.itextpdf:itextg:5.5.10'这个库来创建pdf文件。

相关问题