I have noticed that the problem is already in the pdf template. When I open the pdf template with acrobat reader and close it there is already the saving-prompt.
So what I did is to click on yes and saved the template.
When I use this saved template with pdfsharp and generate a document, then the generated document does not have a saving-prompt anymore.
So this problem seemed to be resolved, but when I do it this way, the acroform fields of the generated document does not show the value I set with pdfsharp anymore.
So I googled and found this solution:
Code:
if (pdfDocument.AcroForm != null)
{
if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
{
pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
}
else
{
pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
}
}
With this code the text will be shown in the acroform textfields again, but the the saving-prompt appears again too.
So I think the problem is connected to this parameter. But how can I reach both, no saving-prompt and the correct value in the acroform textfields?
Here you can see my complete test code:
Code:
public void generateTestDocument()
{
PdfDocument pdfDocument = PdfReader.Open("Template_saved.pdf", PdfDocumentOpenMode.Modify);
if (pdfDocument != null)
{
PdfAcroForm acroForm = pdfDocument.AcroForm;
if (pdfDocument.AcroForm != null)
{
if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
{
pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
}
else
{
pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
}
}
if (acroForm != null)
{
PdfAcroField.PdfAcroFieldCollection fields = acroForm.Fields;
string[] names = fields.Names;
for (int i = 0; i < names.Length; i++)
{
string fqName = names[i];
PdfAcroField field = fields[fqName];
PdfTextField txtField;
field.ReadOnly = false;
if ((txtField = field as PdfTextField) != null)
{
string text = "[test]";
txtField.Value = new PdfString(text);
}
field.ReadOnly = true;
}
}
pdfDocument.Flatten();
pdfDocument.Save("GeneratedDocument.pdf");
}
}