@alexey.noskov Please find the attached sample solution and few sample forms which are used to reproduce the issue.
sample input provided as word :
output PDF generated using aspose
Not able to upload the entire solution hence adding a code hence adding a code here
about.aspx.cs
BuildDocText :
public static void BuildDocText()
{
string fileOutputTemporary = Path.GetTempFileName();
try
{
XmlDocument xmlDoc = new XmlDocument();
string fileXmlPath = @"C:\Users\omkar.sakpal\source\repos\WebAppAspose\XMLFile1.xml";
xmlDoc.Load(fileXmlPath);
XmlDocument mergeData = new XmlDocument();
mergeData.InnerXml = xmlDoc.InnerXml;
string docType = mergeData.DocumentElement.GetAttribute("docType");
string rootPath = @"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_PremiumSection.docx";
string outFileName = mergeData.DocumentElement.GetAttribute("PDFFile");
GenerateDoc(mergeData, rootPath);
}
catch (Exception Err)
{
throw new Exception(string.Format(CultureInfo.CurrentCulture, fileOutputTemporary), Err);
}
}
-----------GenerateDoc------------------
private static void GenerateDoc(XmlDocument mergeData, string rootPath)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList exportSections = mergeData.DocumentElement.SelectNodes("ExportSection");
foreach (System.Xml.XmlElement exportSection in exportSections)
{
string exportName = GetExportName(exportSection);
string duplex = GetDuplex(exportSection);
string paperBin = GetPaperBin(exportSection);
DateTime startTime = DateTime.Now;
XmlNodeList subDocs = exportSection.SelectNodes("subForm");
int subDocCount = subDocs.Count;
Aspose.Words.Document finalDocument;
foreach (System.Xml.XmlElement subDoc in subDocs)
{
string sourceFile = GetSubdocSourceFile(rootPath, subDoc);
finalDocument = AddSubDoc(sourceFile, subDoc);
}
}
}
catch (Exception ex)
{
}
finally
{
}
}
Add sub doc and merge are the two methods which uses aspose and club the forms
private static Aspose.Words.Document AddSubDoc(string sourceFile, XmlElement parent)
{
Aspose.Words.Document mainDoc = new Aspose.Words.Document(sourceFile);
try
{
// Load the main document where others will be appended
// Load the additional documents to be appended
Aspose.Words.Document doc1 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_Header_Cov_Line.docx");
Aspose.Words.Document doc2 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovLineBodilyInjury_notWorking.docx");
Aspose.Words.Document doc3 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovLinePropertyDamage.docx");
Aspose.Words.Document doc4 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CovSafetyRidingApparel.docx");
Aspose.Words.Document doc5 = new Aspose.Words.Document(@"C:\Users\omkar.sakpal\Downloads\PremiumSection 2\PremiumSection\MC_CSCredit.doc");
// Append the documents to the main document
mainDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(doc3, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(doc4, ImportFormatMode.KeepSourceFormatting);
mainDoc.AppendDocument(doc5, ImportFormatMode.KeepSourceFormatting);
int lastSectionIndex = doc2.Sections.Count - 1;
if (lastSectionIndex < mainDoc.Sections.Count - 1)
{
MergeSections(mainDoc, lastSectionIndex);
}
//Save the Form into PDF
string filename = @"C:\AsposeDLL\sample.pdf";
mainDoc.Save(filename, Aspose.Words.SaveFormat.Pdf);
}
catch (Exception ex)
{
}
return mainDoc;
}
private static void MergeSections(Aspose.Words.Document _document, int lastSectionIndex)
{
//Initialize
Section previousLastSection = _document.Sections[lastSectionIndex];
Section newFirstSection = _document.Sections[lastSectionIndex + 1];
newFirstSection.ClearHeadersFooters();
//Clear contents of headers and footers. They are not null, just empty.
//This links them to previous section's headers and footers (previousLastSection's)
Body previousLastBody = previousLastSection.Body;
Body newFirstBody = newFirstSection.Body;
Paragraph previousLastParagraph = null;
Table previousLastTable = null;
bool lastParagraphIsEmpty = false;
bool mergeTables = false;
bool mergeTableStyle = true;// Configuration.Forms.MergeTableStyle;
if (previousLastBody != null && newFirstBody != null)
{
Node lastChild = previousLastBody.LastChild;
//Check if the last node of the document is a paragraph or table
if (lastChild != null && lastChild.NodeType == NodeType.Paragraph)
{
previousLastParagraph = (Paragraph)lastChild;
//If the last paragraph is empty, remember this so it can be removed
lastParagraphIsEmpty = true;// ParagraphIsEmpty(previousLastParagraph);
if (mergeTableStyle)
{
lastChild = lastChild.PreviousSibling;
}
}
if (lastChild != null && lastChild.NodeType == NodeType.Table && mergeTableStyle)
{
//previousLastTable = (Table)lastChild;
mergeTables = true;
}
}
//Copy the content from the new section being added to the end of the current document
previousLastSection.AppendContent(newFirstSection);
//Remove the paragraph that used to be that last paragraph of the first section if it was empty.
//This will merge adjacent tables if they were separated only by a paragraph marker
//Tables seperated by nothing are merged automatically by word due to document validation.
if (lastParagraphIsEmpty)
{
previousLastParagraph.Remove();
}
//Delete the second section, a copy of it has been attached to the main document.
newFirstSection.Remove();
if (mergeTables && previousLastTable != null && mergeTableStyle)
{
Table table = previousLastTable;
//Iterate through every table directly following the table at the previous end of the document
//until a non-table node is reached, or all available nodes have been visited.
//Then for each table, row by row, append marge said table to the previoys last table.
while ((table.NextSibling != null) && (table.NextSibling.NodeType == NodeType.Table))
{
Table nextSibling = (Table)table.NextSibling;
while (table.HasChildNodes && nextSibling.FirstRow != null)
{
/* Workaround to Border Style issue with Aspose - remove when fixed
* see http://www.aspose.com/community/forums/thread/587638/docx-joining-two-tables-and-saving-to-pdf-changes-border-style.aspx
* Aspose issue WORDSNET-11049
*/
Row row = nextSibling.FirstRow;
BorderCollection borders = row.RowFormat.Borders;
table.Rows.Add(row);
table.LastRow.RowFormat.Borders.Left.LineStyle = borders.Left.LineStyle;
table.LastRow.RowFormat.Borders.Right.LineStyle = borders.Right.LineStyle;
table.LastRow.RowFormat.Borders.Top.LineStyle = borders.Top.LineStyle;
table.LastRow.RowFormat.Borders.Bottom.LineStyle = borders.Bottom.LineStyle;
}
//Delete the table that was just merged in
//A copy of it was just merged to the previous last table.
nextSibling.Remove();
}
}
}
The probable cause is
Once sub forms have been appended then it tries to re-align the sub form exactly below to main form where it fails and which is causing formatting and alignment issues , when the subform has large number of section than the main form
As shown in method snippet.
This method tries to readjust the section and alignment of sub form, and due to which there are formatting issues
Forms used
MC_CovComprehensive_NotWorking.docx (16.9 KB)
MC_CovLineBodilyInjury_notWorking.docx (18.0 KB)
MC_CovSafetyRidingApparel.docx (16.9 KB)
MC_Header_Cov_Line.docx (17.8 KB)
MC_PremiumSection.docx (15.9 KB)