How to put a background color to a text in a PdfContentByte of ItextSharp in C #?

0

Good morning.

I am creating a PDF with ItextSharp 5.5.10 and in it I have to include texts with a white background in certain positions. The PDF page contains a background image. For this I am using PdfContentByte but I can not get my white background painted.

PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetTextMatrix(cuadroTexto.PosX, defaultHeight - (cuadroTexto.PosY + cuadroTexto.Alto));
Font font = FontFactory.GetFont(FontFactory.HELVETICA, 8.25f, BaseColor.BLACK);
cb.SetFontAndSize(font.BaseFont, 8.25f);
cb.ShowText(cuadroTexto.Texto);
cb.SetColorFill(BaseColor.WHITE); // Aqui intento ponerle el blanco pero lo ignora
cb.EndText();

Is there any way to do it with PdfContentByte or do I have to resort to painting a white regtanger by hand?

PS: In case it works I'm using Visual Studio 2013 Express

UPDATE 1

I have tried with cb.Fill(); as recommended below and still not colored.

    
asked by MrCode 11.08.2017 в 00:11
source

1 answer

0

It seems that I have already found a solution.

The problem came because I was trying to do 2 things at the same time (insert the text and put the box on the white background). Separandolo in 2 actions with states works perfectly.

PdfContentByte cb = writer.DirectContent;
cb.SaveState();
cb.SetColorFill(BaseColor.WHITE);
cb.Rectangle(cuadroTexto.PosX, defaultHeight - (cuadroTexto.PosY + cuadroTexto.Alto), cuadroTexto.Ancho, cuadroTexto.Alto);
cb.Fill();
cb.RestoreState();

cb.BeginText();
cb.SetTextMatrix(cuadroTexto.PosX, defaultHeight - (cuadroTexto.PosY + 9));
Font font = FontFactory.GetFont(FontFactory.HELVETICA, 8.25f, BaseColor.BLACK);
cb.SetFontAndSize(font.BaseFont, 8.25f);
cb.ShowText(cuadroTexto.Texto);
cb.EndText();
    
answered by 16.08.2017 / 14:47
source