poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ryan Ackley" <sack...@cfl.rr.com>
Subject Re: word doc substitutions
Date Fri, 05 Mar 2004 23:43:57 GMT
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)" <MAckerman@ezenia.com>
To: <poi-user@jakarta.apache.org>
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: poi-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: poi-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org


Mime
View raw message