【问题标题】:Why these borders are showing when generating pdf using iTextsharp?为什么使用 iTextsharp 生成 pdf 时会显示这些边框?
【发布时间】:2016-01-23 12:23:05
【问题描述】:

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

  • 我在插入的图像下方看到了可见的单元格边框。
  • 底部图像占用空间,将图像轻拂到另一个页面,并带有额外的可见边框。
  • 此外,段落未对齐到中心。
  • 除此之外,我还需要文本(段落中)来自视图(此代码在 MVC 中执行)。

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

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

【问题讨论】:

  • 你使用的是什么版本的 iTextSharp?
  • 嗨,Amedee,我使用的是 5.5.8 版

标签: asp.net-mvc itextsharp


【解决方案1】:

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

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;

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

您犯了许多新手错误,这些错误都可以通过阅读文档来解决。你在一篇文章中有太多问题。如果我的回答不能解决您的每一个问题,请提出新问题。我在您的帖子中看到至少还有两个问题(您的问题实际上应该以“太宽泛”的理由结束)。

更新:

在您的评论中,您添加了以下代码 sn-p:

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 级别定义它。所有这些都在我对以下问题的回答中得到了解释:

【讨论】:

  • 你好 Bruno,我读到了 PDF 中的右对齐文本并进行了相应的更改,但我仍然没有将文本放入中心。我已将代码编辑为: iTextSharp.text.Image imageT=iTextSharp.text.Image.GetInstance(Server.MapPath(ImgTopPath)); PdfPCell imgTopCell = new PdfPCell(imageT); imgTopCell.Border = 矩形.NO_BORDER; imgTopCell.Border = 0; table.AddCell(imgTopCell); table.AddCell(new Paragraph(data.EmpName, new Font(Font.FontFamily.COURIER, 32f, Font.BOLD)));
  • @Chandan 我已经更新了我的答案。您不应该使用 cmets 发布代码。如您所见,注释字段中的代码很难阅读。
  • 你好 Bruno,我已经完成了 pdf 生成,它在本地服务器上运行良好,但是当我尝试在服务器上运行 smae 代码时(一种测试服务器,用于在部署前进行测试代码),它会弹出一个错误:**Mailer Exception:Could not find file 'F:\IIS_Sites\Stackatest.xyz.com\Documents\Certificates **,我尝试了一些建议但从未得到结果,所以你能又来这里帮忙了?
  • @Chandan 该问题与上述问题完全无关。因此我不能在这里回答。请发布一个新问题并以更好的方式解释该问题,因为在当前状态下,没有人知道您在说什么。我们对您的F: 驱动器、您的IIS 安装以及您所指的Certificates 目录一无所知。
  • ,感谢您的建议,因为我发布了一个新问题,请看一下。
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多