Hello,
I am currently trying to write in the "Modele.docx" document that is just a template. For each OperationAffecteeViewModel, I want to write in this template, and then copy what i just wrote in "documentFinal.docx". But the application crashes at the line
documentFinal.Sections.Add(copySection);
with the following error : The document element is associated with another document
here's the code :
private async Task GenererOperations()
{
RadFlowDocument document = new RadFlowDocument(); ;
RadFlowDocument documentFinal = new RadFlowDocument();
var docxProvider = new DocxFormatProvider();
foreach (OperationAffecteeViewModel operationAffectee in OperationsAffectees)
{
using (Stream input = File.OpenRead("Modele.docx"))
{
document = docxProvider.Import(input);
}
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
GoToBookmark(document, editor, "OUVRAGE");
editor.InsertText(operationAffectee.LibelleOuvrage);
GoToBookmark(document, editor, "OPERATION");
editor.InsertText(operationAffectee.LibelleTypeOperation);
GoToBookmark(document, editor, "DATE_PRÉVUE");
editor.InsertText(operationAffectee.DatePrevisionnelle.Date.ToString("dd/MM/yyyy"));
}
//chargement de la police pour éviter de perdre les caractères spéciaux lors de l'exportation
var fontData = File.ReadAllBytes(@"wwwroot/fonts/Calibri_Regular.ttf");
FontsRepository.RegisterFont(new Telerik.Documents.Core.Fonts.FontFamily("Calibri"), FontStyles.Normal, FontWeights.Normal, fontData);
var providerPdf = new PdfFormatProvider();
using (Stream outStream = File.OpenWrite("sample.pdf"))
{
providerPdf.Export(document, outStream);
}
await InvokeAsync(() => this.StateHasChanged());
}
private void GoToBookmark(RadFlowDocument document, RadFlowDocumentEditor editor, string bookmarkName)
{
var bookmark = document.EnumerateChildrenOfType<BookmarkRangeEnd>().FirstOrDefault(rangeStart => rangeStart.Bookmark.Name == bookmarkName);
if (bookmark != null)
{
editor.MoveToInlineEnd(bookmark.Paragraph.Inlines[0]);
}
}
Thanks for helping me !
Lucas
I cannot see the specified line in your code. Could you please check the snippet and make sure it is the correct one? Or am I missing something?
Regards,
Dimitar