如何将RecyclerView列表转换为iText PDF?

huangapple go评论113阅读模式
英文:

How I can get recyclerView list to itext PDF?

问题

我想将RecyclerView列表添加到iText PDF文档中,RecyclerView是从用户输入创建的,我不知道用户会添加多少项,而且RecyclerView的每个项都包含许多文本。我该如何做?

英文:

I want to add recyclerView list to itext PDF document,
recyclerview is created from user input, I didn't Know how many item will user add,
and each item of recyclerView contain many text.

How I can do it?

答案1

得分: 0

用户添加的任何项目都将被添加到一个列表中,然后该列表将填充recyclerView。因此,您可以轻松迭代列表,并针对列表中的每个元素将详细信息添加到pdf中。下面是一个示例,您可以在此处找到完整的代码。

private void createPdf(Order order) throws FileNotFoundException, DocumentException {
       
        File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documents");
        if (!docsFolder.exists()) {
            docsFolder.mkdir();
        }
        String pdfName = order.getOrderId() + "ST.pdf";

        // 这是产品项目列表,用于填充recyclerView
        List<ProductItem> items = order.getItemsList();
        
        File pdfFile = new File(docsFolder.getAbsolutePath(), pdfName);
        OutputStream output = new FileOutputStream(pdfFile);
        Document document = new Document(PageSize.A4);

        // 我们创建一个表格来存储每个产品的不同细节
        PdfPTable table = new PdfPTable(new float[]{3, 5, 3, 3});
        
        // 这是一些格式设置,使其看起来更好
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setFixedHeight(28);
        table.setTotalWidth(PageSize.A4.getWidth());
        table.setWidthPercentage(100);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);

        // 我们为表格的每一列添加标题
        table.addCell("Product Id");
        table.addCell("Name");
        table.addCell("Quantity");
        table.addCell("Price");
        table.setHeaderRows(1);
        PdfPCell[] cells = table.getRow(0).getCells();
        for (PdfPCell cell : cells) {
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        }
        
        // 在这里,我们遍历列表,并为列表中的每个产品添加一个带有产品详细信息的表格行。
        for (ProductItem item : items) {
            table.addCell(String.valueOf(item.getProductId()));
            table.addCell(String.valueOf(item.getProductName()));
            int quantity = item.getProductQuantity();
            table.addCell(String.valueOf(quantity));
            table.addCell(String.valueOf(item.getProductPrice() * quantity));
        }

        // 现在我们将表格添加到文档中
        PdfWriter.getInstance(document, output);
        document.open();
        document.add(table);
        document.close();
    }
英文:

What ever item the user adds, it will be added to a list, and then the list populates the recyclerView. So, you can easily iterate the list, and for every element in the list add the details in the pdf. Here is an example given below, you can find the complete code here.

private void createPdf(Order order) throws FileNotFoundException, DocumentException {
File docsFolder = new File(Environment.getExternalStorageDirectory() + &quot;/Documents&quot;);
if (!docsFolder.exists()) {
docsFolder.mkdir();
}
String pdfName = order.getOrderId() + &quot;ST.pdf&quot;;
// This is the list of product items, that populates the recyclerView
List&lt;ProductItem&gt; items = order.getItemsList();
File pdfFile = new File(docsFolder.getAbsolutePath(), pdfName);
OutputStream output = new FileOutputStream(pdfFile);
Document document = new Document(PageSize.A4);
// We create a table to store different details of each product
PdfPTable table = new PdfPTable(new float[]{3, 5, 3, 3});
// This is some formatting done to make it look good
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setFixedHeight(28);
table.setTotalWidth(PageSize.A4.getWidth());
table.setWidthPercentage(100);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
// We add the headers for each column of the table
table.addCell(&quot;Product Id&quot;);
table.addCell(&quot;Name&quot;);
table.addCell(&quot;Quantity&quot;);
table.addCell(&quot;Price&quot;);
table.setHeaderRows(1);
PdfPCell[] cells = table.getRow(0).getCells();
for (PdfPCell cell : cells) {
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}
// Here we iterate over the list, and for every product in the list we add a row in the table with details of the product.
for (ProductItem item : items) {
table.addCell(String.valueOf(item.getProductId()));
table.addCell(String.valueOf(item.getProductName()));
int quantity = item.getProductQuantity();
table.addCell(String.valueOf(quantity));
table.addCell(String.valueOf(item.getProductPrice() * quantity));
}
// Now we add the table to the document
PdfWriter.getInstance(document, output);
document.open();
document.add(table);
document.close();
}

答案2

得分: 0

作为一种替代方法,您不必使用itext,也不必处理其许可证,您可以使用Android内置的PDF类来创建PDF。

创建一个从RecyclerView生成多页PDF的示例可以在https://github.com/Zardozz/RecyclerviewPdf找到。

英文:

As an alternative you don't have to use itext and then have to deal with it's licensing, you can create a PDF using the Android built in PDF classes.

An example that creates a multiple page PDF from a recyclerview is at https://github.com/Zardozz/RecyclerviewPdf

huangapple
  • 本文由 发表于 2023年8月10日 22:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定