使用Itextsharp自动分页 [英] Auto paging with Itextsharp

查看:196
本文介绍了使用Itextsharp自动分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have in my project changing number of tables and in the end of all of the tables I added image.

我的问题是,如果我添加了太多表,则图像只会缩小而不会移至下一页.

My problem is that if I added too many tables, the image just shrink and not moving to the next page.

我还有更多报告希望自动分页,因此如果一个表超出了当前页面的范围,它将自动将该表移至下一页.

Also I have more report that I want to auto paging them, so if one table is out of rage of current page, it automatically move this table to next page.

我在VS 2010上用C#编写

I write in C# on VS 2010

这是我的代码的一部分:

This is part of my code:

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);
    }

感谢帮助!

推荐答案

在Java中,人们会使用:

In Java one would use:

image.setScaleToFitHeight(false);

iTextSharp中的相应语法为:

The corresponding syntax in iTextSharp is:

Image.ScaleToFitLineWhenOverflow = false;

(请参阅 user2235615 的评论)

这篇关于使用Itextsharp自动分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆