繁体   English   中英

通过Open XML创建Word文档后图像不出现

[英]Images are not coming after creating word Document through Open XML

我正在使用OpenXMl sdk通过c#创建Word文档。 我将所有的html页面都转换为word文档,但是在转换时,我为图像提供了一个绝对地址,转换后的图像在我的系统中是完美显示,但是当我尝试将此文档带到其他系统时,图像不在那里。

我检查了媒体目录,所有图像都在那里,但是名称不同。

我的文档已转换,但我正在使用此方法。

            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentPath, true))
        {
            XNamespace w =
                  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
            XNamespace r =
                "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
            string altChunkId = "AltChunkId1";
            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);
            using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
            using (StreamWriter stringStream = new StreamWriter(chunkStream))
                stringStream.Write(html);
            XElement altChunk = new XElement(w + "altChunk",
                new XAttribute(r + "id", altChunkId)
            );
            XDocument mainDocumentXDoc = GetXDocument(myDoc);
            mainDocumentXDoc.Root
                .Element(w + "body")
                .Elements(w + "p")
                .Last()
                .AddAfterSelf(altChunk);
            SaveXDocument(myDoc, mainDocumentXDoc);
        }





    private static XDocument GetXDocument(WordprocessingDocument myDoc)
    {
        // Load the main document part into an XDocument
        XDocument mainDocumentXDoc;
        using (Stream str = myDoc.MainDocumentPart.GetStream())
        using (XmlReader xr = XmlReader.Create(str))
            mainDocumentXDoc = XDocument.Load(xr);
        return mainDocumentXDoc;
    }


    private static void SaveXDocument(WordprocessingDocument myDoc,
XDocument mainDocumentXDoc)
    {
        // Serialize the XDocument back into the part
        using (Stream str = myDoc.MainDocumentPart.GetStream(
            FileMode.Create, FileAccess.Write))
        using (XmlWriter xw = XmlWriter.Create(str))
            mainDocumentXDoc.Save(xw);
    }

然后将生成一个afchunk.dat文件,该文件将显示在内容和绝对路径中。

基本上,我不想通过所有编码来创建文件,我只想将.html转换为.docx文件。

所以任何人都可以告诉我如何转换而不会在html中出错。

您没有嵌入图像是有原因的吗? 这是一个带有示例代码的链接,向您展示如何。

http://msdn.microsoft.com/en-us/library/bb497430.aspx

尝试创建一个DocumentResource(Item-> Add new)并在那里关联图像。

呼叫文件

    using (Stream imgStream = ip.GetStream())
    {
        System.Drawing.Bitmap logo = DocumentResources._default;
        logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }

Drawing drawing = BuildImage(imageRelationshipID, "_default.jpg", 200, 30);

并创建在页眉或页脚中构建图像的方法;

   private static Drawing BuildImage(string imageRelationshipID, string imageName, int pixelWidth, int pixelHeight)
    {
        int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL);
        int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL);
        Drawing drawing = new Drawing();
        d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 };
        d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor();
        d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 };
        d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight };
        d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName };
        d.Graphic graphic = new d.Graphic();            
        d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI };
        d.Pictures.Picture pic = new d.Pictures.Picture();
        d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties();
        d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName };
        d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties();
        d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill();
        d.Blip blip = new d.Blip { Embed = imageRelationshipID };
        d.Stretch stretch = new d.Stretch();
        d.FillRectangle fillRect = new d.FillRectangle();
        d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties();
        d.Transform2D xfrm = new d.Transform2D();
        d.Offset off = new d.Offset { X = 0, Y = 0 };
        d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight };
        d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle };
        d.AdjustValueList avLst = new d.AdjustValueList();
        xfrm.Append(off);
        xfrm.Append(ext);
        prstGeom.Append(avLst);
        stretch.Append(fillRect);
        spPr.Append(xfrm);
        spPr.Append(prstGeom);
        blipFill.Append(blip);
        blipFill.Append(stretch);
        nvPicPr.Append(cNvPr);
        nvPicPr.Append(cNvPicPr);
        pic.Append(nvPicPr);
        pic.Append(blipFill);
        pic.Append(spPr);
        graphicData.Append(pic);
        graphic.Append(graphicData);
        inline.Append(extent);
        inline.Append(docPr);
        inline.Append(graphic);
        drawing.Append(inline);
        return drawing;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM