【问题标题】:DocumentFormat.OpenXml Adding an Image to a word docDocumentFormat.OpenXml 将图像添加到 word doc
【发布时间】:2019-07-21 05:52:27
【问题描述】:

我正在使用 openXml SDK 创建一个简单的 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();              
       }

【问题讨论】:

    标签: c# asp.net openxml openxml-sdk


    【解决方案1】:

    这是一种比上面发布的 msdn 页面中描述的方法更简单的方法,此代码是在 C++/CLI 中,但当然您可以在 C# 中编写等效代码

    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();
    

    【讨论】:

      【解决方案2】:

      此代码对我有用:http://msdn.microsoft.com/en-us/library/bb497430.aspx

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

      我的问题是如何为图片添加效果(编辑、裁剪、背景去除)。 如果你知道怎么做,我会很感激你的帮助:)

      【讨论】:

        【解决方案3】:

        如何:使用 Open XML API 将图像部件添加到 Office Open XML 包

        http://msdn.microsoft.com/en-us/library/bb497430(v=office.12).aspx

        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);
                }
            }
        }
        

        【讨论】:

        • 与其只发布链接,不如为链接添加标题和引用。
        • 这个链接是什么?我知道 MS 发布了这样的代码,但这并没有添加图像。 MS 网站上还有另一个版本,看起来像这样,但包含了其余部分,而且内容更多。这不是一个有效的回复。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        相关资源
        最近更新 更多