Bug 47217 - Cannot open .doc file after adding more text into the document and table
Summary: Cannot open .doc file after adding more text into the document and table
Alias: None
Product: POI
Classification: Unclassified
Component: HWPF (show other bugs)
Version: unspecified
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2009-05-18 20:14 UTC by Lam Duong
Modified: 2016-06-18 05:52 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Lam Duong 2009-05-18 20:14:47 UTC
Dear POI Dev team,

Currently, I have to edit the .doc file based on the template but after I added more text into the paragraph inside and outside of table in the .doc file but after the file is save by java code I cannot open it in MS Word editor.

I am using the "poi-scratchpad-3.2-FINAL-20081019.jar", but I just copy the template to another file and I am able to open it fine.

Here is my code:
HWPFDocument doc = new HWPFDocument(new FileInputStream(request.getRealPath("/")+"/docs/filename.doc"));
            Range range = doc.getRange();           

            for (int x = 0; x < range.numSections(); x++) {
                Section section = range.getSection(x);
                int numberOfParagraphs = section.numParagraphs(); 
                boolean inTable = false;
                boolean rowStart = true;  
                boolean prcessinrow = false;
                Table table = null;
                CharacterProperties prop = null;           

                int pos = 0;
                for (int paragraphIndex = 0; paragraphIndex < numberOfParagraphs; paragraphIndex++) {
                    Paragraph paragraph = null;
                    try {
                        paragraph = section.getParagraph(paragraphIndex);
                    catch (Exception exception) {
                        System.out.print("Ignore paragraph exception: " + exception.toString());
                    if (paragraph != null) {
                              if (bulletinForm.getBulletinDate()!=null){
                                    prop = new CharacterProperties();           
                                      DateFormat formatMMDDYYYY = new SimpleDateFormat("MMMM dd, yyyy");
                                      String bullDate = formatMMDDYYYY.format(newDate);
                                      paragraph.insertBefore(bullDate + " Volume "+bulletinForm.getBulletinVolume()+", Number "+bulletinForm.getBulletinNumber(), prop);
                              if (bulletinForm.getBulletinDate()!=null){
                                    prop = new CharacterProperties();           
                                      DateFormat formatMMDDYYYY = new SimpleDateFormat("MMMM dd, yyyy");
                                      String bullDate = formatMMDDYYYY.format(newDate);
                                      paragraph.insertAfter(bullDate, prop);
                        if (paragraph.isInTable()) {                           
                            System.out.print("is in table **");
                            if (!inTable) {
                                inTable = true;
                                rowStart = true;
                        } else {
                            inTable = false;
                            rowStart = false;
                            table = null;
                            pos = 0;
                            System.out.print("End of Table");
                        prop = new CharacterProperties();
                        if (inTable && !paragraph.isTableRowEnd()) {
                            }else if(pos == 8){
                            }else if(pos == 9){
                                paragraph.insertAfter("$9099976.6565", prop);
                            }else if(pos == 10){
            // Write the result doc to output file
            FileOutputStream fileOut = new FileOutputStream(request.getRealPath("/")+"/docs/filename1.doc");

Please give me an advise.

Thanks and Regards,
Lam Duong
Comment 1 Sergey Vladimirov 2011-07-24 18:45:44 UTC
Lam Duong,

We did some work around POI code related to Word tables. You need to check if problem is still reproducable using POI code from SVN trunk. If it is - please provide an example of filename.doc.

Comment 2 James Velasco 2014-11-24 22:57:33 UTC
Also see this on Mac OS X, Windows 7, non-os specific
Comment 3 James Velasco 2014-11-24 23:00:24 UTC
Please disregard last comment, intended for Bug 56890
Comment 4 Dominik Stadler 2016-06-18 05:52:42 UTC
No update for years after request for testing with latest version of POI, therefore I am closing this as WORKSFORME for now, please reopen this bug with more information if this is still an issue for you.