Hello,
I contact you in order to help me to resolve an issue during combining several docx files in one pdf file. I have defined some cross referenced in my word INPUT4.docx. After transformation I noticed that ‘Error! Reference source not found.Error!’ is raised.
Could you investigate on it ?
I let the input in a zip file (input.zip) and the output with Word DLL 18.1 and 18.3.
result_word_dll_18.1.pdf (396.2 KB)
result_word_dll_18.3.pdf (396.5 KB)
input.zip (183.3 KB)
Please find below the code used :
First define in a class :
public Document DocxFile { get; set; }
public MemoryStream StreamOut { get; set; }
public MemoryStream StreamOutPdf { get; set; }
Then add combineFunction() below :
public void combineFunction()
{
try
{
//Get input data
List docs = new List(files);
Document doc1 = new Document(“INPUT1.docx”);
Document doc2 = new Document(“INPUT2.docx”);
Document doc3 = new Document(“INPUT3.docx”);
Document doc4 = new Document(“INPUT4.docx”);
Document doc5 = new Document(“INPUT5.docx”);
docs.Add(doc1);
docs.Add(doc2);
docs.Add(doc3);
docs.Add(doc4);
docs.Add(doc5);
Document dest = null;
Boolean firstDoc = true;
foreach (Document d in docs)
{
if (firstDoc)
{
dest = d;
//Margins
DocumentBuilder builder = new DocumentBuilder(dest);
PageSetup ps = builder.PageSetup;
ps.LeftMargin = ConvertUtil.MillimeterToPoint(25);
ps.RightMargin = ConvertUtil.MillimeterToPoint(25);
foreach (Section srcSection in dest)
{
srcSection.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(25);
srcSection.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(25);
}
firstDoc = false;
}
else
{
foreach (Section srcSection in d)
{
Section newSection = (Section)dest.ImportNode(srcSection, true, ImportFormatMode.KeepDifferentStyles);
newSection.HeadersFooters.LinkToPrevious(true);
newSection.PageSetup.RestartPageNumbering = false;
newSection.PageSetup.PageNumberStyle = NumberStyle.Arabic;
newSection.PageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(25);
newSection.PageSetup.RightMargin = ConvertUtil.MillimeterToPoint(25);
dest.Sections.Add(newSection);
}
}
}
RemoveSectionBreaks(dest);
//Save output
try
{
((Aspose.Words.Document)dest).Save(StreamOut, SaveFormat.Docx);
}
catch (Exception ex)
{
logger.Error(“Error on ASPOSE DOCX transformation.”, ex);
}
}
catch (Exception globalEx)
{
logger.Error(“Error in Docx merge method.”, globalEx);
}
ToPdf();
}
Below the function RemoveSectionBreaks(Document doc):
protected static void RemoveSectionBreaks(Document doc)
{
// Loop through all sections starting from the section that precedes the last one
// and moving to the first section.
Section mysection = doc.LastSection;
int nbSection = doc.Sections.Count-1;
for (int i = nbSection ; i >= 1; i–)
{
if(CompareFormatSection(doc,i))
{
// Copy the content of the current section to the beginning of the last section.
mysection.PrependContent(doc.Sections[i-1]);
// Remove the copied section.
doc.Sections[i-1].Remove();
}
else
{
mysection = doc.Sections[i-1];
}
}
}
Below the function ToPdf() :
private void ToPdf()
{
try
{
PdfSaveOptions options = new PdfSaveOptions();
options.ImageCompression = PdfImageCompression.Jpeg;
options.JpegQuality = 100;
options.FontEmbeddingMode = PdfFontEmbeddingMode.EmbedNone;
try
{
((Aspose.Words.Document)this.DocxFile).Save(this.StreamOutPdf, (Aspose.Words.Saving.PdfSaveOptions)options);
}
catch (Exception ex)
{
logger.Error(“Error on ASPOSE PDF transformation.”, ex);
throw ex;
}
}
catch (Exception globalEx)
{
logger.Error(“Error in HTML conversion method.”, globalEx);
throw globalEx;
}
}
Below the function CompareFormatSection(Document doc, int i) :
private static Boolean CompareFormatSection(Document doc, int i)
{
if (doc.Sections[i].PageSetup.Orientation.Equals(doc.Sections[i - 1].PageSetup.Orientation) &&
doc.Sections[i].PageSetup.PaperSize.Equals(doc.Sections[i - 1].PageSetup.PaperSize) &&
doc.Sections[i].PageSetup.LeftMargin.Equals(doc.Sections[i - 1].PageSetup.LeftMargin) &&
doc.Sections[i].PageSetup.RightMargin.Equals(doc.Sections[i - 1].PageSetup.RightMargin))
{
return true;
}
else return false;
}
Many thanks
Best regards