由于许可证问题,我正在尝试将旧程序从使用iTextSharp迁移到PdfSharp。
该程序有时会从某些带有表单字段的pdfTemplates中打印pdf。
旧代码看起来像这样。
string path = Assembly.GetExecutingAssembly().Location;
path = path.Substring(0,path.LastIndexOf("\\"));
BaseFont arialBaseFont = BaseFont.CreateFont(path + @"\Assets\myFont.ttf",BaseFont.IDENTITY_H,false);
var templateReader = new iTextSharp.text.pdf.PdfReader(templatePath);
stamper = new PdfStamper(templateReader,new FileStream(newFile,FileMode.Create));
AcroFields pdfFormFields = stamper.AcroFields;
bool bSuccess = false;
foreach (string strKey in fieldValues.Keys)
{
pdfFormFields.setfieldProperty(strKey,"textfont",arialBaseFont,null);
bSuccess = pdfFormFields.setfield(strKey,fieldValues[strKey]);
}
stamper.Formflattening = true;
stamper.Close();
我当前的代码如下:
PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(templatePath,PdfSharp.Pdf.IO.PdfDocumentOpenmode.Modify);
if (doc.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
{
doc.AcroForm.Elements.Add("/NeedAppearances",new PdfSharp.Pdf.PdfBoolean(true));
}
else
{
doc.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
}
foreach (string name in fieldValues.Keys)
{
if (doc.AcroForm.Fields[name] != null)
{
PdfSharp.Pdf.PdfString test = new PdfSharp.Pdf.PdfString(fieldValues[name]);
PdfTextField testField = (PdfTextField)(doc.AcroForm.Fields[name]);
testField.Value = test;
}
}
doc.flatten();
doc.Save(newFile);
当前代码的主要问题:
- 首先,如果我不放置/ NeedAppearances标志,则如果您不单击表单,则pdf似乎为空。
- 第二,如果我插入它,则在使用Acrobat Reader打开它时,总是要求保存pdf。
我似乎找不到解决方法。我所有想尝试的都是使用PdfSharp和ItextSharp尽可能创建相同的结果
编辑:我通过编辑原始模板pdf表单外观解决了字体问题。