【问题标题】:change iTextSharp Font Color in c#在 c# 中更改 iTextSharp 字体颜色
【发布时间】:2018-03-25 19:09:12
【问题描述】:

我想改变字体颜色。我使用SetRGBColorStrokeSetColorFillSetColorStroke,但这些都不起作用。我的代码如下。

var document = new Document(PageSize.A4);
PdfReader reader2 = new PdfReader(outputPdfPath);
var stream = File.OpenWrite(path);
var writer = PdfWriter.GetInstance(document, stream);
writer.PdfVersion = PdfWriter.VERSION_1_5;
float scale = 1f;
int xs = 35; int ys = 726;
int xtemp = 0; int ytemp = 0;
document.Open();
PdfContentByte pb = writer.DirectContent;
PdfContentByte pbtext = writer.DirectContent;

for (int j = 1; j <= reader2.NumberOfPages; j++)
{
    document.NewPage();
    //PdfContentByte pbtext;
    BaseFont bf = BaseFont.CreateFont(@"C:\Windows\Fonts\arial.TTF", BaseFont.WINANSI, true);
    ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();//simple read with accessible
    PdfImportedPage page = writer.GetImportedPage(reader2, j);
    pb.AddTemplate(page, scale, 0, 0, scale, xtemp, ytemp);


    if (j == 1)
    {
        Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(04.04F, 14.04F, BaseColor.BLACK, 100, -46)));
        document.Add(p);

        pbtext = writer.DirectContent;
        pbtext.BeginText();
        pbtext.SetFontAndSize(bf, 4);
        pbtext.SetRGBColorStroke(255, 0, 0);
        pbtext.ShowTextAligned(PdfContentByte.ALIGN_LEFT, namafile.Substring(0, 12), 292.0f, 740f, 0);
       // pbtext.SetColorStroke(BaseColor.RED);
       // pbtext.setColorFill(new BaseColor(255, 0, 0, 50));
       // pbtext.setColorStroke(BaseColor.RED);
        pbtext.EndText();


    }
}

//reader2.Close();

document.Close();

【问题讨论】:

  • 您的意思是要更改段落内的字体颜色?
  • 在此处更改文本 pbtext.ShowTextAligned
  • 那么会发生什么?你期望什么,你实际得到什么?什么不起作用?
  • 通常字体提供被填充的轮廓。因此,您需要填充颜色。你声称你也试过了。结果到底是什么?
  • 用这个代码解决了:pbtext.setColorFill(BaseColor.RED); pbtext.ShowTextAligned(PdfContentByte.ALIGN_LEFT, namafile.Substring(0, 12), 292.0f, 740f, 0);我的错误将 setfillcolor 放在底部。

标签: c# pdf itext


【解决方案1】:

用这段代码解决了:

pbtext.setColorFill(BaseColor.RED);
pbtext.ShowTextAligned(PdfContentByte.ALIGN_LEFT, namafile.Substring(0, 12), 292.0f, 740f, 0); 

我的错误将 setfillcolor 放在底部

【讨论】:

    猜你喜欢
    • 2017-07-04
    • 2015-02-22
    • 2013-11-14
    • 2014-01-19
    • 2015-01-02
    • 2014-05-03
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多