使用itextsharp 4.1版本,我想找到一些文本并替换pdf文件中的文本,但pdf的设计必须保持不变。
protected void Button_Click(object sender, EventArgs e)
{
string filename =@"D:\DocumentTemplates\SanctionLetter_English.pdf";
string filename2 = @"D:\DocumentTemplates\changed4.pdf";
ExtractTextFromPdf(filename.ToString());
InsertTextToPdf(filename, filename2);
}
public static string ExtractTextFromPdf(string path)
{
using (Stream pdfStream = new FileStream(path, FileMode.Open))
{
PdfReader reader = new PdfReader(pdfStream);
// var reader = new PdfReader(path);
StringBuilder sb = new StringBuilder();
try
{
for (int page = 1; page <= reader.NumberOfPages; page++)
{
var cpage = reader.GetPageN(page);
var content = cpage.Get(PdfName.CONTENTS);
var ir = (PRIndirectReference)content;
var value = reader.GetPdfObject(ir.Number);
if (value.IsStream())
{
PRStream stream = (PRStream)value;
var streamBytes = PdfReader.GetStreamBytes(stream);
var tokenizer = new PRTokeniser(new RandomAccessFileOrArray(streamBytes));
try
{
while (tokenizer.NextToken())
{
if (tokenizer.TokenType == PRTokeniser.TK_STRING)
{
string str = tokenizer.StringValue;
sb.Append(str);
}
}
}
finally
{
tokenizer.Close();
}
}
}
}
finally
{
reader.Close();
}
sb.Replace("LOAN PURPOSE", "Mumbai");
return sb.ToString();
}
}
这是我试图替换文本但在pdf文件中它不替换
答案 0 :(得分:1)
你不能用iTextSharp做到这一点 但是使用Aspose.Pdf你可以,在nugget中有一个免费版本