6
6
namespace HtmlToOpenXml . Tests
7
7
{
8
8
using pic = DocumentFormat . OpenXml . Drawing . Pictures ;
9
+ using wp = DocumentFormat . OpenXml . Drawing . Wordprocessing ;
9
10
10
11
/// <summary>
11
12
/// Tests images.
@@ -86,9 +87,37 @@ public async Task LoadRemoteImage_BaseUri()
86
87
AssertIsImg ( elements . First ( ) ) ;
87
88
}
88
89
89
- private void AssertIsImg ( OpenXmlCompositeElement elements )
90
+ [ Test ( Description = "Image ID must be unique, amongst header, body and footer parts" ) ]
91
+ public async Task UniqueImageIdAcrossPackagingParts ( )
90
92
{
91
- var run = elements . GetFirstChild < Run > ( ) ;
93
+ using var generatedDocument = new MemoryStream ( ) ;
94
+ using ( var buffer = ResourceHelper . GetStream ( "Resources.DocWithImgHeaderFooter.docx" ) )
95
+ buffer . CopyTo ( generatedDocument ) ;
96
+
97
+ generatedDocument . Position = 0L ;
98
+ using WordprocessingDocument package = WordprocessingDocument . Open ( generatedDocument , true ) ;
99
+ MainDocumentPart mainPart = package . MainDocumentPart ;
100
+
101
+ var beforeMaxDocPropId = new [ ] {
102
+ mainPart . Document . Body ! . Descendants < wp . DocProperties > ( ) ,
103
+ mainPart . HeaderParts . SelectMany ( x => x . Header . Descendants < wp . DocProperties > ( ) ) ,
104
+ mainPart . FooterParts . SelectMany ( x => x . Footer . Descendants < wp . DocProperties > ( ) )
105
+ } . SelectMany ( x => x ) . MaxBy ( x => x . Id ? . Value ?? 0 ) . Id . Value ;
106
+
107
+ HtmlConverter converter = new ( mainPart ) ;
108
+ await converter . ParseHtml ( "<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' width='42' height='42'>" ) ;
109
+ mainPart . Document . Save ( ) ;
110
+
111
+ var img = mainPart . Document . Body ! . Descendants < Drawing > ( ) . FirstOrDefault ( ) ;
112
+ Assert . That ( img , Is . Not . Null ) ;
113
+ Assert . That ( img . Inline . DocProperties . Id . Value ,
114
+ Is . GreaterThan ( beforeMaxDocPropId ) ,
115
+ "New image id is incremented considering existing images in header, body and footer" ) ;
116
+ }
117
+
118
+ private Drawing AssertIsImg ( OpenXmlCompositeElement element )
119
+ {
120
+ var run = element . GetFirstChild < Run > ( ) ;
92
121
Assert . That ( run , Is . Not . Null ) ;
93
122
var img = run . GetFirstChild < Drawing > ( ) ;
94
123
Assert . That ( img , Is . Not . Null ) ;
@@ -99,6 +128,7 @@ private void AssertIsImg (OpenXmlCompositeElement elements)
99
128
var imagePartId = pic . BlipFill . Blip . Embed . Value ;
100
129
var part = mainPart . GetPartById ( imagePartId ) ;
101
130
Assert . That ( part , Is . TypeOf ( typeof ( ImagePart ) ) ) ;
131
+ return img ;
102
132
}
103
133
}
104
134
}
0 commit comments