在动态输入上生成 pdf 结构

generating pdf structure on dynamic inputs

我有动态行和列。 列:它包含公司详细信息, 行:在特定的范围内。 数据:它包含绘制的图表

解释:
我的应用程序中的网格(假设应用程序的行 a_row 和列 a_col),其中 a_col 是公司名称(动态且用户可以添加更多 a_col)和 a_row 标题包含一些指标。 在 a_row 和 a_col 中包含图表(应用程序 table 格式网格包含 rowXcol 格式的图表)
现在 a_row 和 a_col 都是动态的,用户可以添加和删除图表和列。 (pdf 的行作为 p_row 和列作为 p_col)

对于 pdfexport:PdfPTable(3) 是 m 已修复的内容,即每个 a_col headers 将在 pdftable 行(仅 3 )中,在 3 之后,a_row 的图表将抽取那些 a_col headers.

申请:
COL1 COL2 <--- 公司名称
ROW1 图表 00 图表 01
ROW2 图表 10 图表 11
第 3 行 图表 20 图表 21

行是应用程序中的指标

要绘制的 PDF:(请检查图像 - pdf 快照)

代码完成: 它是动态的,因为没有图表(行和列),没有行是要绘制的不同页面。 下面的代码工作正常且准确(根据旧需要)。

创建 PDF:

        page2 = new Paragraph();
        page2.Add(new Chunk(CriteriaAll, CriteriaFONT));
        page2.Alignment = Element.ALIGN_CENTER;
        newtable();
        int j = 0, temp_tablecount = 0, tablecount = 0;
        int checkrcnt = (int)(Math.Ceiling(chartnameLIST.Count / 3.0d)) * 3;  //chartnameList is total charts rowXcol)
        int col_newpg_temp = 1;
        int col_newpg = (int)(Math.Ceiling(col_count / 12.0d)); (col_count is no of col.)
        for (int row_tb = 0; row_tb < row_count; row_tb++)
        {
            col_newpg_temp = 1;
            newpagedatafeed(pdfDoc, Metric[row_tb]);
            newtable();
            tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" "));
            for (int i = 0; i < checkrcnt; i++)
            {
                if (i < col_count)
                {
                    if (i == col_newpg_temp * 12)
                    {
                        pdfDoc.Add(tablepg1);
                        newtable();
                        newpagedatafeed(pdfDoc, Metric[row_tb]);
                        col_newpg_temp++;
                    }
                    tablepg1.AddCell(new Phrase(Companies[i], companiesH));
                }
                else
                    tablepg1.AddCell("");
                j++;
                if (j > 2)
                {
                    j = 0;
                    for (int k = 0; k < 3; k++)
                    {
                        if (temp_tablecount < col_count)
                        {
                            tablepg1.AddCell(ClipImpageProcess(chartnameLIST[tablecount], 8, writer));
                            tablecount++; temp_tablecount++;
                        }
                        else
                            tablepg1.AddCell("");
                    }
                }
            }//coll ends  
            temp_tablecount = 0;
            pdfDoc.Add(tablepg1);
        }
private static void newtable() //adds a new page
    {
        tablepg1 = new PdfPTable(3);
        tablepg1.DefaultCell.Border = 0;
        tablepg1.WidthPercentage = 95;
    }

    private static void newpagedatafeed(Document pdfDoc, string p) //somethng as a header
    {
        pdfDoc.NewPage();
        DrawLine(pdfDoc, writer.DirectContent);
        pdfDoc.Add(page2);
        tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" "));
        p = "\n" + p;
        PdfPTable temptable = new PdfPTable(3);
        temptable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
        temptable.DefaultCell.Border = 0;
        temptable.AddCell(""); temptable.AddCell(new Phrase(p, MetricH)); temptable.AddCell("");
        pdfDoc.Add(temptable);
    }

要做:(新需求)
需要编码帮助,
1. 如果只有 2 或 3 列,则图表应位于页面中间
2. 由于pdf页面只有很少的图表,所以下一行不要转到下一页,应该画在同一页上。

我将对你的问题做出一些假设。如果假设错误,您应该更新您的问题以澄清它。

请查看 CenterVertically 示例。在这个例子中,我创建了一个包含五行文本的单元格(你有图像而不是文本,但这应该无关紧要):

PdfPCell cell = new PdfPCell();
for (int i = 1; i <= 5; i++)
     cell.addElement(new Paragraph("Line " + i));

然后我创建两个 table。一个 table 三行,一个 table 十行。

这是应该适合单个页面的短 table:

table = new PdfPTable(1);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);

这是不适合一个页面的长 table,因此应该分布在不同的页面上:

table = new PdfPTable(1);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);

如果我对你的问题理解正确,你希望前者 table 不要拆分,而是垂直居中。第二个 table 需要拆分,因为它放不下一个页面。

这是通过使用这两种方法实现的:

private int status = ColumnText.START_COLUMN;
private float y_position = 0;

public void addTable(Document document, PdfContentByte canvas, PdfPTable table)
    throws DocumentException {
    Rectangle pagedimension = new Rectangle(36, 36, 559, 806);
    drawColumnText(document, canvas, pagedimension, table, true);
    Rectangle rect;
    if (ColumnText.hasMoreText(status)) {
        rect = pagedimension;
    }
    else {
        rect = new Rectangle(36, 36, 559, 806 - ((y_position - 36) / 2));
    }
    drawColumnText(document, canvas, rect, table, false);
}

public void drawColumnText(Document document, PdfContentByte canvas, Rectangle rect, PdfPTable table, boolean simulate)
    throws DocumentException {
    ColumnText ct = new ColumnText(canvas);
    ct.setSimpleColumn(rect);
    ct.addElement(table);
    status = ct.go(simulate);
    y_position = ct.getYLine();
    while (!simulate && ColumnText.hasMoreText(status)) {
        document.newPage();
        ct.setSimpleColumn(rect);
        status = ct.go(simulate);
    }
}

在第一种方法中,我们将 PdfPTable 添加到 A4 Document 中,边距为半英寸:

Rectangle pagedimension = new Rectangle(36, 36, 559, 806);

我们先在模拟模式下使用第二种方法。这意味着我们 假装 添加 table,但我们并没有真正添加它,我们只想知道 (1.) 如果 table 适合页面 (status),如果是,(2.) 花费了多少 space (y_position).

根据该信息,我们要么使用页面尺寸实际添加 table(如果 table 不适合页面),要么添加 table使用使 table 垂直居中的新尺寸(如果 table 适合页面)。

查看生成的 PDF:center_vertically.pdf

此示例向您展示如何找出 space 一个 table 的垂直长度。如果这不是您申请中缺少的 link,请重新表述问题。