C#给Word页眉添加图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
public void AddHeader(string docPath)
 
        {
 
            var document = WordprocessingDocument.Open(docPath, true);
 
            var mainDocumentPart = document.MainDocumentPart;
 
            mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
 
            var newHeaderPart = mainDocumentPart.AddNewPart<HeaderPart>();
 
            // try this instead
 
            var imgPart = newHeaderPart.AddImagePart(ImagePartType.Jpeg, "rId999");
 
            var imagePartID = newHeaderPart.GetIdOfPart(imgPart);
 
            using(FileStream fsD =newFileStream("D:\\header.png", FileMode.Open)) { imgPart.FeedData(fsD); }
 
            var rId = mainDocumentPart.GetIdOfPart(newHeaderPart);
 
            var headerRef = new HeaderReference { Id = rId };
 
            var sectionProps = mainDocumentPart.Document.Body.Elements<SectionProperties>().LastOrDefault();
 
            if (sectionProps == null)
 
            {
 
                sectionProps = new SectionProperties();
 
                mainDocumentPart.Document.Body.Append(sectionProps);
 
            }
 
            sectionProps.RemoveAllChildren<HeaderReference>();
 
            sectionProps.Append(headerRef);
 
            newHeaderPart.Header = GeneratePicHeader(imagePartID);
 
            newHeaderPart.Header.Save();
 
        }
 
 
 
private Header GeneratePicHeader(string relationshipId)
 
        {
 
            var element =
 
                new Drawing(
 
                    new DW.Inline(
 
                        // Cx , Cy  控制图片大小,还得修改下面的
 
                        new DW.Extent() { Cx = 1990000L, Cy = 562000L },
 
                        new DW.EffectExtent()
 
                        {
 
                            LeftEdge = 0L,
 
                            TopEdge = 0L,
 
                            RightEdge = 0L,
 
                            BottomEdge = 0L
 
                        },
 
                        new DW.DocProperties()
 
                        {
 
                            Id = (UInt32Value)1U,
 
                            Name = "NIS Logo"
 
                        },
 
                        new DW.NonVisualGraphicFrameDrawingProperties(
 
                            new A.GraphicFrameLocks() { NoChangeAspect = true }),
 
                        new A.Graphic(
 
                            new A.GraphicData(
 
                                new PIC.Picture(
 
                                    new PIC.NonVisualPictureProperties(
 
                                        new PIC.NonVisualDrawingProperties()
 
                                        {
 
                                            Id = (UInt32Value)0U,
 
                                            Name = "nis.png"
 
                                        },
 
                                        new PIC.NonVisualPictureDrawingProperties()),
 
                                    new PIC.BlipFill(
 
                                        new A.Blip(
 
                                            new A.BlipExtensionList(
 
                                                new A.BlipExtension()
 
                                                {
 
                                                    Uri =
 
                                                        "{28A0092B-C50C-407E-A947-70E740481C1C}"
 
                                                })
 
                                        )
 
                                        {
 
                                            Embed = relationshipId,
 
                                            CompressionState =
 
                                                A.BlipCompressionValues.Print
 
                                        },
 
                                        new A.Stretch(
 
                                            new A.FillRectangle())),
 
                                    new PIC.ShapeProperties(
 
                                        new A.Transform2D(
 
                                            new A.Offset() { X = 0L, Y = 0L },
 
                                            new A.Extents() { Cx = 1990000L, Cy = 562000L }),
 
                                        new A.PresetGeometry(
 
                                            new A.AdjustValueList()
 
                                        )
 
                                        { Preset = A.ShapeTypeValues.Rectangle }))
 
                            )
 
                            { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
 
                    )
 
                    {
 
                        DistanceFromTop = (UInt32Value)0U,
 
                        DistanceFromBottom = (UInt32Value)0U,
 
                        DistanceFromLeft = (UInt32Value)0U,
 
                        DistanceFromRight = (UInt32Value)0U,
 
                        EditId = "50D07946"
 
                    });
 
            var header = new Header();
 
            var paragraph = new Paragraph();
 
            var run = new Run();
 
            run.Append(element);
 
            paragraph.Append(run);
 
            header.Append(paragraph);
 
            return header;
 
        }
 
作者:玥月鸟飞
链接:https://www.jianshu.com/p/42b11dc59165
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  

posted @   unicornsir  阅读(276)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示