Hi Aspose Support,
Please find below method used to generate PDF. I am taking the Word Document path value from database. Please also find the attached Word Documents which are used to generate the PDF (SupportingPage_1.docx, FNMA-1004.docx, FNMA-1004-Comps.docx, FNMA-1004-1.docx, FNMA-MC.docx & SupportingPage_2.docx)
public bool CreatePDFOrAndXML(int parentTemplateId, int templateId, string selectedPageByTemplate, String[] fieldNames, Object[] fieldValues,
string fileName, string pdfPath, bool includeXML)
{
bool result = false;
try
{
TemplateRepository templateRepository = new TemplateRepository();
List templateList = templateRepository.GetTemplateListByParentId(parentTemplateId);
if (templateId > 0)
{
templateList = templateList.Where(t => t.TemplateId == templateId).ToList();
}
Document document = new Document();
SectionCollection secCollection = document.Sections;
// delete all sections in the blank document
foreach (Section section in secCollection)
{
section.Remove();
}
if (templateList != null && templateList.Count > 0)
{
foreach (TemplateDTO templateDTO in templateList)
{
Document templateDocument = new Document(HttpContext.Current.Server.MapPath(templateDTO.FilePath));
if (!string.IsNullOrEmpty(selectedPageByTemplate))
{
DocumentBuilder docBuilder = new DocumentBuilder(templateDocument);
string[] selectedPageByTemplateList = selectedPageByTemplate.Split(Constants.Separator);
if (selectedPageByTemplateList != null && selectedPageByTemplateList.Length > 0)
{
List sectionList = new List();
foreach (string mergetag in selectedPageByTemplateList)
{
if (docBuilder.MoveToMergeField(mergetag, false, false))
{
sectionList.Add(docBuilder.CurrentSection);
}
}
SectionCollection templateSectionCollection = templateDocument.Sections;
foreach (Section section in templateSectionCollection)
{
if (!sectionList.Contains(section))
{
section.Remove();
}
}
}
}
document.AppendDocument(templateDocument, ImportFormatMode.KeepSourceFormatting);
}
// Hide Section if data is not present
MergeTagRepository mergeTagRepository = new MergeTagRepository();
List mergeTagMappingRuleDTOList = mergeTagRepository.GetMergeTagMappingRules().Where(m => m.IsRemoveSection == true).ToList();
foreach (MergeTagMappingRuleDTO mergeTagMappingRuleDTO in mergeTagMappingRuleDTOList)
{
int index = Array.IndexOf(fieldNames, mergeTagMappingRuleDTO.MergeTag);
if (index > 0 && string.IsNullOrEmpty(fieldValues[index].ToString().Trim()))
{
DocumentBuilder builder = new DocumentBuilder(document);
if (builder.MoveToMergeField(mergeTagMappingRuleDTO.MergeTag, false, false))
{
Section section = builder.CurrentSection;
section.Remove();
}
}
}
}
#region Set Legal Size for all Sections in Document
SectionCollection sectionCollection = document.Sections;
// Set PDF to Legal size
foreach (Section section in sectionCollection)
{
section.PageSetup.PaperSize = Aspose.Words.PaperSize.Legal;
}
#endregion Set Legal Size for all Sections in Document
if (document.Sections.Count > 0)
{
NodeCollection runs = document.LastSection.GetChildNodes(NodeType.Run, true);
// Find the runs that contain the last page break characters in this section and remove the character.
for (int i = runs.Count - 1; i >= 0; i--)
{
Run run = (Run)runs[i];
if (run.Text.IndexOf(ControlChar.PageBreakChar) >= 0)
{
run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), 1);
break;
}
}
// Setup mail merge event handler to do the custom work.
document.MailMerge.FieldMergingCallback = new HandleMergeField();
// Execute mail merge.
document.MailMerge.Execute(fieldNames, fieldValues);
}
// Save the document.
document.Save(pdfPath + fileName + Constants.DOCX);
// Save the pdf.
document.Save(pdfPath + fileName + Constants.PDF);
if (includeXML)
{
// Save the XML
GenerateXML(pdfPath + fileName + Constants.PDF);
}
result = true;
}
catch (Exception ex)
{
throw ex;
}
return result;
}