poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike McCullough <mike.mccullo...@gmail.com>
Subject Re: Create Header XWPF
Date Fri, 25 Jan 2013 16:11:09 GMT
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 <salimarslan84@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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message