简体   繁体   中英

ItextSharp: Add color to text for selected font-name in existing PDF

Using iTextSharp in c# , I'm trying to add color to some text for the selected fonts in the PDF, I looked in itext documentation but couldn't find anything (or don't know where to find it).

Alternatively, I was able to add highlights to text using the pdfstamper .

PdfAnnotation highlight = PdfAnnotation.CreateMarkup
(
   pdfStamper.Writer,
   TextBlock.Rect,
   null,
   PdfAnnotation.MARKUP_HIGHLIGHT,
   Quad
);

Is there any way to add color to text, I tried the below code to add text but don't know how to reuse the existing font from the input PDF using BaseFont.CreateFont()

contentByte.BeginText();
contentByte.SetFontAndSize(BaseFont.CreateFont(), TextBlock.FontSize);
contentByte.SetTextMatrix(Rect.Left, Rect.Bottom);
contentByte.ShowText(TextBlock.Text);
contentByte.EndText();

According to your code sample, you use low-level API text showing methods. In that case you can use the matching low-level color setting methods, eg:

contentByte.BeginText();
contentByte.SetRGBColorFillF(1f, 0f, 0f);
contentByte.SetFontAndSize(BaseFont.CreateFont(), TextBlock.FontSize);
contentByte.SetTextMatrix(Rect.Left, Rect.Bottom);
contentByte.ShowText(TextBlock.Text);
contentByte.EndText();

Browse your object catalog for more Set*ColorFill* methods, depending on your preferred color space there may be better suited variants.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM