PDF text replace not working

心已入冬 提交于 2019-12-25 04:59:10

问题


I'm trying to replace text in PDF using iTextSharp dll but its not working in all cases. PDF document doesn't have acro fields.

If the text which I need to replace is bigger than original text its not printing all characters. Finding some special characters is also not working.

I have tried this code

using (PdfReader reader = new PdfReader(sourceFileName))
{
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        byte[] contentBytes = reader.GetPageContent(i);
        string contentString = PdfEncodings.ConvertToString(contentBytes, PdfObject.TEXT_PDFDOCENCODING);
        contentString = contentString.Replace("SOMETEXT", "NEWBIGGERTEXT");                 
        reader.SetPageContent(i, PdfEncodings.ConvertToBytes(contentString, PdfObject.TEXT_PDFDOCENCODING));
    }
    new PdfStamper(reader, new FileStream(newFileName, FileMode.Create, FileAccess.Write)).Close();
}

Please let me know how this can be achieved.

来源:https://stackoverflow.com/questions/41726826/pdf-text-replace-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!