Hi Alexey
I have attached the source and output documents.
The main code that does the replacement is too large to paste here but essentially we are replacing the
[!SomeKey]
blocks in the raw RTF string with information strings like names and addresses.
Below is the code that inserts the barcodes which i thought was responsible for the document re-arranging itself.
Thanks
private string ReplaceRTFBarCodeFields(string information, int customerID, int leadID, int matterID, int documentTypeID)
{
Document wordDoc = Aquarium.Documents.DocumentConverter.GetAsposeDocumentFromRTF(information);
barcodeDetails = new RTFBarcodeDetails(wordDoc, leadID, matterID, customerID, documentTypeID);
wordDoc.Range.Replace(new Regex(@"\[!CustomerBarCode\]"), new ReplaceEvaluator(MyReplaceEvaluator), true);
wordDoc.Range.Replace(new Regex(@"\[!LeadBarCode\]"), new ReplaceEvaluator(MyReplaceEvaluator), true);
wordDoc.Range.Replace(new Regex(@"\[!MatterBarCode\]"), new ReplaceEvaluator(MyReplaceEvaluator), true);
wordDoc.Range.Replace(new Regex(@"\[!DocumentBarCode\]"), new ReplaceEvaluator(MyReplaceEvaluator), true);
wordDoc = barcodeDetails.Document;
information = Aquarium.Documents.DocumentConverter.GetStringFromDocument(wordDoc, Aquarium.Documents.DocumentFormat.RTF);
return information;
}
private ReplaceAction MyReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
string barcodeImageURLFormat = [http://www.aquarium-software.com/BarCodeImage.ashx?ID={0}&type={1}](http://www.aquarium-software.com/BarCodeImage.ashx?ID=%7B0%7D&type=%7B1%7D);
DocumentBuilder builder = new DocumentBuilder(barcodeDetails.Document);
builder.MoveTo(e.MatchNode);
switch (e.Match.Value)
{
case @"[!CustomerBarCode]":
builder.InsertImage(string.Format(barcodeImageURLFormat, barcodeDetails.CustomerID, 'C'));
break;
case @"[!LeadBarCode]":
builder.InsertImage(string.Format(barcodeImageURLFormat, barcodeDetails.LeadID, 'L'));
break;
case @"[!MatterBarCode]":
builder.InsertImage(string.Format(barcodeImageURLFormat, barcodeDetails.MatterID, 'M'));
break;
case @"[!DocumentBarCode]":
builder.InsertImage(string.Format(barcodeImageURLFormat, barcodeDetails.DocumentTypeID, 'D'));
break;
}
e.Replacement = string.Empty;
barcodeDetails.Document = builder.Document;
return ReplaceAction.Replace;
}