"The document element is associated with another document " When trying to merge documents

1 Answer 274 Views
PdfProcessing WordsProcessing
Lucas
Top achievements
Rank 1
Lucas asked on 18 Oct 2021, 02:42 PM | edited on 18 Oct 2021, 03:00 PM

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 ! 

Dimitar
Telerik team
commented on 19 Oct 2021, 07:29 AM

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

1 Answer, 1 is accepted

Sort by
0
Leslie
Top achievements
Rank 1
Iron
answered on 29 Oct 2021, 03:27 PM
when i encountered this for the first time, i used the .Clone method on the element to avoid the error
Tags
PdfProcessing WordsProcessing
Asked by
Lucas
Top achievements
Rank 1
Answers by
Leslie
Top achievements
Rank 1
Iron
Share this question
or