iText 7:如何构建混合不同字体的段落? [英] iText 7: How to build a paragraph mixing different fonts?

查看:1703
本文介绍了iText 7:如何构建混合不同字体的段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用iText 7来构建pdf文件,不幸的是,iText 7与iText 5非常不同,文档仍然非常不完整。

I've been using iText 7 for a few days to build pdf files, unfortunately, iText 7 is very different from iText 5 and the documentation is still very incomplete.

我正在尝试构建一个混合两种字体或两种字体样式的段落(例如:在段落中间有一个粗体文本)

I'm trying to build a paragraph that mixes two fonts or two fonts style (example: have a bold text in the middle of a paragraph)

使用iText 5这将使用Chunks完成:

Using iText 5 this would 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);

使用iText 7,我仍然没有办法做到这一点。

Using iText 7, I still haven't found way to do this.

是否有人尝试使用最新版本的iText?

Has anyone tried to do this using the last version of iText?

注意:我使用的是csharp,但java也很有用

Note: I'm using the csharp but java is also useful

谢谢,

LuísPinho

推荐答案

请阅读文档,更具体地说, iText 7:构建块第1章:PdfFont类介绍

Please read the documentation, more specifically iText 7: building blocks "Chapter 1: Introducing the PdfFont class"

在那一章中,你会发现它是使用iText 7时更容易切换字体,因为您可以使用默认字体和字体大小,您可以定义和重用 Style 对象,等等。

In that chapter, you'll discover that it's much easier to switch fonts when using iText 7, because you can work with default fonts and font sizes, you can define and reuse Style objects, and so on.

例如:

Style normal = new Style();
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);

首先我们定义一个 Style 我们称之为正常,使用14 pt Times-Roman。然后我们定义一个 Style ,我们称之为代码,它使用12 pt Courier in Red,灰色背景。

First we define a Style that we call normal and that uses 14 pt Times-Roman. Then we define a Style that we call code and that uses 12 pt Courier in Red with a gray background.

然后我们使用 Text 组成一个段落样式。

Then we compose a Paragraph using Text objects that use these styles.

请注意,您可以链接 add()注释,如下例所示:

Note that you can chain add() comments, as is done in this 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);

我们设置新创建的段落的字体大小到8磅。除非对象覆盖该默认大小,否则此字体大小将由添加到段落的所有对象继承。 title1 就是这种情况,我们为其定义了12 pt的字体大小和 title2 ,我们为此定义了字体大小为16磅。内容添加为字符串by),内容添加为文本未定义字体大小的对象从段落中继承字体大小8磅。

We set the font size of the newly created Paragraph to 8 pt. This font size will be inherited by all the objects that are added to the Paragraph, unless the objects override that default size. This is the case for title1 for which we defined a font size of 12 pt and for title2 for which we defined a font size of 16 pt. The content added as a String (" by ") and the content added as a Text object for which no font size was defined inherit the font size 8 pt from the Paragraph to which they are added.

这是官方教程的复制/粘贴。我希望这对于StackOverflow来说已经足够了,其中不允许使用仅链接答案。这个没有链接的答案规则不应该导致复制/粘贴手册的完整章节......

This is a copy/paste from the official tutorial. I hope this is sufficient for StackOverflow where "link-only" answers aren't allowed. This "no link-only answers rule" shouldn't lead to copy/pasting a full chapter of a manual...

这篇关于iText 7:如何构建混合不同字体的段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆