Created
September 19, 2024 11:45
-
-
Save Forevka/a30373255d6cf98cb8f4172fde562249 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DevExpress.XtraRichEdit; | |
using DevExpress.XtraRichEdit.API.Native; | |
using System; | |
using System.Collections.Generic; | |
namespace DocumentServerExample1 { | |
class Program { | |
const string templatePath = "Document.docx"; | |
const string resultPath = "..\\..\\result.docx"; | |
static void Main(string[] args) { | |
List<Field> fields = GetFields(); | |
using (var srv = new RichEditDocumentServer()) { | |
srv.LoadDocumentTemplate(templatePath); | |
// Rich Edit Document Server documentation https://goo.gl/gnrRgu | |
Document document = srv.Document; | |
ReplaceFieldWithValues(document, fields); | |
srv.SaveDocument(resultPath, DocumentFormat.OpenXml); | |
} | |
} | |
static void ReplaceFieldWithValues(Document document, List<Field> fields) { | |
var options = SearchOptions.WholeWord; | |
foreach (var field in fields) { | |
string textToFind = String.Format("[{0}]", field.Name); | |
DocumentRange[] ranges = document.FindAll(textToFind, options); | |
foreach (var range in ranges) { | |
document.Replace(range, field.Value); | |
} | |
} | |
} | |
static List<Field> GetFields() { | |
return new List<Field>() { | |
new Field("CustomerName", "qwe"), | |
new Field("CustomerFIO", "zxc"), | |
new Field("ContractNumber", "0000000"), | |
new Field("ContractDate", "07/07/2017"), | |
new Field("CustomerRequisites", "requisites") | |
}; | |
} | |
} | |
public struct Field { | |
public Field(string name, string value) { | |
this.Name = name; | |
this.Value = value; | |
} | |
public string Name { get; } | |
public string Value { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment