Pregunta

Necesito ocultar un texto agregando una nueva capa sobre el texto que necesito ocultar.

public void ReplacePDFText(string strSearch, StringComparison scCase, string strSource, string strDest)
{
    PdfContentByte pCont = null;

    if (File.Exists(strSource)) {
        PdfReader pdfFileReader = new PdfReader(strSource);
        using (PdfStamper psStamp = new PdfStamper(pdfFileReader, new FileStream(strDest, FileMode.Create))) {
            for (int intCurrPage = 1; intCurrPage <= pdfFileReader.NumberOfPages; intCurrPage++) {
                LocTextExtractionStrategy Strategy = new LocTextExtractionStrategy();
                pCont = psStamp.GetUnderContent(intCurrPage);
                Strategy.UndercontentCharacterSpacing = pCont.CharacterSpacing;
                Strategy.UndercontentHorizontalScaling = pCont.HorizontalScaling;

                string currText = PdfTextExtractor.GetTextFromPage(pdfFileReader, intCurrPage, Strategy);
                List<iTextSharp.text.Rectangle> lstMatches = Strategy.GetTextLocations(strSearch, scCase);

                PdfLayer pdLayer = default(PdfLayer);
                pdLayer = new PdfLayer("over", psStamp.Writer);
                pCont.SetColorFill(BaseColor.BLACK);
                foreach (Rectangle rctRect in lstMatches) {
                    pCont.Rectangle(rctRect.Left, rctRect.Bottom, rctRect.Width, rctRect.Height);
                    pCont.Fill();
                }
            }
        }
        pdfFileReader.Close();
    }
}

El problema con el enfoque anterior es que la capa se agrega con éxito con el color negro. Entonces, en lugar del texto, tengo una hermosa línea negra sobre el texto. Pero si configuro el pCont.SetColorFill(BaseColor.BLACK) Para blanco, el texto todavía se muestra. ¿Cómo puedo superar este problema?

¿Fue útil?

Solución

En vez de:

pCont = psStamp.GetUnderContent(intCurrPage);

Usar:

pCont = psStamp.GetOverContent(intCurrPage);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top