为什么在使用 iTextsharp 生成 pdf 时显示这些边框?

Why these borders are showing when generating pdf using iTextsharp?

我正在尝试将多个 pdf 生成一个 pdf,这是我通过使用 itextSharp 实现的,但是在生成它们时我遇到了一些事情,如下所示:

如何解决这些错误?下面是我的代码:

public byte[] GetPDF(string pHTML)
    {
        byte[] bPDF = null;
        MemoryStream ms = new MemoryStream();
        TextReader txtReader = new StringReader(pHTML);
        //Rectangle pagesize = new Rectangle(864.0f, 1152.0f);
        Document doc = new Document(PageSize.NOTE);
        string path = Server.MapPath("PDFs");
        PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
        HTMLWorker htmlWorker = new HTMLWorker(doc);
        doc.Open();

            for (int i = 1; i <= 5; i++)
            {

                doc.NewPage();
                PdfPTable table= new PdfPTable(1);
                table.TotalWidth = 500f;
                table.LockedWidth = true;
                table.HorizontalAlignment = 0;
                table.DefaultCell.Border = Rectangle.NO_BORDER;

                Image imageTopURL = Image.GetInstance("Top.PNG");
                       PdfPCell imgTopCell = new PdfPCell(imageTopURL);
                Paragraph p = new Paragraph("XYZ", new Font(Font.FontFamily.COURIER, 32f, Font.UNDERLINE));
                p.Alignment = Element.ALIGN_CENTER;
                table.AddCell(imgTopCell);
                table.AddCell(p);
                Image imageMidURL = Image.GetInstance("Mid.PNG");                    
                PdfPCell imgMidCell = new PdfPCell(imageMidURL);
                Paragraph p1 = new Paragraph("ABC", new Font(Font.FontFamily.HELVETICA, 29f, Font.ITALIC));
                p1.Alignment = Element.ALIGN_CENTER;
                table.AddCell(imgMidCell);
                imgMidCell.Border = 0;                 
                table.AddCell(p1);
                Image imageBotURL = Image.GetInstance("Bottom.PNG");
                PdfPCell imgBotCell = new PdfPCell(imageBotURL);
                table.AddCell(imgBotCell);
                imageTopURL.ScaleAbsolute(505f, 270f);
                imageMidURL.ScaleAbsolute(590f, 100f);
                imageBotURL.ScaleAbsolute(505f, 170f);
                 doc.Open();
                doc.Add(table);
                  htmlWorker.StartDocument();
                htmlWorker.Parse(txtReader);
                htmlWorker.EndDocument();
            }
              htmlWorker.Close();
               doc.Close();
        doc.Close();
        bPDF = ms.ToArray();
        return bPDF; 
}

您是在告诉 table 默认单元格不应该有边框:

table.DefaultCell.Border = Rectangle.NO_BORDER;

这意味着 隐式 创建的 PdfPCell 个实例不会获得边框。例如:如果你这样做:

table.AddCell("Implicit cell creation");

那么该单元格将不会有边框。

但是:您正在创建一个单元格显式:

PdfPCell imgTopCell = new PdfPCell(imageTopURL);

在这种情况下,从不使用 DefaultCellimgTopCell有边框是很正常的。如果你不想要 imgTopCell 的边框,你需要像这样定义 imgTopCellBorder

imgTopCell.Border = Rectangle.NO_BORDER;

关于对齐:您似乎没有了解文本模式复合模式之间的区别。请阅读文档,例如:

您正在犯一些新手错误,这些错误都可以通过阅读文档来解决。你的问题太多了 post。如果我的回答没有解决您的每一个问题,请提出新问题。我在你的 post 中看到至少还有两个问题(你的问题实际上应该以 "Too broad" 为由关闭)。

更新:

在您的评论中,您添加了以下代码片段:

table.AddCell(new Paragraph(data.EmpName, new Font(Font.FontFamily.COURIER, 32f, Font.BOLD)));

您想将此文本居中。

首先,让我解释一下您使用的是 AddCell() 方法,参数是 Paragraph。这实际上没有意义,因为 Paragraph 将被视为 Phrase。你也可以这样写:

table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER ;
table.AddCell(new Phrase(data.EmpName, new Font(Font.FontFamily.COURIER, 32f, Font.BOLD)));

当您将 Phrase 传递给 AddCell() 方法时,您是

  1. 使用文本模式(单元格的属性优先于其元素的属性),并且
  2. 您要求 iTextSharp 创建 PdfPCell

在这种情况下,iTextSharp 将查看 DefaultCell 并使用该单元格的属性创建一个新单元格。如果要使新单元格的内容居中,则需要在 DefaultCell 级别进行定义。所有这些都在我对以下问题的回答中得到了解释: