It may work but you have to increase the size of the Paragraph, CharacterRun and Section the text belongs to and all subsequent Paragraphs, CharacterRuns, and Sections.
-Ryan ----- Original Message ----- From: "Mitchell Ackerman (Contractor)" <[EMAIL PROTECTED]> To: "POI Users List" <[EMAIL PROTECTED]> Sent: Monday, March 08, 2004 11:30 AM Subject: RE: word doc substitutions > Ryan, my thought was to substitute the text and increase the length of the > textpiece so I could use what you had done for insert. So far all my > substitutions will be longer that the place holders in the document. Of > course if you had a delete that would work too, but in the short term do you > think this approach would work? It kind of works, but like I said the end > doc seems to be corrupted. > > Thanks, Mitchell > > -----Original Message----- > From: Ryan Ackley [mailto:[EMAIL PROTECTED] > Sent: Friday, March 05, 2004 4:44 PM > To: POI Users List > Subject: Re: word doc substitutions > > Sorry haven't written a way to delete stuff only insert. I will see what I > can do over the weekend. > > -Ryan > > ----- Original Message ----- > From: "Mitchell Ackerman (Contractor)" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Friday, March 05, 2004 6:12 PM > Subject: word doc substitutions > > > > hi, i am trying to substitute some text in a work doc. i wrote a little > > program to look for keywords and substitute some text of different > lengths. > > i couldn't see a way to do this, so i modified the TextPieceTable class as > > follows (sorry about the formatting, its outlook): > > > > public int adjustForInsert(TextPiece atp, int length) > > { > > int size = _textPieces.size(); > > length = length * (atp.usesUnicode() ? 2 : 1); > > atp.setEnd(atp.getEnd() + length); > > boolean adjusting = false; > > for (int x = 0; x < size; x++) > > { > > TextPiece tp = (TextPiece)_textPieces.get(x); > > if (adjusting) { > > tp.setStart(tp.getStart() + length); > > tp.setEnd(tp.getEnd() + length); > > } > > adjusting = adjusting || tp.equals(atp); > > } > > return length; > > } > > > > here is a snipped of the program that uses POI > > > > HWPFDocument doc = new HWPFDocument(new FileInputStream(filename)); > > for (Iterator iter = doc.getTextTable().getTextPieces().iterator(); > > iter.hasNext();) { > > TextPiece tp = (TextPiece) iter.next(); > > System.out.println(tp.getStringBuffer()); > > StringBuffer sb = tp.getStringBuffer(); > > int substart = sb.toString().indexOf(COLUMN); > > int start = substart; > > int totlen = 0; > > while (substart >= 0) { > > String text = "substitute " + start; > > int end = sb.toString().substring(start).indexOf(")") + > > start; > > System.out.println(sb.substring(start, end + 1) + " => "+ > > text); > > sb.replace(start, end + 1, text); > > int diff = text.length() - (end - start) - 1; > > int adjustedLength = doc.getTextTable().adjustForInsert(tp, > > diff); > > //doc.getCharacterTable().adjustForInsert(start, > > adjustedLength); > > //doc.getParagraphTable().adjustForInsert(start, > > adjustedLength); > > //doc.getSectionTable().adjustForInsert(textStart, > > adjustedLength); > > totlen = totlen + adjustedLength; > > substart = sb.toString().substring(end).indexOf(COLUMN); > > start = end + substart; > > } > > doc.getParagraphTable().adjustForInsert(0, totlen); > > } > > OutputStream out = new FileOutputStream("out_" + filename); > > doc.write(out); > > > > this kind of works, but the resultant doc, although readable, is > corrupted. > > any ideas how to do this better? > > > > thanks, Mitchell > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
