C# DocumentFormat.OpenXml将图像添加到word文档

C# DocumentFormat.OpenXml将图像添加到word文档,c#,asp.net,openxml,openxml-sdk,C#,Asp.net,Openxml,Openxml Sdk,我正在使用OpenXMLSDK创建一个简单的word文档。 到目前为止,它正在发挥作用。 现在,如何将文件系统中的图像添加到此文档?我不在乎它在文件里的什么地方,只是它在那里。 谢谢 这是我到目前为止所拥有的 string fileName = "proposal"+dealerId +Guid.NewGuid().ToString()+".doc"; string filePath = @"C:\DWSApplicationFiles\Word\" + fileName;

我正在使用OpenXMLSDK创建一个简单的word文档。 到目前为止,它正在发挥作用。 现在,如何将文件系统中的图像添加到此文档?我不在乎它在文件里的什么地方,只是它在那里。 谢谢 这是我到目前为止所拥有的

 string fileName = "proposal"+dealerId +Guid.NewGuid().ToString()+".doc";
       string filePath = @"C:\DWSApplicationFiles\Word\" + fileName;
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document, true))
       {
           MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

           mainPart.Document = new Document();
           //create the body
           Body body = new Body();
           DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
           DocumentFormat.OpenXml.Wordprocessing.Run runParagraph = new DocumentFormat.OpenXml.Wordprocessing.Run();         

           DocumentFormat.OpenXml.Wordprocessing.Text text_paragraph = new DocumentFormat.OpenXml.Wordprocessing.Text("This is a test");
           runParagraph.Append(text_paragraph);
           p.Append(runParagraph);
           body.Append(p);
           mainPart.Document.Append(body);
           mainPart.Document.Save();              
       }

如何:使用开放式XML API将图像部分添加到Office开放式XML包中


这个代码对我有用:

您的代码将图像添加到docx包中,但为了在文档中看到它,您必须在document.xml中声明它,即将其链接到物理图像。这就是为什么您必须编写msdn链接中列出的长函数

我的问题是如何为图片添加效果(编辑、剪切、背景移除)。
如果您知道如何做到这一点,我将非常感谢您的帮助:)

这里有一个比上面发布的msdn页面中描述的方法更简单的方法,这段代码是用C++/CLI编写的,但是您当然可以用C编写相应的代码#


与其只是发布一个链接,不如为链接添加标题和报价。这个链接是什么?我知道微软发布了这样的代码,但这并没有添加图像。微软网站上还有另一个版本,看起来像这样,但包含了它的其余部分,而且还有很多。这不是一个有效的答复。
public static void AddImagePart(string document, string fileName)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;

        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }
    }
}
WordprocessingDocument^ doc = WordprocessingDocument::Open(doc_name, true);
FileStream^ img_fs = gcnew FileStream(image_path, FileMode::Open);
ImagePart^ image_part = doc->MainDocumentPart->AddImagePart(ImagePartType::Jpeg);
image_part->FeedData(img_fs);
Run^ img_run = doc->MainDocumentPart->Document->Body->AppendChild(gcnew Paragraph())->AppendChild(gcnew Run());
Vml::ImageData^ img_data = img_run->AppendChild(gcnew Picture())->AppendChild(gcnew Vml::Shape())->AppendChild(gcnew Vml::ImageData());
img_data->RelationshipId = doc->MainDocumentPart->GetIdOfPart(image_part);
doc->Close();