【问题标题】:iTextSharp - Is it possible to set a different font color for the same cell and row?iTextSharp - 是否可以为相同的单元格和行设置不同的字体颜色?
【发布时间】:2012-06-18 05:33:56
【问题描述】:

我正在使用带有以下代码的 iTextSharp.dll:

var Title = "This is title";
var Description = "This is description";

Innertable.AddCell(new PdfPCell(new Phrase(string.Format("{0} {1}", Title, Description.Trim()), listTextFont)) { BackgroundColor = new BaseColor(233, 244, 249), BorderWidth = 0, PaddingTop = 4, PaddingLeft = -240, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_LEFT });

我们可以为标题和描述设置不同的字体颜色,但只使用单个单元格(即不创建新表格)吗?

在这件事上的任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:

    您要做的是创建 2 个Chunk 对象,然后将它们组合成 1 个Phrase,您将添加到单元格中。

    var blackListTextFont = FontFactory.GetFont("Arial", 28, Color.BLACK);
    var redListTextFont = FontFactory.GetFont("Arial", 28, Color.RED);
    
    var titleChunk = new Chunk("Title", blackListTextFont);
    var descriptionChunk = new Chunk("Description", redListTextFont);
    
    var phrase = new Phrase(titleChunk);
    phrase.Add(descriptionChunk);
    
    table.AddCell(new PdfPCell(phrase));
    

    看看http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

    【讨论】:

    • 感谢短语的想法。这个答案有效。
    • 完美解决方案。谢谢。
    【解决方案2】:

    尝试这样在 pdf 单元格中设置不同的前景色:

    var FontColour = new BaseColor(35, 31, 32);
    var Calibri8 = FontFactory.GetFont("Calibri", 8, FontColour);
    
    PdfPCell R3C2 = new PdfPCell(new Paragraph("Hello", Calibri8));
    R3C2.BorderWidth = 0f;
    R3C2.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
    table.AddCell(R3C2);
    

    【讨论】:

      【解决方案3】:

      对于 VB.net 使用如下函数

       Public Function CreateFont(size As Integer, Optional style As Integer = iTextSharp.text.Font.BOLD) As iTextSharp.text.Font
              Dim FontColour = New BaseColor(193, 36, 67)  'Color code
              Return New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, size, style, FontColour)
       End Function
      
      
       Dim p As New Paragraph
       p.Add(New Paragraph("Your Sentence Here", CreateFont(12, iTextSharp.text.Font.BOLDITALIC)))
       pdfDoc.Add(p)
      

      【讨论】:

        【解决方案4】:

        诀窍是创建具有不同字体的短语(不是块)并将它们添加到父短语中。据我所知,如果您创建具有不同字体的块并将它们添加到短语中,则最终短语中的所有文本都以相同的字体显示。

        这是一个适合我的例子:

        // create the font we'll use
        var fNormal = FontFactory.GetFont("Helvetica", 10f);
        fNormal.SetColor(0, 0, 0);
        
        // add phrase containing link
        var pFooter = new Phrase();
        
        // add phrase to this containing text only
        var footerStart = new Phrase("Visit ");
        footerStart.Font = fNormal;
        pFooter.Add(footerStart); 
        
        // now create anchor and add with different font
        string wolSiteUrl = "http://www.whateveryoulike.com";
        Anchor wolWebSiteLink = new Anchor(wolSiteUrl, fNormal);
        wolWebSiteLink.Reference = wolSiteUrl;
        wolWebSiteLink.Font = fNormal;
        wolWebSiteLink.Font.SetColor(242, 132, 0);
        pFooter.Add(wolWebSiteLink);
        
        // add text to go after this link
        var footerEnd = new Phrase(" to view these results online.");
        footerEnd.Font = fNormal;
        pFooter.Add(footerEnd);
        var paraFooter = new Paragraph(pFooter);
        
        // add the phrase we've built up containing lots of little phrases to document
        // (assume you have one of these ...)
        doc.Add(paraFooter);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-12
          • 2015-07-18
          • 1970-01-01
          • 2015-02-22
          • 1970-01-01
          • 2015-03-12
          • 2013-01-10
          • 2012-05-24
          相关资源
          最近更新 更多