使用Itextsharp自动分页

时间:2014-03-31 07:26:25

标签: c# pdf itextsharp

我在项目中更改了表的数量,并在所有表的末尾添加了图像。

我的问题是,如果我添加了太多表格,图像就会缩小,而不会移动到下一页。

此外,我还有更多报告要自动分页,所以如果一个表超出当前页面的范围,它会自动将此表移动到下一页。

我在VS 2010上用C#编写

这是我的代码的一部分:

private void CreateQCTable(Document mdoc, DataRow Headers, DataTable Table, String imgurl, int lb_or_ss)
{
    PdfPTable table;
    PdfPCell cell;
    CultureInfo currentCulture = new CultureInfo("he-IL");

    Font ArialSmall = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.BLACK);
    Font ArialSmallRowSpan = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.BLACK);
    Font ArialSmallGreen = FontFactory.GetFont("Arial", 7F, Font.NORMAL, new BaseColor(0, 100, 0));
    Font ArialSmallRed = FontFactory.GetFont("Arial", 7F, Font.NORMAL, BaseColor.RED);
    Font ArialSmallBlue = FontFactory.GetFont("Arial", 7F, Font.NORMAL, new BaseColor(0, 68, 136));
    Font ArialBold = FontFactory.GetFont("Arial", 8F, Font.BOLD, BaseColor.BLACK);

    String NewGroupName = Table.Rows[0]["group"].ToString();
    String OldGroupName = Table.Rows[0]["group"].ToString();
    int i = 0;
    while (i < Table.Rows.Count)
    {
        float[] headersparam = new float[Headers.ItemArray.Length - 1];
        for (int j = 0; j < Headers.ItemArray.Length - 1; j++)
        {
            headersparam[j] = 0.1f;
        }
        table = new PdfPTable(headersparam);
        table.WidthPercentage = 90;
        table.SpacingBefore = 0;


        int internalRowCount = 0;
        if (internalRowCount == 0)
        {
            cell = new PdfPCell(new Phrase("DATE: " + Table.Rows[i]["group"].ToString(), ArialBold));
            cell.PaddingTop = 25;
            cell.Colspan = Headers.ItemArray.Length - 1;
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.BorderWidth = 0f;
            cell.BorderWidthBottom = 0.6f;
            table.AddCell(cell);
            foreach (object obj in Headers.ItemArray)
            {
                if (obj.ToString() != "")
                {
                    cell = new PdfPCell(new Phrase(obj.ToString(), ArialBold));
                    cell.PaddingTop = 10;
                    cell.PaddingBottom = 5;
                    cell.Colspan = 1;
                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                    cell.BorderWidth = 0f;
                    cell.BorderWidthBottom = 0.6f;
                    table.AddCell(cell);
                }
            }

            internalRowCount++;
        }

        while (OldGroupName == NewGroupName && i < Table.Rows.Count)
        {
            i++;
            if (i < Table.Rows.Count)
                OldGroupName = Table.Rows[i]["group"].ToString();

            foreach (object obj in Table.Rows[i - 1].ItemArray)
            {
                if ( obj.ToString() != Table.Rows[i - 1]["group"].ToString())
                {
                    if (obj.ToString() == "PASSED")
                        AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmallGreen)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE);
                    else if(obj.ToString() == "FAILED")
                        AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmallRed)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE);
                    else
                        AddTextCell(table, new PdfPCell(new Phrase(obj.ToString(), ArialSmall)), Element.ALIGN_LEFT, 1, 1, 0, 0.5F, 0, 0, 5, 5, BaseColor.WHITE);

                }
            }

        }

        mdoc.Add(table);
        if (i < Table.Rows.Count)
        {
            NewGroupName = Table.Rows[i]["group"].ToString();

        }



    }
    table = new PdfPTable(new float[] { 0.1f});
    table.WidthPercentage = 90;
    table.SpacingBefore = 0;

    FileStream fs;
    fs = File.OpenRead(imgurl);
    int length = (int)fs.Length;
    byte[] logo = new byte[length];
    fs.Read(logo, 0, length);
    fs = null;
    var stream = new System.IO.MemoryStream();
    if (logo.Length != 0)
    {
        System.Drawing.Image img2 = System.Drawing.Image.FromStream(new MemoryStream(logo));
        img2.Save(stream, ImageFormat.Jpeg);
        stream.Position = 0;

    }

    cell = new PdfPCell(iTextSharp.text.Image.GetInstance(stream), true);
    cell.Rowspan = 1;
    cell.PaddingTop = 25;
    cell.PaddingBottom = 50;
    cell.PaddingLeft = 50;
    cell.PaddingRight = 50;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    cell.BorderWidth = 0f;
    table.AddCell(cell);

    mdoc.Add(table);
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

在Java中,人们会使用:

image.setScaleToFitHeight(false);

iTextSharp中的相应语法是:

Image.ScaleToFitLineWhenOverflow = false;

(见user2235615的评论)