In my app, I have created a Word.docx file. I can open it and read it to an Editor Control in .NET Maui and below is the code that I use:
public string ReadDocument(string fileName)
{
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), fileName);
// Ensure the file exists
if (!File.Exists(filePath))
{
return "File not found!";
}
// Open the document
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
// Load the document using Syncfusion's WordDocument class
WordDocument document = new WordDocument(fileStream, FormatType.Docx);
// Extract the text content (example: reading the first section)
string documentText = string.Empty;
foreach (WSection section in document.Sections)
{
foreach (WParagraph paragraph in section.Paragraphs)
{
documentText += paragraph.Text + "\n";
}
}
// Close the document
document.Close();
// Return the document's text
return documentText;
}
I just want to open the Word file and display the contents using the Word program. (Like is done with the SaveService method.
How do I simply open an existing file and show it in the Word app?
Hi Frederick,
From the provided details, we understood that your requirement is to open the
Word document in Microsoft Word application using our Syncfusion DocIO library in.NET
MAUI application. We have created and attached the sample application for your requirement.
In the provided sample, we use a partial class called ViewFile, and its
complete saving logic varies based on the platform, such as Windows, Android,
or iOS. For each platform, we provide a platform-specific implementation. For
Windows platform, the SaveWindow.cs file contains the partial implementation of
ViewFile. This file is included in the Windows platform folder.
Regards,
Dharanya.
Thanks Dharanya that works, but I have a new issue. In my .NET Maui app, I storing the Word document in one of the LocalApplicationData folders and the View program will not find the Word file. However, if I use the full path string as shown in the example below it will work, it finds the file and opens the file up in MS Word.
//This works
//SaveService saveService = new();
//saveService.ViewFile(@"C:\Users\xxxxx\AppData\Local\Packages\33033zzzzzz.5810028C1670B_zzzzzzzz\LocalCache\Local\Workflow for yyyyyyy 2024b.docx");
//This does not work
string tempDocPath = App.docPath;
//This does not work tempDocPath = "C:\\Users\\xxxxx\\AppData\\Local\\Workflow for yyyyyyyy 2024b.docx";
SaveService saveService = new();
saveService.ViewFile(@App.docPath);
What is causing this error? Is it in the ViewFile code
Frederick,
We tried to reproduce the issue as you mentioned in your response, but
everything is working fine on our end, and we were able to retrieve the
document without any errors. We have attached a sample which we tried at our
end.
To reproduce the issue at our end, kindly share the below details:
1. The specific error you are facing.
2. Issue reproducible sample.
Based on the information you provide, we will check further and provide a
solution.
Regards,
Dharanya.
Here is the example code that shows the error when I try to open a file from the .NET Maui
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).
In this sample the Word file DocIOTest.dock is added to the LAD folder, the app opens and tries to read the file but cannot find the file.
Reading from a local folder works.
Can you fix this code for the DocIO library to work in this example?
Here is the App.xaml.cs that will load the test file.
using System.Reflection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
namespace Open_and_save_Word_document
{
public partial class App : Application
{
internal static string docPath = "C:\\Temp\\DocIOTest.docx";
public static string LADPath { get; } = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DocIOTest.docx");
public App()
{
InitializeComponent();
if (!File.Exists(LADPath))
{
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(App)).Assembly;
using (Stream stream = assembly.GetManifestResourceStream("Open_and_save_Word_document.DocIOTest.docx"))
{
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
File.WriteAllBytes(LADPath, memoryStream.ToArray());
}
}
}
MainPage = new AppShell();
}
}
}
Here is the Open and Save code.
private void OpenAndSaveDocument(object sender, EventArgs e)
{
SaveService saveService = new();
//Works
//saveService.ViewFile("D:\\Support\\Documents\\Input.docx");
string tempPath = Path.Combine(FileSystem.AppDataDirectory, "DocIOTest.docx");
//Does not work
saveService.ViewFile(@tempPath);
//Does not work
string tempLADPath = @App.LADPath;
//saveService.ViewFile(@App.LADPath);
}
Thanks
Frederick,
We have retrieved the input document from the LocalApplicationData path without
any errors. We have attached the modified sample and video demonstrating
how we retrieve the input document and run the sample without issues while
viewing the document.
If you are still facing the issue, please share a video that includes the following details:
1. The input document path location.
2. The code snippet where the variable holds the input path.
3. The exact error message thrown, along with the corresponding code.
Regards,
Dharanya.
Dharanya ,
Attached is a video of my error when opening the Word docx file.
I hope this identifies the issue. My LAD path is a lot longer.
Frederick,
We have seen the video you provided, and we suspect that the error might be
occurring due to the following line of code in your sample HelpPage.xaml.cs:
|
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"DocIOTest.docx"); |
To resolve the issue, we recommend the following steps:
Store the first part of the path, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), in a new variable.
Check the path location in File Explorer to verify if the input document is present in that location as in the new variable path value.
Refer to the below screenshot to get the first
part path value. We suspect that the path might not match the document's
location in your File Explorer, which could be the reason for the error.
Please refer to the attached video and the modified sample in the
below attachment. If you continue to encounter the issue, kindly share a
screenshot of the first part of path location, similar to the above screenshot.
Based on that we will check and provide you the solution further.
Regards,
Dharanya.
Frederick,
To resolve the error you are facing, please try one of the following solutions:
Suggestion 1 : Use complete path of input file
Use the following code to open the document by specifying the exact full file
path:
|
public static string inputPath1 { get; } = System.IO.Path.Combine("C:\Users\xyz\AppData\Local\”, "Packages\33033abletFactory.5810028C1670B_p5a1ejzetvpqj\LocalCache\Local\Input.docx"); |
Suggestion 2 : Maintain input file in LocalApplicationData
Place the input document in "C:\Users\fswit\AppData\Local\” and use
the below code snippet
|
public static string inputPath { get; } = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Input.docx"); |
Note: This
issue is not related to the DocIO library. It is standard file-opening code in
C#. Make sure that the file path you specify is correct, as the application
will not throw any errors.
Regards,
Dharanya.
Dharanya,
The issue was resolved by changing the following code:
public static string destinationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Input.docx");
SaveService saveService = new();
saveService.ViewFile(destinationPath);
Thanks for all your help.
FTs
Hi Frederick,
We are glad to hear that the issue has been resolved. Feel free to contact us if you have any further questions or need additional assistance.
Regards,
Chris