C# iTextSharp multi fonts in a single cell

后端 未结 1 1116
萌比男神i
萌比男神i 2020-12-06 19:13

First off I\'m not that great with C# and it\'s been a while since I\'ve worked with it..

I\'m making a windows form for a friend that delivers packages. So I want t

相关标签:
1条回答
  • 2020-12-06 19:49

    You're passing a String and a Font to the AddCell() method. That's not going to work. You need the AddCell() method that takes a Phrase object or a PdfPCell object as parameter.

    A Phrase is an object that consists of different Chunks, and the different Chunks can have different font sizes. Please read chapter 2 of my book for more info about this object.

    Phrase phrase = new Phrase();
    phrase.Add(
        new Chunk("Some BOLD text",  new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
    );
    phrase.Add(new Chunk(", some normal text", new Font()));
    table.AddCell(phrase);
    

    A PdfPCell is an object to which you can add different objects, such as Phrases, Paragraphs, Images,...

    PdfPCell cell = new PdfPCell();
    cell.AddElement(new Paragraph("Hello"));
    cell.AddElement(list);
    cell.AddElement(image);
    

    In this snippet list is of type List and image is of type Image.

    The first snippet uses text mode; the second snippet uses composite mode. Cells behave very differently depending on the mode you use.

    This is all explained in the documentation; you can find hundreds of C# examples here.

    0 讨论(0)
提交回复
热议问题