HTML JPG PDF XML DOCX
  Product Family
PDF

在 Java SDK 中创建 PDF 表格

使用服务器端 Java API 为 PDF 文档创建具有扩展属性的表格。

Get Started

如何通过 Cloud Java SDK 为 PDF 创建表格

为了通过 Cloud Java SDK 为 PDF 创建表格,我们将使用 Aspose.PDF Cloud Java SDK 此 Cloud SDK 允许您轻松使用 Java 语言为各种云平台构建基于云的 PDF 创建、编辑和转换应用程序。打开 Repository 在包管理器中搜索 Aspose.PDF Cloud 并安装。您还可以使用包管理器控制台中的以下命令通过 Maven 安装它。

将 Aspose Cloud 仓库添加到您的应用程序 pom.xml 中

添加 Aspose Cloud 仓库


    <repositories>
        <repository>
            <id>aspose-cloud</id>
            <name>Aspose Cloud Repository</name>
            <url>https://releases.aspose.cloud/java/repo/</url>
        </repository>
    </repositories>

要将 API 客户端库安装到本地 Maven 仓库,只需执行以下操作:

从 Github 安装


    mvn clean install

要将其部署到远程 Maven 仓库,请配置仓库设置并执行以下操作:

部署 Maven 仓库


    mvn clean deploy

通过 Java SDK 为 PDF 创建表格的步骤

Aspose.PDF Cloud 开发者可以轻松加载并仅用几行代码创建具有扩展属性的 PDF 表格。

  1. 将文件上传至云存储
  2. 定义表格的外观
  3. 定义表格内容
  4. 将表格添加到 PDF 中
  5. 下载更新后的文件
 

此示例代码展示了向 PDF 文档添加表格


    import java.util.ArrayList;

    import com.aspose.asposecloudpdf.model.BorderInfo;
    import com.aspose.asposecloudpdf.model.Cell;
    import com.aspose.asposecloudpdf.model.Color;
    import com.aspose.asposecloudpdf.model.FontStyles;
    import com.aspose.asposecloudpdf.model.GraphInfo;
    import com.aspose.asposecloudpdf.model.Row;
    import com.aspose.asposecloudpdf.model.Table;
    import com.aspose.asposecloudpdf.model.TextRect;
    import com.aspose.asposecloudpdf.model.TextState;

    public class TableInitialize {
        public static Table create() {
            int numOfCols = 5;
            int numOfRows = 5;

            TextState textState = new TextState()
                .fontSize(10.);

            TextState headerState = new TextState()
                .fontSize(10.)
                .fontStyle(FontStyles.BOLD);

            Table table = new Table();        
            table.setRows(new ArrayList<>());

            String colWidths = "";
            for (int c = 0; c < numOfCols; c++)
            {
                colWidths += " 70";
            }
            table.columnWidths(colWidths);

            table.defaultCellTextState(textState);

            GraphInfo borderTableBorder = new GraphInfo();
            borderTableBorder.setColor(new Color().A(255).G(255));
            borderTableBorder.setLineWidth(1.);

            table.setDefaultCellBorder(new BorderInfo()
                .top(borderTableBorder)
                .right(borderTableBorder)
                .bottom(borderTableBorder)
                .left(borderTableBorder)
            );

            table.setTop(100.);

            for (int r = 0; r < numOfRows; r++) {
                Row row = new Row().cells(new ArrayList<>());

                for (int c = 0; c < numOfCols; c++) {
                    Cell cell = new Cell();
                
                    cell.setBackgroundColor(new Color().A(255).R(150).G(150).B(150));
                    cell.setParagraphs(new ArrayList<>());
                    cell.getParagraphs().clear();

                    // change properties on cell
                    if (r == 0) {
                        cell.setDefaultCellTextState(headerState);
                        cell.getDefaultCellTextState().setForegroundColor(new Color().A(255).B(255));
                        cell.getParagraphs().add(new TextRect().text(String.format("Col #%d", c)));
                    } else {
                        if (c == 0) {
                            cell.setDefaultCellTextState(headerState);
                            cell.getDefaultCellTextState().setForegroundColor(new Color().A(255).B(255));
                            cell.getParagraphs().add(new TextRect().text(String.format("Row #%d", r)));
                        } else {
                            cell.setDefaultCellTextState(textState);
                            cell.getDefaultCellTextState().setForegroundColor(new Color().A(255).R(255));
                            cell.getParagraphs().add(new TextRect().text(String.format(" Value: row #%d - col #%d", r, c)));
                        }
                    }
                    row.getCells().add(cell);
                }
                table.getRows().add(row);
            }
            return table;
        }
    }
    
 

在 PDF 中使用表格

表格提供了一种结构化的格式来系统地呈现数据,使读者更容易理解和分析信息。它们还提升了文档的视觉吸引力,增加了专业性和条理性。当处理数值或对比数据时,表格通过以易读的格式对相关信息进行分组,提升了清晰度。此外,表格可以加入实时或动态生成的内容,例如来自数据库或分析仪表盘的数据。 使用 Aspose.PDF Cloud Java SDK 为 PDF 文档创建具有扩展属性的表格。

使用我们的 Java 库,您可以:

  • 以文本或图像格式添加 PDF 文档的页眉和页脚。
  • 向 PDF 文档添加表格和印章(文本或图像)。
  • 将多个 PDF 文档附加到现有文件中。
  • 处理 PDF 附件、批注和表单字段。
  • 对 PDF 文档进行加密或解密并设置密码。
  • 删除页面或整个 PDF 文档中的所有印章和表格。
  • 按 ID 删除 PDF 文档中的特定印章或表格。
  • 替换 PDF 页面或整个文档中的单个或多个文本实例。
  • 对将 PDF 文档转换为各种其他文件格式提供广泛支持。
  • 提取 PDF 文件的各种元素并优化 PDF 文档。
  • 您可以试用我们的免费 App在线提取 PDF 文件中的表格并测试功能。

  •