Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有一种方法可以使用iText7使第一个字符变成不同的颜色?_C#_Pdf_Itext_Pdf Generation_Itext7 - Fatal编程技术网

C# 有没有一种方法可以使用iText7使第一个字符变成不同的颜色?

C# 有没有一种方法可以使用iText7使第一个字符变成不同的颜色?,c#,pdf,itext,pdf-generation,itext7,C#,Pdf,Itext,Pdf Generation,Itext7,我将itext7与C#一起使用,我只想使用一个段落,第一个字符需要不同的颜色。有什么办法吗?在iText5中有一个关于块的东西,但在iText7中没有,所以我现在陷入了困境。没有块,但您可以向段落中添加具有不同特征的字符串 PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST)); Document doc = new Document(pdfDoc); Paragraph p = new Paragraph()

我将itext7与C#一起使用,我只想使用一个段落,第一个字符需要不同的颜色。有什么办法吗?在iText5中有一个关于块的东西,但在iText7中没有,所以我现在陷入了困境。

没有块,但您可以向段落中添加具有不同特征的字符串

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
    Document doc = new Document(pdfDoc);

    Paragraph p = new Paragraph();
    string original = "Some text";

    string first = original.Substring(0, 1);

    p.Add(new Text(first)
            .SetFontColor(ColorConstants.BLUE));
    p.Add(original.Substring(1, original.Length - 1));
    doc.Add(p);
    doc.Close();
这将创建一个PDF文件,其中“ome文本”为黑色,“S”为蓝色


相关文档()和。

的API您可以将文本添加到段落,并在将其添加到段落之前设置颜色

var dotstring = new Text("$");
var cashstring = new Text("1.000,-");
dotstring.SetFontColor(DeviceCmyk.MAGENTA);

var NummerParagraph = new Paragraph();
NummerParagraph.Add(dotstring);
NummerParagraph.Add(cashstring);
这是一个简单的解决方案,因为我的第一个字母是静态的,我知道它是什么。
如果你不知道字符串的第一个字母,安德烈·莱莫斯发布的另一个解决方案会更好。

谢谢你的回复,这也是一个很好的解决方法!我正要发布一个解决方案,当时我的键盘差点坏了。