Inserção de imagem no Word com DocumentFormat.OpenXml

Adilson Medeiros 0 Reputation points
2024-12-16T18:28:23.34+00:00

Não estou conseguindo abrir um arquivo docx que foi criado de forma programática usando C#

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Drawing.Wordprocessing;

using DocumentFormat.OpenXml.Drawing;

using DocumentFormat.OpenXml.Packaging;

using  DocumentFormat.OpenXml.Wordprocessing;

using Pic = DocumentFormat.OpenXml.Wordprocessing.Picture;

using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph;

using Run = DocumentFormat.OpenXml.Wordprocessing.Run;

using System.IO;

namespace InsertImage

{

class Program

{

    static void Main(string[] args)

    {

        // Criar um novo documento

        using (WordprocessingDocument package = WordprocessingDocument.Create("MyDocument.docx", WordprocessingDocumentType.Document))

        {

            MainDocumentPart mainPart = package.AddMainDocumentPart();

            mainPart.Document = new Document();

            Body body = mainPart.Document.AppendChild(new Body());

            // Adicionar uma parte de imagem

            ImagePart imagePart = package.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);

            using (FileStream stream = new FileStream("image1.jpeg", FileMode.Open))

            {

                imagePart.FeedData(stream);

            }

            // Criar um elemento de imagem e definir o tamanho (ajustar os valores EMU conforme necessário)

            Drawing drawing = new Drawing();

            Inline inline = new Inline();

            Extent extent = new Extent() { Cx = 404 * 9525L, Cy = 554 * 9525L }; // Ajustar o tamanho em EMU

            Graphic graphic = new Graphic();

            GraphicData graphicData = new GraphicData();

            Pic pic = new Pic();

            // ... outras configurações da imagem

            // Adicionar o elemento de imagem ao corpo

            body.AppendChild(new Paragraph(new Run(drawing)));

        }

    }

}
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,146 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,095 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.