0

We are trying to create a overlay pdf using itext and its working.

We are also trying to change the entire text font color of our pdf.

We are trying to do it using itext and c#.net

The font color is not changing

I also attached the pdf file.

Below is the code snippet.

string outputFile = @"./Overlay.pdf";

iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sourcefilePath);
iTextSharp.text.pdf.PdfReader sReader = new iTextSharp.text.pdf.PdfReader(overlayfilePath);
PdfStamper stamper = new PdfStamper(reader, new FileStream(outputFile, FileMode.Create));
int inputDocumentPages = reader.NumberOfPages;
int overlayDocumentPages = sReader.NumberOfPages;
PdfContentByte background;
for (int i = 1; i <= inputDocumentPages; i++)
{
    if (i <= overlayDocumentPages)
    {
        PdfImportedPage page = stamper.GetImportedPage(sReader, i);
        background = stamper.GetUnderContent(i);
        background.AddTemplate(page, 0, 0);
        PdfGState state = new PdfGState();
        state.FillOpacity = 0.6f;
        state.BlendMode = PdfGState.BM_MULTIPLY;
                   
        background.SetGState(state);
        background.SaveState();
        background.BeginText();
        background.SetColorFill(BaseColor.RED);
        background.EndText();
        background.Fill();
    }
}
stamper.Close();
  • Your approach cannot work because `SetColorFill` sets the color for the following filling operations (e.g. text drawing in default mode) until the color is changed by another operation. As the only thing that follows is `EndText` and an orphan `Fill`, your color affects nothing. What you want to do most likely requires something like the code in [this answer](https://stackoverflow.com/a/40709845/1729265), merely without the `BaseColor.BLACK.equals(currentFillColor)` check. – mkl Jul 12 '21 at 14:38

0 Answers0