在我的应用程序中,我将图像上传到公司服务器 这些图像将转换为 pdf 文档并上传

我为此使用内置的 PDFdocument,现在我的问题是页面是动态的,具体取决于用户选择或用相机拍摄的图像数量,图像被放入 ARRAYLIST(字符串)中 然后我调用照片的位置并使用canvas将其绘制到pdf中

现在我的问题是生成的 pdf 文档具有适当的页码 IE。如果用户选择 4 个图像,则会返回 4 页,但没有图像

我发现了问题我的第二个 for 循环无法正常工作,因为它没有给出我基本上想要的数字广告

PDF 代码

    PdfDocument document=new PdfDocument(); 
            // crate a page description 
            PdfDocument.PageInfo pageInfo; 
            PdfDocument.Page page; 
            Canvas canvas; 
            int i; 
 
            Bitmap image; 
 
**here I loop for page Creation** 
 
            for (i=0; i < list.size(); i++)  { 
 
                pageInfo=new PdfDocument.PageInfo.Builder(3000, 6000, 1).create(); 
                page=document.startPage(pageInfo); 

这里我循环更改图像在数组列表中的位置 这并没有像它应该的那样循环遍历数组列表位置

for(int t = 0; t< list.indexOf(0); t+=1){ 
 
                    canvas=page.getCanvas(); 
                    image=BitmapFactory.decodeFile(String.valueOf(list.get (t))); 
                    image.setDensity(DENSITY_XHIGH); 
                    canvas.drawBitmap(image, 1, 1, null); 
                    canvas.setDensity(DENSITY_XHIGH); 
                } 
 
 
 
            document.finishPage(page); 
        } 
 
 
        String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/"; 
            File file=new File(directory_path); 
            if (!file.exists()) { 
                file.mkdirs(); 
            } 
            String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date()); 
            String targetPdf=directory_path + timeStamp + ".pdf"; 
            File filePath=new File(targetPdf); 
            try { 
                document.writeTo(new FileOutputStream(filePath)); 
                Toasty.info(this, "PDF Created", Toast.LENGTH_LONG).show(); 
            } catch (IOException e) { 
                Log.e("main", "error " + e.toString()); 
                Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show(); 
            } 
            // close the document 
            document.close(); 

请您参考如下方法:

1.使用PrintAttributes.build()正确设置pdf的属性

  • 并将 printattrs 传递给 PrinterpdfDocument(context,/*打印属性 obj 位于此处 */)

  • 创建 Rect() 对象

  • 使用 rect() 对象在 forloop 中创建综合浏览量并将这些综合浏览量添加到文档

  • 用户FileOutputStream(//您的pdf文件),将其传递给文档,然后

  • fileOutPutstream = new FileOutputStream(pdffilename); 
    doucument.writeTo(fileOutPutstream); 
    doucument.close(); 
    fileOutPutstream.close(); // close your stream  
    

    上面是粗略的实现,这应该会给你一个继续的想法。


    评论关闭
    IT干货网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!