I used iText 7 for several days to create pdf files and I can say that, unfortunately, it is very different from iText 5 and the documentation is still not sufficiently complete. I'm trying to create a paragraph that uses two fonts or two styles (example: bold text in the middle of a paragraph)

When using iText 5, this can be done using Chunks:

Font regular = new Font(FontFamily.HELVETICA, 12); Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD); Phrase p = new Phrase("NAME: ", bold); p.add(new Chunk(cc_cust_dob, regular)); PdfPCell cell = new PdfPCell(p); 

In iText 7, I never found a way to do this. Did anyone manage to do this in the latest version of iText? Note: I am using C #, but Java will also be useful.

    1 answer 1

    Please read the documentation, pay special attention to the iText7 section : building blocks "Chapter 1: Introduction to the PdfFont class ". From this chapter, you will learn that using iText7 makes it much easier to switch fonts, because you can work with standard fonts and their sizes, you can define and reuse Style objects, etc.

    Example:

     PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); normal.setFont(font).setFontSize(14); Style code = new Style(); PdfFont monospace = PdfFontFactory.createFont(FontConstants.COURIER); code.setFont(monospace).setFontColor(Color.RED) .setBackgroundColor(Color.LIGHT_GRAY); Paragraph p = new Paragraph(); p.add(new Text("The Strange Case of ").addStyle(normal)); p.add(new Text("Dr. Jekyll").addStyle(code)); p.add(new Text(" and ").addStyle(normal)); p.add(new Text("Mr. Hyde").addStyle(code)); p.add(new Text(".").addStyle(normal)); document.add(p); 

    for starters, we will define the Style , call it normal and specify a 14pt font Times-Roman for it. After that, we will define a Style , which we will call code and assign it a red font Courier, 12pt in size with a gray background. After that we will create a Paragraph using Text objects that use these styles.

    Note that you can hook add() comments, as in the following example:

     Text title1 = new Text("The Strange Case of ").setFontSize(12); Text title2 = new Text("Dr. Jekyll and Mr. Hyde").setFontSize(16); Text author = new Text("Robert Louis Stevenson"); Paragraph p = new Paragraph().setFontSize(8) .add(title1).add(title2).add(" by ").add(author); document.add(p); 

    “Set the font size for the newly created Paragraph to 8pt. This font size will be inherited by all objects added to the Paragraph , unless objects override this default size. This refers to title1 , for which we defined the font size at 12pt and for title2 , for which we defined the font size at 16pt. Content added as a String ( " by " ), and content added as a Text object for which no font size has been defined, inherits the size of the 8pt font from the Paragraph to which they are added. ”

    This is an excerpt from the official tutorial, and I hope this will be enough for StackOverflow, where responses in the form of links alone are not welcome. However, I believe that this rule should not lead to a copy-paste of the whole chapter of the manual.

    • And the answer is good and the question is not bad, but it makes no sense to create different accounts for the question and answer. Also write whether it is your library or you are somehow involved - you have the impression that you registered on the Japanese and Russian sites only for the purpose of advertising your own library. The site even has a corresponding type of spam alarm "Advertise a product or service, without disclosing the author's involvement." - AK
    • This is just brilliant (I'm talking about advertising) - TEA