0

我正在处理 pdf 生成任务,但我在 pdf 单元格中遇到问题。

这是我的代码:

public string SaveMinute(WrapperModel obj)
{
    string fileName = DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
    var objHelper = new PdfHelper();
    using (Document pdfDoc = new Document(
        PageSize.A4, 
        Utilities.MillimetersToPoints(-15), 
        Utilities.MillimetersToPoints(-15), 
        Utilities.MillimetersToPoints(10), 
        Utilities.MillimetersToPoints(7)))
    {
        CreateIfMissing(System.Web.HttpContext.Current.Server.MapPath("~/Uploads"));
        CreateIfMissing(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/Minutes"));
        string path = System.Web.HttpContext.Current.Server
            .MapPath(Path.Combine("~/Uploads/Minutes/", fileName));                
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(path, FileMode.Create));
        pdfDoc.Open();

        #region --- Page 1 ----
        PdfPTable mastertableP1 = new PdfPTable(1); // Main master table page 1
        mastertableP1.SplitLate = false; // it will stop table auto split to new paage
        mastertableP1.KeepTogether = true;
        mastertableP1.CompleteRow();
        mastertableP1.AddCell(objHelper.AddMinuteTable(obj)); 
        pdfDoc.Add(mastertableP1); // add master table to the pdf document
        #endregion --- Page 1 ----
        pdfDoc.Close();
        return string.Concat("/Uploads/Minutes/", fileName);
    }
}

这是我将单元格添加到 PDF 的功能

internal PdfPCell AddMinuteTable(WrapperModel objModel)
{

    string _projectName = "Minute Demo";

    PdfPTable tblHeader = new PdfPTable(1);
    tblHeader.AddCell(GetProjectDetails(ref objModel, _projectName));
    tblHeader.AddCell(AddEmptyCell());
    tblHeader.AddCell(GetAttendees(ref objModel));
    tblHeader.AddCell(AddEmptyCell());
    tblHeader.AddCell(GetMeetingData(ref objModel));
    tblHeader.AddCell(AddEmptyCell());
    tblHeader.AddCell(GetNextMeeting(ref objModel));
    return new PdfPCell(tblHeader) { Border = CellNoBorder() };
}

我创建了一些常用函数来创建表格和单元格

private PdfPTable AddNewTable(int column, 
        float[] columnwidth, 
        int horiontalAllign = 0, 
        int width = 100)
{
    PdfPTable tbl = new PdfPTable(column);
    tbl.HorizontalAlignment = horiontalAllign;
    tbl.WidthPercentage = width;
    tbl.SetWidths(columnwidth); 
    tbl.CompleteRow();
    return tbl;
}

internal PdfPCell AddNewCell(string phrase, 
        Font font, 
        int borderType, 
        string color = "", 
        string type = "L", 
        bool padTB = true)
{
    PdfPCell cell = new PdfPCell(new Phrase(phrase, font))
    {
        Border = borderType,
    };
    switch (type)
    {
        case "R":
            cell.PaddingRight = 5f;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            break;
        case "C":
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            break;
        default:
            cell.PaddingLeft = 5f;
            break;
    }
    if (padTB)
    {
        cell.PaddingTop = 2f;
        cell.PaddingBottom = 4f;
    }
    if (color != "")
    {
        BaseColor bColor = BaseColor.BLACK;
        bColor = GetBaseColor(color);
        cell.BackgroundColor = bColor;
    }
    return cell;
}

这就是我使用它的方式

private PdfPCell GetMeetingData(ref WrapperModel objModel)
{
    PdfPTable tblMeet = AddNewTable(4, new float[] { 0.5f, 6f, 2f, 1.5f });

    tblMeet.AddCell(AddNewCell("Sr", f_8b, CellFullBorder(), "", "R"));
    tblMeet.AddCell(AddNewCell("Minute", f_8b, CellFullBorder(), "", "C"));
    tblMeet.AddCell(AddNewCell("Owner", f_8b, CellFullBorder(), "", "C"));
    tblMeet.AddCell(AddNewCell("Date", f_8b, CellFullBorder(), "", "C"));

    foreach (var minute in objModel.MeetingObj)
    {
        if (minute.isParent == 1)
        {
            tblMeet.AddCell(AddNewCell(Convert.ToString(minute.PointNo), f_8b, Rectangle.TOP_BORDER, "blueL", "R"));
            PdfPCell cell = new PdfPCell(new Phrase(Convert.ToString(minute.Moment), f_8b))
            {
                Border = Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER,
                PaddingLeft = 5f,
                PaddingTop = 2f,
                PaddingBottom = 3f,
                Colspan = 3,
                BackgroundColor = GetBaseColor("blueL")
            };
            tblMeet.AddCell(cell);
        }
        else
        {
            tblMeet.AddCell(AddNewCell(Convert.ToString(minute.PointNo), f_8, Rectangle.TOP_BORDER, "", "R"));
            tblMeet.AddCell(AddNewCell(Convert.ToString(minute.Moment), f_8, Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER));
            tblMeet.AddCell(AddNewCell(Convert.ToString(minute.Owner), f_8, Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER));
            tblMeet.AddCell(AddNewCell(Convert.ToString(minute.Ddate), f_8, Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER));
        }
    }

    return new PdfPCell(tblMeet);
}

此代码在数据仅限于单个页面时有效,但如果数据较大则失败 有大空白空间的页面

它在 2 张桌子之间创建空白空间

我试图添加

mastertableP1.SplitLate = false; // it will stop table auto split to new paage
mastertableP1.KeepTogether = true;
mastertableP1.CompleteRow();

但它没有用。

4

0 回答 0