You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by sars84 <sa...@googlemail.com> on 2013/01/24 15:32:32 UTC

Create Header XWPF

Hello everyone,

I need help to create a header in docx file. I tried many exaples, which i
found, but unfortunately no success.
What i need is; two header ; one on the Rightside of the Head and one on the
Leftside of the Head. 

look like;

  ------------------------------
|header1                     header2|
|                                          |
|                                          |
|                                          |


I tried recently;

XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(Inputdocx);
XWPFHeader header = policy.getDefaultHeader();
....

but here is no success!
I donot found any more Examples to undestand, how it work to create headers
in .docx.

Please help! I am thankful for any Idea.
Thanks in advance.




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: How can the actual XSLFTableRow height be determined?

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
....but I should have gone on to say that you may be able to calculate what
the height will be. Get the column width. Get the text that will be placed
into is along with information about the font face and size. Next, use the
support available under Java's core api to work out how that text would have
to be formatted - how many rows it should occupy - to fit into the column.
>From that you know how high the row should be to accommodate that many lines
of text.

It is likely that there will be some minor discrepancy between the value you
calculate and the one the application would, but the two should be very
close.



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712293.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: How can the actual XSLFTableRow height be determined?

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
I am going to hazard a guess - and educated one - and say that you will not
be able to do this using the api. The reason is that to determine the
height, the document must be rendered for display - the height information
will not/cannot be stored within the file as it depends upon the default
font - for example - selected within the application where it is displayed.



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712291.html
Sent from the POI - User mailing list archive at Nabble.com.

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


How can the actual XSLFTableRow height be determined?

Posted by Matt Vuturo <ma...@yahoo.com>.

Hello,

How can I determine the height of a XSLFTableRow that has expanded due to its content? 

I have tried the the XSLFTableRow.getHeight() method, but it appears to return the minimum height for the row, and not its actual height.  For instance, if the row height has been set to 30, and a cell in the row is populated with a lot of text the entire row expands down to display the text. The rows' actual height could be 45, but row.getHeight() will still returns 30. 


Thank you in advance for your help.
Matt

Re: Create Header XWPF

Posted by renji <je...@gmail.com>.
I followed the instructions in this topic - it works great, but i have
another problem. 

I tried to add a picture to header paragraph but it doesnt work (red cross)
- it works fine if i add it to the body paragraph:

XWPFRun runHeaderImage = paragraphHeader.createRun();
			runHeaderImage.addPicture(fis, XWPFDocument.PICTURE_TYPE_JPEG, "1.jpg",
imageWidth,imageHeight);

Has anybody tried to add an image to the header?

please help,

best regards, 

renji




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712140.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by sars84 <sa...@googlemail.com>.
Hi Mike,

I thank you so much for your support and for your help . It works now great! 

Additional to this code!

By getting the defaultHeader is better to use just XWPFHeader!
XWPFHeaderFooterPolicy returned always null! I done it also with;

--
XWPFHeader defaultHeader =doc.getHeaderList().get(0);
List<XWPFParagraph> listParagraph = defaultHeader.getListParagraph();
...
---

Best Regards
sars




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712005.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
You could remove all paragraphs/runs similar to how I added them in the
example code from previous emails.

On Fri, Mar 8, 2013 at 8:41 AM, Paul C. <wr...@yahoo.com> wrote:

> Guys,
> Does anyone know how to remove the footer/header from a word doc?
> I was hoping there would be something like:
> XWPFDocument document.......
> document.getHeaderFooterPolicy().removeFooter().
> However, the HeaderFooterPolicy does not seem to have any method for
> something like that.
> Anyone aware of some other way of achieving this?
> Thanks in advance.
> Paul
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712281.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by "Paul C." <wr...@yahoo.com>.
Guys,
Does anyone know how to remove the footer/header from a word doc?
I was hoping there would be something like:
XWPFDocument document.......
document.getHeaderFooterPolicy().removeFooter(). 
However, the HeaderFooterPolicy does not seem to have any method for
something like that.
Anyone aware of some other way of achieving this?
Thanks in advance.
Paul



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712281.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by "Paul C." <wr...@yahoo.com>.
According to the faq explanation, /"The full ooxml-schemas jar is distributed
with Apache POI, along with the cut-down poi-ooxml-schemas jar containing
just the common parts."/ So the problem of failed compilation was because of
me using poi-ooxml-schemas.jar instead of ooxml-schemas.jar. 
Thank you, Nick and Mike for your generous help.
Paul



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712249.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Nick Burch <ap...@gagravarr.org>.
On Tue, 5 Mar 2013, Mike McCullough wrote:
> I'm fairly certain the poi-ooxml-schema.jar file is auto-generated by the
> xml schemas that define the OOXML format.

Correct

> I have been unsuccessful in finding the source code.

The FAQ explains it more, and also covers how to get the sources if you 
really need them: http://poi.apache.org/faq.html#faq-N10109

Nick

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
I'm fairly certain the poi-ooxml-schema.jar file is auto-generated by the
xml schemas that define the OOXML format. I have been unsuccessful in
finding the source code. Instead, I've only relied on the ECMA
specifications (
http://www.ecma-international.org/publications/standards/Ecma-376.htm) for
the Word format and then by trial and error, determining the structure of
how the document should be formatted.

Mike

On Tue, Mar 5, 2013 at 12:27 PM, Paul C. <wr...@yahoo.com> wrote:

> Indeed, there are a lot of advantage of using Maven. However, I am not in
> the
> position of making that decision.
> I peeked into the poi-ooxml-schema.jar version 3.9 jar, I could not find
> STPTabRelativeTo.class, STPTabAlignment.class, etc. However, I see the
> inner
> classes: such as STPTabRelativeTo$Enum.class, STPTabAlignment$Enum.class,
> etc. In version 1.1, both outter class and inner classes exist. How come?
>
> Also, I don't seem to be able to find the source code for
> poi-ooxml-schema.jar. Does anyone know where to get it?
> Thanks.
> Paul
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712246.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by "Paul C." <wr...@yahoo.com>.
Indeed, there are a lot of advantage of using Maven. However, I am not in the
position of making that decision. 
I peeked into the poi-ooxml-schema.jar version 3.9 jar, I could not find
STPTabRelativeTo.class, STPTabAlignment.class, etc. However, I see the inner
classes: such as STPTabRelativeTo$Enum.class, STPTabAlignment$Enum.class,
etc. In version 1.1, both outter class and inner classes exist. How come?

Also, I don't seem to be able to find the source code for
poi-ooxml-schema.jar. Does anyone know where to get it?
Thanks.
Paul



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712246.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
Since it sounds like you're working as part of a team, I would highly
recommend that you use Maven for your project files. I cannot stress its
importance in a team environment more (or some other dependency management
tool).

Mike

On Tue, Mar 5, 2013 at 12:04 PM, Paul C. <wr...@yahoo.com> wrote:

> Thanks for the prompt reply, Mike.
> We are restricted to use Eclipse (Ganymede) as IDE. On my class path, I
> have
> the following:
> poi-3.9-20121203.jar
> poi-ooxml-3.9-20121203.jar
> poi-ooxml-schemas-3.9-20121203.jar
> xbean.jar (version 2.6)
>
> The major difference of mine setting and yours is that you are using
> poi-ooxml-schemas.jar version 1.1. So I went on line and downloaded. Sure
> enough, with the older version of schemas jar, it compiled. Isn't that
> interesting?!
>
> Paul
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712244.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by "Paul C." <wr...@yahoo.com>.
Thanks for the prompt reply, Mike.
We are restricted to use Eclipse (Ganymede) as IDE. On my class path, I have
the following:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xbean.jar (version 2.6)

The major difference of mine setting and yours is that you are using
poi-ooxml-schemas.jar version 1.1. So I went on line and downloaded. Sure
enough, with the older version of schemas jar, it compiled. Isn't that
interesting?!

Paul



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712244.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
The dependencies that I used in my maven pom.xml files include:

<dependency>
<groupId>org.apache.poi</groupId>
 <artifactId>poi-ooxml</artifactId>
<version>3.9</version>
 </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
 <artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
 </dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
 <artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
 </dependency>

So you can either update your classpath to include the above jar files, or,
what I would recommend is that you use maven to handle all your dependency
management.

As a side note, if you have maven as your project file, you can easily move
towards any other java IDE that supports it as well *cough* IntelliJ IDEA
*cough*.

Mike

On Tue, Mar 5, 2013 at 10:52 AM, Paul C. <wr...@yahoo.com> wrote:

> Hi Mike,
> Thanks for the post. When I was trying out your sample code, I have
> difficult compiling the code. My eclipse project complaining that
> STPTabRelativeTo, STHorizontalAlignment and STPTabLeader cannot be
> resolved.
> I do have poi-ooxml-schemas-3.7-20101029.jar on my class path. Sorry I am
> new to POI. Is there something that I might be missing?
> Thanks,
> Paul
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712242.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by "Paul C." <wr...@yahoo.com>.
Hi Mike,
Thanks for the post. When I was trying out your sample code, I have
difficult compiling the code. My eclipse project complaining that
STPTabRelativeTo, STHorizontalAlignment and STPTabLeader cannot be resolved.
I do have poi-ooxml-schemas-3.7-20101029.jar on my class path. Sorry I am
new to POI. Is there something that I might be missing?
Thanks,
Paul



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5712242.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
In case others were following along and for future reference, here's what I
came up with (we took further discussion offline to see what the problem
was).

--

Here's some sample code. Note that there's a big difference to what I had
to add. Adding paragraphs to the default header is just not allowed for
whatever reason, so I had to resort to using the XmlBeans objects to get
direct modification access.

The output will contain two paragraphs, each on their own separate line.

XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader();

List<XWPFParagraph> listParagraph = defaultHeader.getListParagraph();
for (XWPFParagraph paragraph : listParagraph) {
while (!paragraph.getRuns().isEmpty()) {
paragraph.removeRun(0);
}
}

XWPFParagraph firstParagraph = listParagraph.get(0);
firstParagraph.setAlignment(ParagraphAlignment.LEFT);

XWPFRun firstParagraphRun = firstParagraph.createRun();
firstParagraphRun.setText("Test plan");

if (listParagraph.size() < 2) {
CTHdrFtr ctHeader = defaultHeader._getHdrFtr();
CTP ctParagraph = ctHeader.addNewP();
CTPPr ctParagraphProperties = ctParagraph.addNewPPr();
CTJc ctJustification = ctParagraphProperties.addNewJc();
ctJustification.setVal(STJc.RIGHT);
CTR ctRun = ctParagraph.addNewR();
CTText ctRunText = ctRun.addNewT();
ctRunText.setStringValue("Header Right");
} else {
XWPFParagraph secondParagraph = listParagraph.get(1);
secondParagraph.setAlignment(ParagraphAlignment.RIGHT);

XWPFRun secondParagraphRun = secondParagraph.createRun();
secondParagraphRun.setText("Header Right");
}

doc.write(new FileOutputStream(new File("Output.docx")));

--

But, the problem with the output of this code will be two paragraphs, each
on separate lines--which is probably not something you desire.

In order to get all the text on one line, it was simply creating what I
desired in Word, and replicating that using POI XmlBeans objects--only
because there is no other way possible with POI itself. The following code
will use the existing default header, delete any runs that it may currently
have, and add in the proper formatting and text.

The code for that is:

XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader();

List<XWPFParagraph> listParagraph = defaultHeader.getListParagraph();

for (XWPFParagraph paragraph : listParagraph) {
while (!paragraph.getRuns().isEmpty()) {
paragraph.removeRun(0);
}
}

XWPFParagraph firstParagraph = listParagraph.get(0);
CTP ctParagraph = firstParagraph.getCTP();
CTPPr paragraphProperties = ctParagraph.addNewPPr();
CTJc ctJustification = paragraphProperties.addNewJc();
ctJustification.setVal(STJc.LEFT);

CTR leftRun = ctParagraph.addNewR();
CTText leftRunText = leftRun.addNewT();
leftRunText.setStringValue("Test plan");

/**
 * This is only to get the next run's properties set.
 */
CTR rightAlignmentRun = ctParagraph.addNewR();
CTPTab paragraphTab = rightAlignmentRun.addNewPtab();
paragraphTab.setRelativeTo(STPTabRelativeTo.MARGIN);
paragraphTab.setAlignment(STPTabAlignment.RIGHT);
paragraphTab.setLeader(STPTabLeader.NONE);

CTR rightRun = ctParagraph.addNewR();
CTText rightRunText = rightRun.addNewT();
rightRunText.setStringValue("Header Right");

doc.write(new FileOutputStream(new File("LeftAndRight.docx")));



On Fri, Jan 25, 2013 at 6:25 AM, sars84 <sa...@googlemail.com>wrote:

> Hi Mike,
>
> in your first reply, you had an Idea " to create a couple different
> paragraphs, each with different alignments".
>
> How to do it?
>
> Thanks in advance Mike.
>
> Regards sars
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711994.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by sars84 <sa...@googlemail.com>.
Hi Mike,

in your first reply, you had an Idea " to create a couple different
paragraphs, each with different alignments".

How to do it?

Thanks in advance Mike.

Regards sars



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711994.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by sars84 <sa...@googlemail.com>.
No that is not. There is the same header on the every page!



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711982.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
Make certain you created the correct header in your template document.
There are three different types defined by XWPFHeaderFooterPolicy (DEFAULT,
EVEN, and FIRST), and I'm assuming that you created a header for only the
first page, but I could be wrong.

On Thu, Jan 24, 2013 at 10:46 AM, sars84 <sa...@googlemail.com>wrote:

> It is the header! DefaultHeader is Null. But i have headers in my input
> file
>
> XWPFHeaderFooter header = headerFooterPolicy.getDefaultHeader();
>                 System.out.println(header.getText());
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711980.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by sars84 <sa...@googlemail.com>.
It is the header! DefaultHeader is Null. But i have headers in my input file 

XWPFHeaderFooter header = headerFooterPolicy.getDefaultHeader();
		System.out.println(header.getText());



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711980.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
I don't know what corresponds to your line 67. Try setting breakpoints and
figure out what's null.

On Thu, Jan 24, 2013 at 10:29 AM, sars84 <sa...@googlemail.com>wrote:

> Hi Mike,
>
> Thank you first so much for your quickly reply!
>
> Your Code seems very useful! I trying it still.
> But wenn i run this Code;
>
> ----
> XWPFDocument doc = new XWPFDocument(OPCPackage.open(new
> java.io.File(docFile)));
> XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
> XWPFHeaderFooter header = headerFooterPolicy.getDefaultHeader();
>
> XWPFParagraph paragraph = header.getListParagraph().iterator().next();
> paragraph.setAlignment(ParagraphAlignment.CENTER);
>
>                 while(!paragraph.getRuns().isEmpty()){
>                         paragraph.removeRun(0);
>                 }
>
>                 String a = "HALLO";
>
>                 XWPFRun run = paragraph.createRun();
>                 run.setText(a);
>
>
>         doc.write(new FileOutputStream("C://Test/Output.docx"));
>
>
> ----
>
> I get;
>
> Exception in thread "main" java.lang.NullPointerException
>         at packageVal.DocSubmission.readDocFile(DocSubmission.java:67)
>         at packageVal.userInterface.main(userInterface.java:45)
>
>
> Also NullPointer! Any Idea?
>
> Thanks in advance.
>
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711978.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Create Header XWPF

Posted by sars84 <sa...@googlemail.com>.
Hi Mike,

Thank you first so much for your quickly reply!

Your Code seems very useful! I trying it still.
But wenn i run this Code;

----
XWPFDocument doc = new XWPFDocument(OPCPackage.open(new
java.io.File(docFile)));
XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
XWPFHeaderFooter header = headerFooterPolicy.getDefaultHeader();
		
XWPFParagraph paragraph = header.getListParagraph().iterator().next();
paragraph.setAlignment(ParagraphAlignment.CENTER);
		
		while(!paragraph.getRuns().isEmpty()){
			paragraph.removeRun(0);
		}
		
		String a = "HALLO";

		XWPFRun run = paragraph.createRun(); 
		run.setText(a);
		
		
	doc.write(new FileOutputStream("C://Test/Output.docx"));
		

----

I get;

Exception in thread "main" java.lang.NullPointerException
	at packageVal.DocSubmission.readDocFile(DocSubmission.java:67)
	at packageVal.userInterface.main(userInterface.java:45)


Also NullPointer! Any Idea?

Thanks in advance.




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976p5711978.html
Sent from the POI - User mailing list archive at Nabble.com.

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


Re: Create Header XWPF

Posted by Mike McCullough <mi...@gmail.com>.
Unfortunately, I have not found a way to create a header from scratch.
There's been a bug report already regarding this issue:
https://issues.apache.org/bugzilla/show_bug.cgi?id=53009

However, what you can do in the meantime is to create a template docx file
that contains a simple header/footer. One you open a new XWPFDocument using
this template file, you can retrieve the existing header/footer, and modify
them as you see fit.

So to get you started, here's some sample code that will get you along the
right path.

XWPFDocument document = new XWPFDocument(new
FileInputStream("Template.docx")); // This is your created template docx
file.
XWPFHeaderFooter headerFooterPolicy = document.getHeaderFooterPolicy();
XWPFHeaderFooter header = headerFooterPolicy.getDefaultHeader(); // or
.getDefaultFooter()
XWPFParagraph paragraph =
headerFooter.getListParagraph().iterator().next(); // assumes one and only
one paragraph in your header or footer

paragraph.setAlignment(ParagraphAlignment.CENTER);
// Go through the paragraph and delete all the XWPFRun instances within
your paragraph.
while (!paragraph.getRuns().isEmpty()) {
    paragraph.removeRun(0);
}

XWPFRun run = paragraph.createRun();
run.setText("Some new text value");

--

What I imagine you'll have to do is to create a couple different
paragraphs, each with different alignments. Or, one paragraph that contains
an XWPFTable so that you can have it all on one line (no borders shown),
with the left cell being left aligned and the right cell as right aligned.



On Thu, Jan 24, 2013 at 9:32 AM, sars84 <sa...@googlemail.com>wrote:

> Hello everyone,
>
> I need help to create a header in docx file. I tried many exaples, which i
> found, but unfortunately no success.
> What i need is; two header ; one on the Rightside of the Head and one on
> the
> Leftside of the Head.
>
> look like;
>
>   ------------------------------
> |header1                     header2|
> |                                          |
> |                                          |
> |                                          |
>
>
> I tried recently;
>
> XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(Inputdocx);
> XWPFHeader header = policy.getDefaultHeader();
> ....
>
> but here is no success!
> I donot found any more Examples to undestand, how it work to create headers
> in .docx.
>
> Please help! I am thankful for any Idea.
> Thanks in advance.
>
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/Create-Header-XWPF-tp5711976.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>