2

I am new to POI API and therefore, whenever I am using the HWPFDocument class to write down text in a .doc file created by the HWPF Document, the resulting doc file is having only a single letter and not the entire text, I wrote.

So please give the answer of how can I add more text to it.

This is the code:

/**
             *  Create a POIFSFileSystem from an InputStream.
             *  Test.doc is an empty doc with no contents
             */
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("test.doc"));

            /**
             * Horrible word Document Format
             */
            HWPFDocument hwpfDocument = new HWPFDocument(fs);
            /**
             * range is used for getting the range of the document except header and footer
             */

            Range range = hwpfDocument.getRange();
            range.numParagraphs();
            /**
             * creating Paragraph
             */
            /**
             *  Inserts a paragraph into the end of this range.
             *  ParagraphProperties -> for creating Paragraph, index of the paragraph
             */
            /*Paragraph paragraph1 = range.insertBefore(new ParagraphProperties(), 0);

            paragraph1.setJustification((byte) 0); // 0-left, 1-center, 2-right, 3-left and right*/
            CharacterRun characterRun1 =  range.insertBefore("Document");
            /**
             * setting the font size
             */
            characterRun1.setFontSize(2 * 12); // takes as an argument half points
            /**
             * making Text Bold Italic
             */
            characterRun1.setBold(true);
            characterRun1.setItalic(true);
            characterRun1.setColor(111);
            characterRun1.setData(true);
            //characterRun1.replaceWith("Document");

            hwpfDocument.write(new FileOutputStream("hpwf-create-doc.doc", true));
Ankur Mukherjee
  • 3,777
  • 5
  • 32
  • 39

1 Answers1

0

If you can, switch from using HWPFDocument to XWPFDocument (generating a .docx instead of a .doc). The .docx format is much saner under the hood (it's XML based rather than binary), so POI generally does a much better job with it.

Otherwise, try using a simpler template document. The word file format can be very picky about all sorts of bits, and it's likely that you've hit a case where POI isn't aware of needing to update something in your file, and word then silently objects.

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • When I am using XWPFDocument instead of HWPFDocument, then I am able to write large text but not any kind of rich text i.e. when using the addPicture option of the XWPFDocument, the picture is definitely, present in the document but it is never getting displayed in the document so formed.How to deal with this? – Ankur Mukherjee May 30 '11 at 08:41
  • Try upgrading? Stefan has been doing quite a bit of work on XWPF pictures of late. Also, you want to add the picture to a run, not the overall document, so it shows up correctly. – Gagravarr May 30 '11 at 10:22