名称';颜色';在使用iTextSharp的当前上下文中不存在

名称';颜色';在使用iTextSharp的当前上下文中不存在,itext,Itext,我正在使用itextsharp dll创建pdf 我想改变我的字体颜色 我在谷歌找到了解决方案 Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f); Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK); 但是我试着修改我的源代码。显示错误“颜色”名称在当前上下文中不存在。颜色类不喜欢 如何解决这个错误 多谢各位 下

我正在使用itextsharp dll创建pdf

我想改变我的字体颜色

我在谷歌找到了解决方案

Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
但是我试着修改我的源代码。显示错误“颜色”名称在当前上下文中不存在。颜色类不喜欢

如何解决这个错误

多谢各位

下面是我的代码

using iTextSharp.text;
using iTextSharp.text.pdf;


    private void sPDF(DataRow row)
    {
        Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
        Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);

        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
            Phrase phrase = null;
            PdfPCell cell = null;
            PdfPTable table = null;
            Color color = null;

            document.Open();

            //Separater Line
            color = new Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
            DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color);
            DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color);
            document.Add(table);

            document.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

        }
    }

    private static void DrawLine(PdfWriter writer, float x1, float y1, float x2, float y2, Color color)
    {
        PdfContentByte contentByte = writer.DirectContent;
        contentByte.SetColorStroke(color);
        contentByte.MoveTo(x1, y1);
        contentByte.LineTo(x2, y2);
        contentByte.Stroke();
    }

正如我之前多次解释的那样,我们不再谈论iTextSharp。该名称在几年前被更改为.NET的iText。查看您的代码,我发现您使用的是旧版本的iText(可能是版本5)。今天,我们的版本是7。请参阅和

你的问题有两个答案:

  • 请升级到iText 7,并使用iText 7颜色类别:

  • 如果坚持使用旧版本的iText(注意:不再支持这些版本),请将
    Color
    替换为
    BaseColor

  • 我不知道您在哪里找到了有关使用
    Color
    的文档,但这些信息一定很旧,因为我们在2009年将
    Color
    更改为
    BaseColor


    为避免进一步的问题,请使用最新版本,并始终咨询。

    我看不出您在哪里尝试使用您尝试使用的字体。如果将其更改为
    Font-normalFont=新字体(“Arial”,12,Font.NORMAL,Color.BLACK)我认为它可能会停止错误,但请尝试完全删除该行。或者举一个例子,说明您在哪里尝试使用它,以便我们能够提供帮助more@Red我不明白你的评论。请阅读我的答案,看看到底发生了什么。