This is a migrated thread and some comments may be shown as answers.
Close WordsProcessing Document?
3 Answers 2 Views
This is a migrated thread and some comments may be shown as answers.
Debra
Top achievements
Rank 1
Debra asked on 15 Mar 2021, 08:29 PM

I want to use a then close a RadFlowDocument. 

My current process (will post code):

Open existing RadFlowDocument (word document).

Use RadFlowDocumentEditor to ReplaceText

Save the RadFlowDocument (now edited) as a new RadFlowDocument.  (now it is open on my machine).

Serialize the saved document to send it as an e-mail attachment.

Open the initial RadFlowDocument again

Use RadFlowDocumentEditor to ReplaceText again with a different value

Now I can't serialize the RadFlowDocument because it is open on my machine.

I'm using C# and Blazor

 

Code Sample (This is mostly from https://github.com/telerik/document-processing-sdk/blob/master/WordsProcessing/GenerateDocument/DocumentGenerator.cs)

private RadFlowDocument _document;

protected void OnMySendCommand(GridCommandEventArgs args)

{

OpenFile(fileName);

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(_document);

editor.ReplaceText("Invoice_Number", "T1234567", false, false);

Save("docx", newFileName);

byte[] output = File.ReadAllBytes(newFileName + ".docx");

InsertEmail("myemail@my.com", "Test word file", "Test 1234567", output, "T1234567", true);

 

//Next invoice:

OpenFile(fileName);

editor = new RadFlowDocumentEditor(_document);

editor.ReplaceText("Invoice_Number", "T9999", false, false);

Save("docx", newFileName);

byte[] output2 = File.ReadAllBytes(newFileName + ".docx"); //Error now says:  The process cannot access the file 'E:\Workspace\Intranet\CPE\CPE\Pages\Classes\Invoices\InvoiceTemplate2.docx' because it is being used by another process.

//To Be Clear - it did replace the text and save it to my machine but now can't access the file because it is in use.  

//If necessary, I could save each file with a different file name.  But I would still want to close them programmatically.

}

 

Thanks for any help!

 

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 16 Mar 2021, 10:10 AM

Hello Debra,

Can you share the code of the  OpenFile and Save methods as well? This will give me a better understanding of the exact scenario. 

In addition, you can directly save the file to a byte array instead of saving it to a file and then loading it again. Here is an example of this:  

DocxFormatProvider provider = new DocxFormatProvider();
byte[] bytes = provider.Export(document);

I am looking forward to your reply.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Debra
Top achievements
Rank 1
answered on 16 Mar 2021, 12:26 PM

Dimitar - Actually that is perfect!  I don't actually want or need to save the file. I just thought I would have to but really I just want to read into the array so your code is perfect.  

 

This is my first time using the forums or contacting support.  Wow!  Great response.  Thank you so much.

0
Dimitar
Telerik team
answered on 17 Mar 2021, 06:23 AM

Hello Debra,

I am glad that this solution works for you. 

Do not hesitate to contact us if you have other questions.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Asked by
Debra
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Debra
Top achievements
Rank 1
Share this question
or