Hello,
When I make a conversion for replace a tag in a Docx to another Docx. Section Break, break page are not keep in resultat.
Could you give me an advice for keep break in result docx?
Code :
public void InsertDocx2docxTest()
{
List filesToInsert = new List();
List insertTags = new List();
Document d1= new Document(@“D:\Temp\test insertdocx2docx\WORD_1.docx”);
Document d0 = new Document(@“D:\Temp\test insertdocx2docx\WORD_2.docx”);
MemoryStream m1 = new MemoryStream();
d1.Save(m1,SaveFormat.Docx);
filesToInsert.Add(m1);
insertTags.Add(“DOCPROPERTY SHORT_FORM_JUSTIF \* MERGEFORMAT”);
d0.Save(@“D:\Temp\test insertdocx2docx\WORD_2_res.docx”);
d1.Save(@“D:\Temp\test insertdocx2docx\WORD_1_res.docx”);
//Search all tag in order to insert the doc at the right place
NodeCollection allSections = d0.GetChildNodes(NodeType.Section, true);
List nodeToDelete = new List();
int iLoop = 0, nbTag = 0;
StringBuilder errorTags = new StringBuilder();
errorTags.AppendLine(“Tag(s) Unknow :”);
Dictionary<String, MemoryStream> dico = new Dictionary<String, MemoryStream>();
foreach (String tag in insertTags)
{
dico.Add(tag, filesToInsert[iLoop]);
iLoop++;
}
DocumentBuilder builder = new DocumentBuilder(d0);
foreach (KeyValuePair<String, MemoryStream> pair in dico)
{
if (d0.GetText().IndexOf(pair.Key) >= 0)
{
FindAndInsertDocument replacedoc = new FindAndInsertDocument(pair.Value);
d0.Range.Replace(new Regex(pair.Key), replacedoc, false);
if (!replacedoc.Replaced)
{
foreach (Field field in d0.Range.Fields)
{
if (field.Type == Aspose.Words.Fields.FieldType.FieldDocProperty && field.GetFieldCode().Contains(pair.Key))
{
builder.MoveToField(field, true);
builder.InsertDocument(new Document(pair.Value), ImportFormatMode.KeepSourceFormatting);
break;
}
}
}
nbTag++;
}
else
{
errorTags.AppendLine(pair.Key);
}
}
// Test if all tags are put in document
if (dico.Count != nbTag)
{
ConverterMessage myConvertMessage = ConverterMessage.CSCT_E_005;
myConvertMessage.formatMessage(errorTags.ToString());
logger.Error(myConvertMessage.MessageContent);
throw new ConverterException(myConvertMessage, null);
}
else
{
d0.Save(@“D:\Temp\test insertdocx2docx\WORD_FINAL.docx”);
}
}
regards