美文网首页
Android开发iText7生成复杂的pdf

Android开发iText7生成复杂的pdf

作者: 你的益达233 | 来源:发表于2022-08-15 16:59 被阅读0次

由于牵扯到列表,就很难用view生成pdf方式。也找不到其他好用的方法,最后决定用陌生的iText7。可以说它的功能很强大,入门简单,精通难。

先来看下生成的pdf效果图
Screenshot_2022-08-15-16-30-58.jpg
需求要有基本文本,图片,表格,表格中有图片,页脚
一、添加文本
Paragraph pPO2 = new Paragraph();
    pPO2.setMarginTop(40);
    Text poText = new Text("Customer PO:");
    poText.setRelativePosition(0,0,200,0); //居右属性不生效,只能写绝对值调整
    pPO2.setTextAlignment(TextAlignment.RIGHT);
    pPO2.setWidth(ps.getWidth());
    pPO2.add(poText);
    //pPO2.setRelativePosition(150,100,0,0);
    document.add(pPO2);

最后用Text包一层,如果直接给Paragraph文本有些情况会不显示

二、图片

网络图片:

    Paragraph pimage = new Paragraph();
    Image fox = new Image(ImageDataFactory.create(new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fb5e0844ba40b44cb91c7b86da4d9e8e217687557.png&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1662887423&t=92409dd36b81eeb9c21b29902356f62f")));
    pimage.add(fox);
    document.add(pimage);

res图片:

Bitmap imageBitmap = BitmapFactory.decodeResource(App.getAppContext().getResources(), R.mipmap.main_bg);
            //bitmap转byte[]
            ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOutStream);
            byte[] imageBytes = byteOutStream.toByteArray();

            Image image = new Image(ImageDataFactory.create(imageBytes));  
三、表格
public static Table setTable(PdfFont font, String[] title, List<ProductList> orderProducts) {
    //8列
    Table table = new Table(8,true);

    for (String head : title) {
        table.addHeaderCell(createHeadCell(head, font));
    }
    for (ProductList orderProduct : orderProducts) {
        //table.addCell(createCell(orderProduct.getItemNo(), font));
        table.addCell(createImageCell(orderProduct.getImages()));
        table.addCell(createCell(orderProduct.getItemNo(), font));
        table.addCell(createCell(orderProduct.getDescription(), font));
        table.addCell(createCell(" ", font));
        table.addCell(createCell("1.0", font));
        table.addCell(createCell("Case", font));
        table.addCell(createCell("$9999", font));
        table.addCell(createCell("$9999", font));
    }
    return table;
}

document.add(table);  
四、表格中显示图片
Image image = new Image(ImageDataFactory.create(beforeSaveFilePath));
            image.setWidth(60).setHeight(50);
            //pimage.add(pimage);
            cell.add(image);  
五、页脚
public class DemoHeaderFooterHandler implements IEventHandler {


private final PdfFont font;
private final String fileName;

public DemoHeaderFooterHandler(String fileName, final PdfFont font) {
    this.font = font;
    this.fileName = fileName;
}

@Override
public void handleEvent(final Event event) {
    try {
        final PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        final PdfDocument pdfDoc = docEvent.getDocument();
        final Document doc = new Document(pdfDoc);
        final PdfPage page = docEvent.getPage();
        final int pageNumber = pdfDoc.getPageNumber(page);
        final Rectangle pageSize = page.getPageSize();
        final float pdfWidth = pageSize.getWidth();
        final float pdfHeight = pageSize.getHeight();

        if (pageNumber == pdfDoc.getNumberOfPages()){
            App.pdfNumMap.put(this.fileName,pageNumber);
        }
        Paragraph pageNoParagraph1 = new Paragraph();
        final Text textNum = new Text("Page "+pageNumber+" of "+ App.pdfNumMap.get(this.fileName));
        textNum.setBold();
        textNum.setFontSize(18);
        textNum.setFontColor(new DeviceRgb(0, 0, 0));
        pageNoParagraph1.add(textNum);
        final Text textNumCopy = new Text("[Customer Copy]");
        textNumCopy.setBold();
        textNumCopy.setFontSize(16);
        textNumCopy.setFontColor(new DeviceRgb(0, 0, 0));
        textNumCopy.setRelativePosition(340,0,0,0);
        pageNoParagraph1.add(textNumCopy);
        pageNoParagraph1.setRelativePosition(-20,pdfHeight-100,0,0);
        final String pageNo = "Logicicel time";
        final Paragraph pageNoParagraph = new Paragraph();
        pageNoParagraph.setFontSize(12f).setFont(PdfUtils.getChineseFont());
        pageNoParagraph.add(pageNo);
        pageNoParagraph.setRelativePosition(-20,pdfHeight-110,0,0);
        final Text textJieRi = new Text("[1629]61儿童节");
        textJieRi.setRelativePosition(400,0,0,0);
        pageNoParagraph.add(textJieRi);

        doc.add(pageNoParagraph1);
        doc.add(pageNoParagraph);



    } catch (final Exception e) {
        e.printStackTrace();

    }
}



}

其中因为在翻页时没法获取总页码的,现在我的做法是生成一次pdf,保存总页码,然后删掉pdf,再生成一次pdf,用上之前保存的总页码。

这里应该满足大部分需求了,还有其他特殊需求,可以私信或者评论,一起讨论下。要完整源码可私信我

相关文章

网友评论

      本文标题:Android开发iText7生成复杂的pdf

      本文链接:https://www.haomeiwen.com/subject/millgrtx.html