We have a requirement to insert quick parts into Word documents. We need to insert a field from the document’s CustomDocumentProperties collection. Can we do that using Aspose?
I saw that DocumentBuilder.InsertField() method is used to insert quick parts. However, the method throws an exception if I provide the name of a field that is in the document’s CustomDocumentProperties collection.
FYI, the custom document properties come from SharePoint. The document is saved in a SharePoint library that adds and sets those fields via the document’s content type.
Here is an example:
try
{
Document doc = new Document(@"somepath\somedoc.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
string fieldName = null;
if (doc.CustomDocumentProperties.Count > 0)
{
if (doc.CustomDocumentProperties["FIELD_NAME"] != null)
{
fieldName = "FIELD_NAME";
}
}
if (!string.IsNullOrEmpty(fieldName))
{
builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Custom Property ");
builder.InsertField(fieldName);
doc.Save(doc.OriginalFileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}