在ITextSharp中抛出错误的特殊字符

时间:2017-09-12 11:33:12

标签: c# itext pdfptable

ITextSharp在我的应用程序中用于以pdf格式查看数据库中的数据。

 Document document = new Document(PageSize.A4);
 MemoryStream stream = new MemoryStream();

 PdfWriter writer = PdfWriter.GetInstance(document, stream);
 document.Open();

 PdfPTable detail = GetTable(4, 500f, 0);
 foreach (var Stage in stageDetails)
   {
      detail.AddCell(GetCellDetails(Stage.Key,string.Empty, 4, 0, 0));                            
      detail.AddCell(GetCellDetails(string.Empty,Stage.Value, 4, 0, 0, 0f, 1, 0, 8, 0, 1));
   }                    
 document.Add(detail);

GetCellDetails是另一种生成css样式的方法

 public static PdfPCell GetCellDetails(string Topic, string value, int Colspan, int HorizontalAlignment, int main = 1, float Length = 0f, int IsHeight = 0, int IsBorder = 0, int PaddingBottom = 8, int PaddingTop = 0, int IsHTML = 0)
    {
        var phrase = new Phrase();
        var titleFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
        var SubtitleFont = FontFactory.GetFont("Arial", 10, Font.BOLD);
        var SubAnsFont = FontFactory.GetFont("Arial", 8, Font.NORMAL);
        if (main == 1)
        {
            phrase.Add(new Chunk(Topic, titleFont));
        }
        else
        {
            if (Topic != string.Empty && value != string.Empty)
            {
                phrase.Add(new Chunk(Topic, SubtitleFont));
                phrase.Add(new Chunk(value, SubAnsFont));
            }
            else if (Topic != string.Empty)
            {
                phrase.Add(new Chunk(Topic, SubtitleFont));
            }
            else if (value != string.Empty)
            {
                phrase.Add(new Chunk(value, SubAnsFont));
            }
        }
        Paragraph Paragraph = new Paragraph(phrase);
        if (main == 2 || main == 3 || main == 4)
        {
            if (Length >= 100f)
            {
                LineSeparator LineSeparator = new LineSeparator((main == 4) ? 3.5f : (main == 2) ? 1.5f : 0.2f, Length, BaseColor.BLACK, Element.ALIGN_LEFT, -1);
                Paragraph.Add(LineSeparator);
            }
            else
            {
                LineSeparator LineSeparator = new LineSeparator(1f, Length, BaseColor.BLACK, Element.ALIGN_LEFT, -1);
                Paragraph.Add(LineSeparator);
            }
        }
        PdfPCell Cell = new PdfPCell(Paragraph);
        if (IsHTML == 1)
        {
            Cell = new PdfPCell();
            StyleSheet css = new StyleSheet();
            css.LoadTagStyle(HtmlTags.DIV, HtmlTags.FONTSIZE, "8");
            css.LoadStyle("tHeader", "size", "15ptx");
            css.LoadStyle("rows", "color", "red");
            css.LoadStyle("left", "width", "auto");
            css.LoadStyle("left", "float", "left");

            css.LoadStyle("right", "width", "auto");
            css.LoadStyle("right", "float", "right");
            css.LoadStyle("right", "border", "solid 1px blue");
            css.LoadStyle("lbl", "font-weight", "700");
            css.LoadStyle("content", "size", "15ptx");
            string[] spilt = { "<hr/>", "<hr />" };
            foreach (string ab in spilt)
            {
                value = value.Replace(ab, "<table border='1' width=auto cellpadding='0' cellspacing='0'><tr><td>&nbsp;</td></tr></table>");
            }
            var objects = HTMLWorker.ParseToList(new StringReader(value), css);
            for (int k = 0; k < objects.Count; ++k)
            {
                Cell.AddElement((IElement)objects[k]);
            }
        }
        if (IsHeight == 1)
        {
            Cell.MinimumHeight = 25f;
        }
        Cell.BorderWidthBottom = (IsBorder == 4) || (IsBorder == 6) || (IsBorder == 5) || (IsBorder == 1) || (IsBorder == 8) || (IsBorder == 9) || (IsBorder == 7) ? 1 : 0;
        Cell.BorderWidthRight = (IsBorder == 3) || (IsBorder == 6) || (IsBorder == 1) || (IsBorder == 9) ? 1 : 0;
        Cell.BorderWidthLeft = (IsBorder == 2) || (IsBorder == 4) || (IsBorder == 1) || (IsBorder == 8) ? 1 : 0;
        Cell.BorderWidthTop = (IsBorder == 1) ? 1 : 0;
        Cell.Colspan = Colspan;
        if (PaddingTop == 1)
        {
            Cell.PaddingTop = 8;
        }
        Cell.PaddingBottom = PaddingBottom;
        Cell.HorizontalAlignment = HorizontalAlignment;
        return Cell;
    }

问题在于特殊字符,它显示了一些错误,例如表格宽度必须大于零。如何使用这段代码进行编码和解码?这个问题的解决方案是什么?

0 个答案:

没有答案