How to convert Byte array of Image into .docx file?
Hi, I have an Image data type field in database. I need to convert those files in to .docx.I am using open xml.Please suggest me How can I convert those Image byte array into .docx files?
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
Shubham Gangrade76 reputationPosted Jul 7, 2020, 4:31 AM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Jun 14, 2023, 4:07 PM EDT
An option that can be used is to leverage the DocumentConverter class which would allow you to pass the byte data from the database, convert it to a stream, and then output a .docx file. Here is some code that demonstrates this:
static void Main(string[] args) { var outBytes = ConvertStreams(new MemoryStream(inBytes)); // supply your byte data here File.WriteAllBytes("C:\Output\file.docx", outBytes.ToArray()); } static MemoryStream ConvertStreams(Stream inputStream) { var ms = new MemoryStream(); //output stream using var document = DocumentFactory.LoadFromStream(inputStream, new LoadDocumentOptions()); using var documentConverter = new DocumentConverter(); var jobData = new DocumentConverterJobData { Document = document, DocumentFormat = DocumentFormat.Docx, OutputDocumentStream = ms }; var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); if (job.Status == DocumentConverterJobStatus.Success) return ms; else return null; }https://www.leadtools.com/help/sdk/tutorials/dotnet-console-convert-files-with-the-document-converter.html
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 15, 2023, 10:58 PM EDT
To convert image byte arrays into a .docx file using Open XML, you can follow these steps:
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 15, 2023, 10:59 PM EDT
using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using A = DocumentFormat.OpenXml.Drawing; using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing; // Assuming you have an array of image byte arrays byte[][] imageByteArrays = GetImageByteArraysFromDatabase(); using (WordprocessingDocument document = WordprocessingDocument.Create("Output.docx", WordprocessingDocumentType.Document)) { // Create the main document part MainDocumentPart mainPart = document.AddMainDocumentPart(); mainPart.Document = new Document(); // Create a body for the document Body body = mainPart.Document.AppendChild(new Body()); // Loop through each image byte array foreach (byte[] imageBytes in imageByteArrays) { // Create an image part and add it to the main document part ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (System.IO.MemoryStream stream = new System.IO.MemoryStream(imageBytes)) { imagePart.FeedData(stream); } // Create a unique relationship ID for the image string relationshipId = mainPart.GetIdOfPart(imagePart); // Create a new paragraph and run for the image Paragraph paragraph = body.AppendChild(new Paragraph()); Run run = paragraph.AppendChild(new Run()); // Create a drawing and add it to the run Drawing drawing = run.AppendChild(new Drawing()); DW.Inline inline = drawing.AppendChild(new DW.Inline()); // Create a graphic and add it to the inline A.Graphic graphic = inline.AppendChild(new A.Graphic()); graphic.GraphicData = new A.GraphicData(new A.Picture(new A.BlipFill(new A.Blip() { Embed = relationshipId }), new A.Stretch(new A.FillRectangle()))); // Set the size of the image inline.Extent = new A.Extent() { Cx = 500000, Cy = 500000 }; // Adjust the size as needed // Save the document document.Save(); } }Sapna GirishRank #1,811 · 819 reputationPosted Jul 6, 2020, 4:06 PM EDT