You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by turjabarua <Tu...@lntinfotech.com> on 2007/03/01 11:45:56 UTC

Re: [2] Need to set style of a character/paragraph using HWPF

Hi Rainer,
             Actuall by saying style i wanted to say style of
character/paragraph , i.e if I open a document in ms word it shows just
beside font name like... normal, heading 1, heading 2....... etc .... When I
parse one do file following code gives  me style...... I think by seeing
this you'll understand what I want
<code>
Paragraph p = s.getParagraph(y);
StyleSheet styleSheet = doc.getStyleSheet();
          for (int z = 0; z < p.numCharacterRuns(); z++)
          {
            //character run
        	  StyleDescription paragraphStyle =
        		   styleSheet.getStyleDescription(p.getStyleIndex());
        		                String styleName = paragraphStyle.getName(); 
            CharacterRun run = p.getCharacterRun(z);
            //character run text
            CharacterProperties prop = run.cloneProperties();
            System.out.println(styleName)
</code>
this stylename I want to set ...... Please tell me is there any way out??
Thanks in advence.
                                                                                                                
Best regards, turja 





Rainer Schwarze wrote:
> 
> At 10:34 01.03.2007, turjabarua wrote:
>>Hi all,
>>        I am quite new to HWPF. I want to create a word doc where
>>characters/paragraph will be written in specific style. So i need to know
>>how can I set style while creating a word doc using HWPF. Can anybody help
>>me??
>>turja
> 
> Hi Turja,
> 
> you probably insert text and/or paragraphs using a Range instance. There
> are those insertBefore / insertAfter methods which in addition to the text
> String take a CharacterProperties instance. The CharacterProperties
> instance is the thing you need to take care of for formatting. For
> paragraphs you look for ParagraphProperties. If you need more details,
> just
> ask :-)
> 
> (My usual warning for HWPF: You may get along while staying with very
> simple documents. If you use fields, headers/footers, etc. you should
> expect that your generated files are invalid and Word crashes on them.)
> 
> Best wishes, Rainer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Need-to-set-style-of-a-character-paragraph-using-HWPF-tf3325890.html#a9247395
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by Rainer Schwarze <rs...@admadic.de>.
At 05:57 13.03.2007, turjabarua wrote:
>
>Hi Rainer,
>             Thanks for ur great help....... it almost worked.....if I get a
>complete solution I'll definitely post a message...........it seems it's 
>better being a HWPF developer rather than a user :o)..... 

Hi Turja,

"thanks for the flowers" :-) 
well - being a HWPF developer has it's downsides too (just look at the Word
file spec ...) :-)

best wishes,
Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by turjabarua <Tu...@lntinfotech.com>.
Hi Rainer,
             Thanks for ur great help....... it almost worked.....if I get a
complete solution I'll definitely post a message...........it seems it's 
better being a HWPF developer rather than a user :o)..... 
                                                                                                                 
turja 




Rainer Schwarze wrote:
> 
> At 05:18 06.03.2007, turjabarua wrote:
>>Hi Rainer,
>>             I got your point.... hwpf is really taking a toll on me
>> :)..... 
> [...]
>>in that document... And another problem i have come across is that my old
>>document has many tables and images in it and now if i say 
> 
> Hi Turja,
> 
> the exception which you got looks like a problem in the list formatting
> handling. The handling of the list formats (bulleted, numbered...) is not
> stable in HWPF. There are some cases where the software just does not read
> the correct data - partly also because the Word spec is incorrect about
> that.
> 
> Images work in many situations. However as soon as you apply specific
> layout properties, the way how the images are stored in the Word file
> changes and HWPF can't handle these cases.
> 
>>do i to need to copy images and tables explicitely? what to do for
that....
>>beacause if i take one simple document without any image or table it does
>>fine... 
> 
> First you should try to use no list formatting.
> Second you may succeed by including images which are not set to "behind
> the
> text" or things like that. And regarding tables, I believe that it should
> work unless they are nested. But it may fail with simple tables too. The
> best would be to check simple documents with images only or tables only
> and
> see how far you get.
> 
> Best wishes,
> Rainer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Need-to-set-style-of-a-character-paragraph-using-HWPF-tf3325890.html#a9448490
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by Rainer Schwarze <rs...@admadic.de>.
At 05:18 06.03.2007, turjabarua wrote:
>Hi Rainer,
>             I got your point.... hwpf is really taking a toll on me :)..... 
[...]
>in that document... And another problem i have come across is that my old
>document has many tables and images in it and now if i say 

Hi Turja,

the exception which you got looks like a problem in the list formatting
handling. The handling of the list formats (bulleted, numbered...) is not
stable in HWPF. There are some cases where the software just does not read
the correct data - partly also because the Word spec is incorrect about that.

Images work in many situations. However as soon as you apply specific
layout properties, the way how the images are stored in the Word file
changes and HWPF can't handle these cases.

>do i to need to copy images and tables explicitely? what to do for that....
>beacause if i take one simple document without any image or table it does
>fine... 

First you should try to use no list formatting.
Second you may succeed by including images which are not set to "behind the
text" or things like that. And regarding tables, I believe that it should
work unless they are nested. But it may fail with simple tables too. The
best would be to check simple documents with images only or tables only and
see how far you get.

Best wishes,
Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by turjabarua <Tu...@lntinfotech.com>.
Hi Rainer,
             I got your point.... hwpf is really taking a toll on me :)..... 
if I create a new blank word document first and then want to copy something
into it  , the same error comes until i put at least one character manually
in that document... And another problem i have come across is that my old
document has many tables and images in it and now if i say 

doc.write(OutputStream) it shows the following error

java.lang.NullPointerException
	at org.apache.poi.hwpf.model.ListLevel.toByteArray(ListLevel.java:227)
	at
org.apache.poi.hwpf.model.ListTables.writeListDataTo(ListTables.java:129)
	at org.apache.poi.hwpf.HWPFDocument.write(HWPFDocument.java:375)
	at org.apache.poi.hwpf.QuickTest.main(QuickTest.java:169)


do i to need to copy images and tables explicitely? what to do for that....
beacause if i take one simple document without any image or table it does
fine... 
                                                          best regards
                                                                        
turja
 

Rainer Schwarze wrote:
> 
> At 06:18 05.03.2007, turjabarua wrote:
>>
>>Hi Rainer,
>>             Might be the way i'm trying to generate a word document is
>>wrong. But i'm really confused how to create a blank document first and
add
>>text, paragraph or whatever I want to that document. Now I'm trying 
>>by using (default constructor)
> 
> You need to use a base document. HWPF does not yet support a creation of
> empty documents from scratch.
> 
>>HWPFDocument doc1 = new HWPFDocument ();
> 
> Doesn't work - that constructor is actually empty and does nothing :-)
> (You
> see, there are some issues which make it necessary, that HWPF is still in
> the scratchpad :-) )
> 
>>I also tried using 
>>
>>HWPFDocument doc1 = new HWPFDocument (new POIFSFileSystem());
> 
> Doesn't work either, because HWPF expects to find real "Word content" with
> that call.
> 
>>both the cases i'm gettign the same error!! ... And also i'm really
clueless
>>how to make that blank document first.
> 
> You can only take an empty document and open that. (Need not be a template
> (DOT)) If you want to make your life a bit easier, you can define the
> styles you need in that document so you can just apply them and do not
> need
> to create them.
> 
> Best wishes,
> Rainer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Need-to-set-style-of-a-character-paragraph-using-HWPF-tf3325890.html#a9325753
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by Rainer Schwarze <rs...@admadic.de>.
At 06:18 05.03.2007, turjabarua wrote:
>
>Hi Rainer,
>             Might be the way i'm trying to generate a word document is
>wrong. But i'm really confused how to create a blank document first and add
>text, paragraph or whatever I want to that document. Now I'm trying 
>by using (default constructor)

You need to use a base document. HWPF does not yet support a creation of
empty documents from scratch.

>HWPFDocument doc1 = new HWPFDocument ();

Doesn't work - that constructor is actually empty and does nothing :-) (You
see, there are some issues which make it necessary, that HWPF is still in
the scratchpad :-) )

>I also tried using 
>
>HWPFDocument doc1 = new HWPFDocument (new POIFSFileSystem());

Doesn't work either, because HWPF expects to find real "Word content" with
that call.

>both the cases i'm gettign the same error!! ... And also i'm really clueless
>how to make that blank document first.

You can only take an empty document and open that. (Need not be a template
(DOT)) If you want to make your life a bit easier, you can define the
styles you need in that document so you can just apply them and do not need
to create them.

Best wishes,
Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by turjabarua <Tu...@lntinfotech.com>.
Hi Rainer,
             Might be the way i'm trying to generate a word document is
wrong. But i'm really confused how to create a blank document first and add
text, paragraph or whatever I want to that document. Now I'm trying 
by using (default constructor)

HWPFDocument doc1 = new HWPFDocument ();
   
I also tried using 

HWPFDocument doc1 = new HWPFDocument (new POIFSFileSystem());

both the cases i'm gettign the same error!! ... And also i'm really clueless
how to make that blank document first.
Thanks in advance.
                           Turja




Rainer Schwarze wrote:
> 
> Hi Turja,
> 
> At 12:16 02.03.2007, turjabarua wrote:
>>Hi Rainer,
>>             Thanks my problem is almost solved now. It is workign with
>>existing document edit. 
> 
> I'm glad that I could help :-)
> 
>>But now the problem is whenever I want to create a
>>new document and add a paragraph to that, it is throwing  
>>
>>java.io.FileNotFoundException: no such entry: "DocumentSummaryInformation"
>>Error getting property set with name SummaryInformation n
>>
>>I read some previous threads and in one u have put a solution to add
>> writeProperties(pfs);
>>this line in write function of hwpfdocument....... but it is not
>>working..... 
> 
> First question: How is it not working?
> 
> Second question: How do you create a new document?
> 
>>and add to that 
>>if I want to copy one style from one document to the new one is there any
>>way out??
> 
> You may try to take a StyleDescription from the source document, clone it
> by creating a byte[] via calling toByteArray and creating a new
> StyleDescription instance and add that to the destination document. (At
> least that's the clone which seems to be implemented very quickly)
> 
> <code>
> // in StyleDescription:
> public StyleDescription createCopy() {
>   byte [] data = this.toByteArray();
>   StyleDescription sd = new StyleDescription(data, this._baseLength, 0,
> true);
> }
> 
> // in StyleSheet add an addStyleDescription function ...
> // (probably should return the index at which the new StyleDescription is
> stored)
> 
> // in your main code
> StyleDescription sd = srcDoc.getStyleSheet().getStyleDescription(idx);
> dstDoc.getStyleSheet().addStyleDescription(sd.createCopy());
> </code>
> 
> You should read the Word spec and understand the _baseLength of the
> StyleSheet/StyleDescription. Then test to see what this does in your Word
> files. (The _baseLength could be different between two Word files and I
> don't know immediately under which circumstances this could happen and
> what
> would be the best solution for this case. As with every detail of the Word
> format, one needs to check whether the spec is correct and matches your
> actual files.)
> 
> Best wishes,
> Rainer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Need-to-set-style-of-a-character-paragraph-using-HWPF-tf3325890.html#a9304884
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by Rainer Schwarze <rs...@admadic.de>.
Hi Turja,

At 12:16 02.03.2007, turjabarua wrote:
>Hi Rainer,
>             Thanks my problem is almost solved now. It is workign with
>existing document edit. 

I'm glad that I could help :-)

>But now the problem is whenever I want to create a
>new document and add a paragraph to that, it is throwing  
>
>java.io.FileNotFoundException: no such entry: "DocumentSummaryInformation"
>Error getting property set with name SummaryInformation n
>
>I read some previous threads and in one u have put a solution to add
> writeProperties(pfs);
>this line in write function of hwpfdocument....... but it is not
>working..... 

First question: How is it not working?

Second question: How do you create a new document?

>and add to that 
>if I want to copy one style from one document to the new one is there any
>way out??

You may try to take a StyleDescription from the source document, clone it
by creating a byte[] via calling toByteArray and creating a new
StyleDescription instance and add that to the destination document. (At
least that's the clone which seems to be implemented very quickly)

<code>
// in StyleDescription:
public StyleDescription createCopy() {
  byte [] data = this.toByteArray();
  StyleDescription sd = new StyleDescription(data, this._baseLength, 0, true);
}

// in StyleSheet add an addStyleDescription function ...
// (probably should return the index at which the new StyleDescription is
stored)

// in your main code
StyleDescription sd = srcDoc.getStyleSheet().getStyleDescription(idx);
dstDoc.getStyleSheet().addStyleDescription(sd.createCopy());
</code>

You should read the Word spec and understand the _baseLength of the
StyleSheet/StyleDescription. Then test to see what this does in your Word
files. (The _baseLength could be different between two Word files and I
don't know immediately under which circumstances this could happen and what
would be the best solution for this case. As with every detail of the Word
format, one needs to check whether the spec is correct and matches your
actual files.)

Best wishes,
Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by turjabarua <Tu...@lntinfotech.com>.
Hi Rainer,
             Thanks my problem is almost solved now. It is workign with
existing document edit. But now the problem is whenever I want to create a
new document and add a paragraph to that, it is throwing  

java.io.FileNotFoundException: no such entry: "DocumentSummaryInformation"
Error getting property set with name SummaryInformation n

I read some previous threads and in one u have put a solution to add
 writeProperties(pfs);
this line in write function of hwpfdocument....... but it is not
working..... 

and add to that 
if I want to copy one style from one document to the new one is there any
way out??
Thank you for your great help...
                                            turja 



Rainer Schwarze wrote:
> 
> At 11:45 01.03.2007, turjabarua wrote:
>>Hi Rainer,
>>             Actuall by saying style i wanted to say style of
>>character/paragraph , i.e if I open a document in ms word it shows just
>>beside font name like... normal, heading 1, heading 2....... etc .... When
I
>>parse one do file following code gives  me style...... I think by seeing
>>this you'll understand what I want
> 
> Hi Turja,
> 
> ok. now I see :-)
> Maybe you want the ParagraphProperties.setIstd method. (Its actually
> defined in the super class PAPAbstractType - in case you're looking for
> it).
> 
> As fas as I remember, the istd value is an index into the stylesheet
> entries. I believe that the first 10 entries are predefined styles and
> beginning with index 10, one gets the custom styles.
> 
> If you want to set an existing stylesheet you should check the "contents"
> of the StyleSheet instance of the document. If you need to add a new
> style,
> you should check the Word file spec for the details of style handling.
> 
>>this stylename I want to set ...... 
> 
> (Just to be sure: If you want to set the name, you need to adjust the
> library and add a setter function to the StyleDescription class. But I'm
> not sure right now, whether this is all you need to do or if it requires
> more work. BTW: Most of the things which I did in HWPF ended up as
> "required more work"... :-) )
> 
> Best wishes, Rainer
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Need-to-set-style-of-a-character-paragraph-using-HWPF-tf3325890.html#a9267795
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: [2] Need to set style of a character/paragraph using HWPF

Posted by Rainer Schwarze <rs...@admadic.de>.
At 11:45 01.03.2007, turjabarua wrote:
>Hi Rainer,
>             Actuall by saying style i wanted to say style of
>character/paragraph , i.e if I open a document in ms word it shows just
>beside font name like... normal, heading 1, heading 2....... etc .... When I
>parse one do file following code gives  me style...... I think by seeing
>this you'll understand what I want

Hi Turja,

ok. now I see :-)
Maybe you want the ParagraphProperties.setIstd method. (Its actually
defined in the super class PAPAbstractType - in case you're looking for it).

As fas as I remember, the istd value is an index into the stylesheet
entries. I believe that the first 10 entries are predefined styles and
beginning with index 10, one gets the custom styles.

If you want to set an existing stylesheet you should check the "contents"
of the StyleSheet instance of the document. If you need to add a new style,
you should check the Word file spec for the details of style handling.

>this stylename I want to set ...... 

(Just to be sure: If you want to set the name, you need to adjust the
library and add a setter function to the StyleDescription class. But I'm
not sure right now, whether this is all you need to do or if it requires
more work. BTW: Most of the things which I did in HWPF ended up as
"required more work"... :-) )

Best wishes, Rainer


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/