I'm using WinForm C#. How can I open a byte into a DocumentViewer? the byte[] is a .doc or .xps file..
first how to add a document viewer a winform?
I'm using WinForm C#. How can I open a byte into a DocumentViewer? the byte[] is a .doc or .xps file..
first how to add a document viewer a winform?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rahul PrasadRank #2,557 · 546 reputationPosted Feb 11, 2022, 4:39 AM EST
Hi, you could use Spire.DocViewer instead to load Word documents from stream and view it in a Windows Form application.
For more details, see this link:
https://www.e-iceblue.com/Introduce/word-viewer-net-introduce.html
Ganesan CRank #369 · 6.8K reputationPosted Feb 8, 2022, 1:50 AM EST
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 27, 2023, 10:22 PM EDT
Even though this is a very old post, here is my approach to solve the problem.
Part 1:
To add a DocumentViewer to a WinForm, you can follow these steps:
1. Open your WinForm project in Visual Studio.
2. From the Toolbox, locate the "DocumentViewer" control under the "Reporting" section.
3. Drag and drop the DocumentViewer control onto your WinForm.
4. Resize the control to fit your needs.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 27, 2023, 10:23 PM EDT
Part 2:
Once you have added the DocumentViewer control to your WinForm, you can use the following code to display a .doc or .xps file from a byte array:
// Load the document from the byte array byte[] fileContents = /* your file contents */; MemoryStream stream = new MemoryStream(fileContents); // Create a DocumentViewer and set its Document property DocumentViewer docViewer = new DocumentViewer(); docViewer.Document = new System.Windows.Xps.Packaging.XpsDocument(stream, System.IO.Packaging.CompressionOption.NotCompressed).GetFixedDocumentSequence(); // Add the DocumentViewer to your WinForm this.Controls.Add(docViewer);This code loads the document from the byte array into a MemoryStream, then creates an XpsDocument object from the stream. The XpsDocument object is then used to create a FixedDocumentSequence object, which is assigned to the DocumentViewer control's Document property. Finally, the DocumentViewer control is added to your WinForm's Controls collection.
Marvin ReidRank #1,963 · 746 reputationPosted Jan 9, 2024, 3:00 PM EST
You'll want to ensure the solution you use supports the file formats you plan on working with. A route you can take is by passing the byte array into a MemoryStream and loading the file using the LoadFromStream method. This will give you a LEADDocument which you can supply into the DocumentViewer.
Here is some sample code and a tutorial which you can supply this into.
byte[] data = File.ReadAllBytes(@"Insert path to file"); using (MemoryStream ms = new MemoryStream(bytes)) { DocumentFactory.LoadFromStream(ms, new LoadDocumentOptions()); }https://www.leadtools.com/help/sdk/tutorials/dotnet-winforms-display-files-in-the-document-viewer.html