We're sorry Aspose doesn't work properply without JavaScript enabled.

# Specify the fields that are set as form fields on save

Hello,

the Aspose.Words API allows preserve the form fields created in a word document to set as a Pdf Document.

The reference https://reference.aspose.com/words/net/aspose.words.saving/pdfsaveoptions/properties/preserveformfields shows how to use it.

Is there a way I can control if a field is transformed or not?

My use case:

• I do have form fields in header and footer
• I do have form fields in the content area

On save as pdf I want only the content area form fields preserved, but the header and footer form fields should be saves as text.

Any way to achieve this?

Most likely in your document you use Structured Document Tags because legacy form fields cannot be inserted in Header footer. In this case, you can simply replace Structured Document Tags in headers and footers of your document with their text representation. for example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Convert sdts in the document's header and footers with text.
{
NodeCollection sdts = hf.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (StructuredDocumentTag sdt in sdts)
{
if (sdt.SdtType == SdtType.PlainText)
{
foreach (Node child in sdt.ChildNodes)
{
sdt.ParentNode.InsertAfter(child, sdt);
}
sdt.Remove();
}
}
}

PdfSaveOptions opt = new PdfSaveOptions();
opt.PreserveFormFields = true;
doc.Save(@"C:\Temp\out.pdf", opt);


Hello,
if I do understand your approach the correct way you mean:

2. modify header footer fields and replace with text representation
3. save as Pdf with PreserveFormFields set to “true”

Correct?

BTW, you code creates and endless loop and never stops.

@rausch Yes, you understand the proposed approach correctly. The code replaces structured document tags with their content. Please attach your document here for testing. The provided code works properly with a simple test document I created on my side.

test.docx (29.1 KB)

Sample Word Document where I tried your code.

@rausch Please find the modified code:

Document doc = new Document(@"C:\Temp\in.docx");

// Convert sdts in the document's header and footers with text.
{
NodeCollection sdts = hf.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (StructuredDocumentTag sdt in sdts)
{
if (sdt.SdtType == SdtType.PlainText)
{
while(sdt.HasChildNodes)
{
sdt.ParentNode.InsertAfter(sdt.LastChild, sdt);
}
sdt.Remove();
}
}
}

PdfSaveOptions opt = new PdfSaveOptions();
opt.PreserveFormFields = true;
doc.Save(@"C:\Temp\out.pdf", opt);


It works properly with the attached document on my side.

Thanks for your quick help, works now as expected. Thank you.