{"id":244,"date":"2021-02-16T09:33:29","date_gmt":"2021-02-16T01:33:29","guid":{"rendered":"http:\/\/liyanliang.net\/?p=244"},"modified":"2021-02-16T09:33:30","modified_gmt":"2021-02-16T01:33:30","slug":"%e4%bd%bf%e7%94%a8-open-xml-sdk%e4%b8%ad%e7%9a%84%e7%b1%bb%e4%bb%a5%e7%bc%96%e7%a8%8b%e6%96%b9%e5%bc%8f%e5%be%80%e6%bc%94%e7%a4%ba%e6%96%87%e7%a8%bf%e4%b8%ad%e6%8c%87%e5%ae%9a%e7%9a%84%e5%b9%bb","status":"publish","type":"post","link":"http:\/\/liyanliang.net\/index.php\/2021\/02\/16\/%e4%bd%bf%e7%94%a8-open-xml-sdk%e4%b8%ad%e7%9a%84%e7%b1%bb%e4%bb%a5%e7%bc%96%e7%a8%8b%e6%96%b9%e5%bc%8f%e5%be%80%e6%bc%94%e7%a4%ba%e6%96%87%e7%a8%bf%e4%b8%ad%e6%8c%87%e5%ae%9a%e7%9a%84%e5%b9%bb\/","title":{"rendered":"\u4f7f\u7528 Open XML SDK\u4e2d\u7684\u7c7b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5f80\u6f14\u793a\u6587\u7a3f\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u56fe\u7247"},"content":{"rendered":"\n<p>\u672c\u6587\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Open XML SDK\u4e2d\u7684\u7c7b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5f80\u6f14\u793a\u6587\u7a3f\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u56fe\u7247\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">       public static void AddImage(string file, int slideIndex, string image, long OffsetX = 0, long OffsetY = 0, long ExtentsX = 1000000, long ExtentsY = 1000000)\n        {\n            using (var presentation = PresentationDocument.Open(file, true))\n            {\n                SlidePart slidePart = GetSlidePartbyIndex(presentation, slideIndex);\n\n                var part = slidePart\n                    .AddImagePart(ImagePartType.Png);\n\n                using (var stream = File.OpenRead(image))\n                {\n                    part.FeedData(stream);\n                }\n\n                var tree = slidePart\n                    .Slide\n                    .Descendants&lt;DocumentFormat.OpenXml.Presentation.ShapeTree>()\n                    .First();\n\n                var picture = new DocumentFormat.OpenXml.Presentation.Picture();\n\n                picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties();\n                picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties\n                {\n                    Name = \"My Shape\",\n                    Id = (UInt32)tree.ChildElements.Count - 1\n                });\n\n                var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties();\n                nonVisualPictureDrawingProperties.Append(new DocumentFormat.OpenXml.Drawing.PictureLocks()\n                {\n                    NoChangeAspect = true\n                });\n                picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);\n                picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties());\n\n                var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill();\n                var blip1 = new DocumentFormat.OpenXml.Drawing.Blip()\n                {\n                    Embed = slidePart.GetIdOfPart(part)\n                };\n                var blipExtensionList1 = new DocumentFormat.OpenXml.Drawing.BlipExtensionList();\n                var blipExtension1 = new DocumentFormat.OpenXml.Drawing.BlipExtension()\n                {\n                    Uri = \"{28A0092B-C50C-407E-A947-70E740481C1C}\"\n                };\n                var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi()\n                {\n                    Val = false\n                };\n                useLocalDpi1.AddNamespaceDeclaration(\"a14\", \"http:\/\/schemas.microsoft.com\/office\/drawing\/2010\/main\");\n                blipExtension1.Append(useLocalDpi1);\n                blipExtensionList1.Append(blipExtension1);\n                blip1.Append(blipExtensionList1);\n                var stretch = new DocumentFormat.OpenXml.Drawing.Stretch();\n                stretch.Append(new DocumentFormat.OpenXml.Drawing.FillRectangle());\n                blipFill.Append(blip1);\n                blipFill.Append(stretch);\n                picture.Append(blipFill);\n\n                picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties();\n                picture.ShapeProperties.Transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D();\n                picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Offset\n                {\n                    X = OffsetX,\n                    Y = OffsetY,\n                });\n                picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Extents\n                {\n                    Cx = ExtentsX,\n                    Cy = ExtentsY,\n                });\n                picture.ShapeProperties.Append(new DocumentFormat.OpenXml.Drawing.PresetGeometry\n                {\n                    Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle\n                });\n\n                tree.Append(picture);\n            }\n        }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Open XML SDK\u4e2d\u7684\u7c7b\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5f80\u6f14\u793a\u6587\u7a3f\u4e2d\u6307\u5b9a\u7684\u5e7b\u706f\u7247\u63d2\u5165\u56fe\u7247\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[30],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-c-2","tag-open-xml"],"_links":{"self":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":1,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":245,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/posts\/244\/revisions\/245"}],"wp:attachment":[{"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/liyanliang.net\/index.php\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}