Bug 47217

Summary: Cannot open .doc file after adding more text into the document and table
Product: POI Reporter: Lam Duong <lam.duong>
Component: HWPFAssignee: POI Developers List <dev>
Status: RESOLVED WORKSFORME    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

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(paragraphIndex==7){
                              if (bulletinForm.getBulletinDate()!=null){
                                    prop = new CharacterProperties();           
                                      prop.setBold(true);                                   
                                      DateFormat formatMMDDYYYY = new SimpleDateFormat("MMMM dd, yyyy");
                                      String bullDate = formatMMDDYYYY.format(newDate);
                                      paragraph.insertBefore(bullDate + " Volume "+bulletinForm.getBulletinVolume()+", Number "+bulletinForm.getBulletinNumber(), prop);
                              }
                        }
                       
                        if(paragraphIndex==19){
                              if (bulletinForm.getBulletinDate()!=null){
                                    prop = new CharacterProperties();           
                                      prop.setBold(true);
                                      prop.setFontSize(20);
                                      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 **");
                            pos++;
                            if (!inTable) {
                                inTable = true;
                                rowStart = true;
                            }
                        } else {
                            inTable = false;
                            rowStart = false;
                            table = null;
                            pos = 0;
                            System.out.print("End of Table");
                        }
                        prop = new CharacterProperties();
                        prop.setBold(true);
                        if (inTable && !paragraph.isTableRowEnd()) {
                            if(pos==7){
                                paragraph.insertAfter("$1245,458",prop);
                            }else if(pos == 8){
                                paragraph.insertAfter("$7657576.6565",prop);
                            }else if(pos == 9){
                                paragraph.insertAfter("$9099976.6565", prop);
                            }else if(pos == 10){
                                paragraph.insertAfter("10");
                            }
                        }
                    }   
                }
            }
            // Write the result doc to output file
            FileOutputStream fileOut = new FileOutputStream(request.getRealPath("/")+"/docs/filename1.doc");
            doc.write(fileOut);
            fileOut.flush();
            fileOut.close();

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.

--
Sergey.
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.