最近开发一个功能:公司要求把pdf文件中的对应关键字文本动态替换为对应的内容,于是就从网络上查找各种相关的资料,最终决定用Spire.Pdf.General.Find.PdfTextFind来解决问题,在代码编写时候发现该类下的一个方法ApplyRecoverString是支持文本替换功能的,但是中文内容替换会出现乱码,研究了一下发现加入一个参数ApplyRecoverString(string newvalue, bool unicode);使用unicode为true可以解决这个问题,可是问题来了,打开程序运行,第一次运行可以正常操作替换,第二次这个参数就会报错,试了很多种方法没有办法解决,最终决定放弃该参数的使用,采用另一种方案解决。就是把对应文本找到,内容替换为空的,再在对应的文本位置采用写入pdf文字的方法,才得以解决。
下面贴上相关代码:
public string pdfreplace(string templatePath,string signhtidsign)
{
Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(templatePath);
System.Drawing.Font font = new System.Drawing.Font(“宋体”, 10, System.Drawing.FontStyle.Regular);
Spire.Pdf.Graphics.PdfTrueTypeFont pdfTrueTypeFont = new Spire.Pdf.Graphics.PdfTrueTypeFont(font, true);//true防止中文乱码
string sql = “declare @HTId int set @HTId=” + signhtidsign +
@" declare @HTBegTrial varchar(10),@HTEndTrial varchar(10)
select @HTBegTrial=HTBegTrial,@HTEndTrial=HTEndTrial from MenSignHT where id=@HTId
if(len(@HTBegTrial)=10 and len(@HTEndTrial)=10)
select BH,XM,XL,SFZHM,TXDZ,JJLXR,JJLXRDH,
KHMC,GW,GSBZ,SYQGZ,HTGZ,GZFFR,
N1 = substring(HTBeg, 1, 4),Y1 = SUBSTRING(HTBeg, 6, 2),D1 = SUBSTRING(HTBeg, 9, 2),
N2 = substring(HTEnd, 1, 4),Y2 = SUBSTRING(HTEnd, 6, 2),D2 = SUBSTRING(HTEnd, 9, 2),
N3 = substring(HTBegTrial, 1, 4),Y3 = SUBSTRING(HTBegTrial, 6, 2),D3 = SUBSTRING(HTBegTrial, 9, 2),
N4 = substring(HTEndTrial, 1, 4),Y4 = SUBSTRING(HTEndTrial, 6, 2),D4 = SUBSTRING(HTEndTrial, 9, 2),
N5 = substring(QDRQ, 1, 4),Y5 = SUBSTRING(QDRQ, 6, 2),D5 = SUBSTRING(QDRQ, 9, 2),
QTYD=case when isnull(QTYD,’’)=’’ then ‘/’ else QTYD end,JJLXRGX
from MenSignHT where id= @HTId
else select BH, XM, XL, SFZHM, TXDZ, JJLXR, JJLXRDH,
KHMC, GW, GSBZ, SYQGZ=’/’, HTGZ, GZFFR,
N1 = substring(HTBeg, 1, 4), Y1 = SUBSTRING(HTBeg, 6, 2), D1 = SUBSTRING(HTBeg, 9, 2),
N2 = substring(HTEnd, 1, 4), Y2 = SUBSTRING(HTEnd, 6, 2), D2 = SUBSTRING(HTEnd, 9, 2),
N3 = ‘/’, Y3 = ‘/’, D3 = ‘/’,
N4 = ‘/’, Y4 = ‘/’, D4 = ‘/’,
N5 = substring(QDRQ, 1, 4), Y5 = SUBSTRING(QDRQ, 6, 2), D5 = SUBSTRING(QDRQ, 9, 2),
QTYD=case when isnull(QTYD,’’)=’’ then ‘/’ else QTYD end,JJLXRGX
from MenSignHT where id=@HTId";
DataTable dt = DLLDBUtility.DbHelperSQL.Query(sql, “电子签名合同替换”);
DataRow dr = dt.Rows[0];
Spire.Pdf.General.Find.PdfTextFind[] AllMatchedText = null;
//遍历页面
foreach (Spire.Pdf.PdfPageBase page in doc.Pages)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
//根据关键词查找页面中匹配的文本
//AllMatchedText = page.FindText("").Finds;
AllMatchedText = page.FindText("<"+ dt.Columns[i].ColumnName+">").Finds;
foreach (Spire.Pdf.General.Find.PdfTextFind text in AllMatchedText)
{
try
{
text.ApplyRecoverString("");
page.Canvas.DrawString(dr[dt.Columns[i].ColumnName].ToString(), pdfTrueTypeFont, new Spire.Pdf.Graphics.PdfSolidBrush(System.Drawing.Color.Black), text.Bounds.X, text.Bounds.Y);
}
catch
{
}
}
}
}
if (File.Exists(DLLDBUtility.LoginInfo.ApplicationPath + "syslog\\test.pdf"))
{
File.Delete(DLLDBUtility.LoginInfo.ApplicationPath + "syslog\\test.pdf");
}
doc.SaveToFile(DLLDBUtility.LoginInfo.ApplicationPath + "syslog\\test.pdf");
doc.Dispose();
return DLLDBUtility.LoginInfo.ApplicationPath + "syslog\\test.pdf";
}