TPPDF:在Table()中的同一单元格中添加带有文本内容的多个图像。

问题描述 投票:0回答:1

我正在开发一个PDF生成器应用程序,并通过以下方式来实现。这个 教程。

有没有什么方法可以在一个单元格中添加一些文字与多个图像。我尝试了不同的方法,但它的结果是在PDF中出现错误。

我已经尝试了这一点。

      let table = PDFTable()

            let str = String(format: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
            let str1 = String(format: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. \nLorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \n\nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. ")

            do {
                try table.generateCells(
                    data:[
                        ["Name", "Image", "Description"],
                        [project.name + "\n" +  project.address1, UIImage(named: "demo")!, str],
                        [project.name + "\n" +  project.address1, UIImage(named: "demo")!, str1]
                        ],
                    alignments: [
                    [.topLeft, .top, .top],
                    [.topLeft, .top, .top],
                    [.topLeft, .top, .top]
                        ])
                } catch PDFError.tableContentInvalid(let value) {
                    // In case invalid input is provided, this error will be thrown.

                    print("This type of object is not supported as table content: " + String(describing: (type(of: value))))
                } catch {
                    // General error handling in case something goes wrong.

                    print("Error while creating table: " + error.localizedDescription)
                }

    table.widths = [
               0.23, 0.30, 0.47
           ]

        let style = PDFTableStyleDefaults.simple
    table.style = style

    do {
        // Style each cell individually
        try table.setCellStyle(row: 1, column: 0, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
        try table.setCellStyle(row: 1, column: 1, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
        try table.setCellStyle(row: 1, column: 2, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
        try table.setCellStyle(row: 2, column: 0, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
        try table.setCellStyle(row: 2, column: 1, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
        try table.setCellStyle(row: 2, column: 2, style: PDFTableCellStyle(colors: (fill: UIColor.lightGray, text: UIColor.black)))
    } catch PDFError.tableIndexOutOfBounds(let index, let length){
        // In case the index is out of bounds

        print("Requested cell is out of bounds! \(index) / \(length)")
    } catch {
        // General error handling in case something goes wrong.

        print("Error while setting cell style: " + error.localizedDescription)
    }
    table.padding = 1.0
    table.margin = 2.0

    table.showHeadersOnEveryPage = true

    document.add(table: table)

请帮助我找到一些方法。谢谢你的帮助。

swift pdf-generation tppdf
1个回答
0
投票

我很抱歉没有早点回复(不知为何我没有得到通知)。

  1. 尝试在表格单元格内使用一个组。
  2. 试试新的表格api & 逻辑(仍在开发中,请看分支。develop),如果这仍然不能解决你的问题,请打开一个新的问题。请提供一个代码示例项目让我重现你的用例。
© www.soinside.com 2019 - 2024. All rights reserved.