Quantcast
Viewing all articles
Browse latest Browse all 1621

Created Unassigned: Serialization behavior for empty attributes in XWPF and associated problems [13661]

Hello. As of 2.2.0 version there is a bug, that results in unreadable docx files.

Tested on Office 2013

Steps to reproduce:
1) Add an image to the document. For example:
```
using (FileStream image = new FileStream(image_path, FileMode.Open, FileAccess.Read))
{
var p = doc.CreateParagraph();
p.IndentationLeft = UnitConverter.MetricToTwip(-2, MetricUnit.Centimeter);
p.CreateRun()
.AddPicture(image, (int)NPOI.SS.UserModel.PictureType.EMF, image_path, width, height);
}
```

Reason: required attribute "id" is missing from docpr element of image section of document.xml. The reason for that is id of first picture in the document being zero, and in XmlHelper class there is set of methods, named WriteAttribute with bool parameter writeIfBlank, which is set false in code. Since zero value is handled as blank, therefore id attribute is not being written into document.

Interesting enough, adding pictures works in older version (2.1.3.1), which has the same XMLHelper code, but id of a first picture in the document is 1 (one). That is because in XWPFDocument constructor there is a code like this:
```
private IdentifierManager drawingIdManager = new IdentifierManager(1L, 4294967295L); // old version
private IdentifierManager drawingIdManager = new IdentifierManager(0L, 4294967295L); // new version
```

Sadly, issue with images is not the only one. Setting line spacing to zero does not work either (same reason: zero -> does not serialize). That's what i've personally encountered. Maybe there is more to it.

For now i am using a workaround: in the XmlHelper all WriteAttribute method calls are done as
```
WriteAttribute(sw, attributeName, value, true);
```
except for attributes with string-type value (turning them on can result in empty lines).

Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>