Bug 58805

Summary: Header and footer corruption on Run text replacement
Product: POI Reporter: Thomas Schwery <thomas.schwery>
Component: HWPFAssignee: POI Developers List <dev>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: 3.13-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Input file with header and footer tests

Description Thomas Schwery 2016-01-05 14:13:26 UTC
Created attachment 33407 [details]
Input file with header and footer tests

The header and footer of doc documents can become corrupted and either crash Word or present wrong header and footers in the resulting document when runs have their text replaced.

The attached document is used as a template by the following code that will replace placeholders with a new text content :

        HWPFDocument doc = new HWPFDocument(new FileInputStream(tstFile));
        
        Range oRange = doc.getHeaderStoryRange();
        for (int i = 0; i < oRange.numCharacterRuns(); i++) {
            CharacterRun run = oRange.getCharacterRun(i);
            run.replaceText("_TEST_", "This text is longer than the initial text. It goes on and on without interruption.");
        }
        
        FileOutputStream fos = new FileOutputStream(outFile);
        doc.write(fos);
        fos.close();

The resulting file displays only part of the text in the header and the footer is copied from the header and cannot be edited when opened in Word 2013. LibreOffice 5 displays a short text in the header and copies the table in the footer with only a small snippet of text.
The _TEST_ in the main part of the document is not replaced, which is as expected.