Скрытие текста PDF, добавив дополнительный слой

StackOverflow https://stackoverflow.com/questions/20353785

  •  25-08-2022
  •  | 
  •  

Вопрос

Мне нужно скрыть текст, добавив новый слой по тексту, который мне нужно скрыть.

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();
    }
}

Проблема с приведенным выше подходом заключается в том, что слой успешно добавлен с черным цветом. Так что вместо текста у меня есть красивая черная линия над текстом. Но если я установите pCont.SetColorFill(BaseColor.BLACK) Для белого текст все еще отображается. Как я могу преодолеть эту проблему?

Это было полезно?

Решение

Вместо:

pCont = psStamp.GetUnderContent(intCurrPage);

Использовать:

pCont = psStamp.GetOverContent(intCurrPage);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top