Apache POI Footer issue in MS word - apache-poi

I am using poi to generate docx file.file is getting opened in open office correctly.but in ms word i am getting error "A text/xml declaration may occur only at very beginning of the input in /word/footer1.xml". When i extract my generated docx file i found my /word/footer1.xml with the following code
<?xml version="1.0" encoding="UTF-8"?>
<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:r>
<w:rPr>
<w:color w:val="D3D3D3"/>
</w:rPr>
<w:t>
Section 1Page1
</w:t>
</w:r>
</w:p>
</w:ftr>
<?xml version="1.0" encoding="UTF-8"?>
<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:r>
<w:rPr>
<w:color w:val="D3D3D3"/>
</w:rPr>
<w:t>
Section 1Page1
</w:t>
</w:r>
</w:p>
</w:ftr>
What i got is that xml code is generated twice that's why Ms word is giving that error.What i am not getting what causing it to be generated twice or how can i handle this.My Poi code for footer is as follows
CTP ctp = CTP.Factory.newInstance();
CTR ctr = ctp.addNewR();
CTRPr rpr = ctr.addNewRPr();
rpr.setColor( CTColor.Factory.newInstance() );
style = new WordStyle();
style.setColor( "D3D3D3" );
ctr.getRPr().getColor().setVal( style.getColor() );
CTText text = ctr.addNewT();
text.setStringValue( parentLevelTitle+" Page 1" );
XWPFParagraph codePara = new XWPFParagraph( ctp, document );
XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
newparagraphs[0] = codePara;
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageMar ctPageMar = sectPr.addNewPgMar();
ctPageMar.setLeft( BigInteger.valueOf( 720L ) );
ctPageMar.setTop( BigInteger.valueOf( 1440L ) );
ctPageMar.setRight( BigInteger.valueOf( 720L ) );
ctPageMar.setBottom( BigInteger.valueOf( 1440L ) );
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy( document, sectPr );
headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
if i removed createFooter statement from code It gets opened successfully in ms-word.
Can anybody suggest how to resolve this ?

Turns out this is was a bug when creating a header/footer completely from scratch. I was having this same problem, up to and including POI 3.9. It is now working correctly with with 3.10FINAL, my code is nearly identical to yours so I'm assuming you will be all set.

Related

How to Convert drawn shapes of HWPFDocument to XSL FO?

I am trying to convert .doc file to PDF,
For this I am initially trying to convert .doc > XSL-FO > PDF.
On Converting the .doc to XSL-FO I am unable to convert the drawn objects such as checkbox,rectangle,square to XSL-FO.
It gets converted as below , which should actually be a box
The conversion code I am using is
HWPFDocumentCore wordDocument = WordToFoUtils.loadDoc(is);
WordToFoConverter wordToFoConverter = new WordToFoConverter(
XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument());
wordToFoConverter.processDocument(wordDocument);
File foFile = new File("D:\\Testing\\testing\\" + "test.fo");
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(out);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new
DOMSource(wordToFoConverter.getDocument()), streamResult);
String result =
org.apache.commons.lang3.StringUtils.normalizeSpace(java.text.Normalizer.normalize(new
String(out.toByteArray(), "UTF-8"), java.text.Normalizer.Form.NFD));
result = URLEncoder.encode(result, "UTF-8");
Further Apache FOP is used to convert the .fo to pdf
The .doc file is as below
and the WordToFoConverter converted the boxes as below
In Plain Text like XML, check boxes usually come from basic symbol fonts.
They are seen / shown as ☐ when unchecked, or ☑ or ☒ when checked.
In any basic text stream it should be relatively easy to use or find and replace them. However beware the encoding especially UTF , thus best copied from a clean set of Zapf Dingbats or Adobe TTF Symbol font.
many have a Unicode description but do test visually that they work after copy and paste from the PDF since the font mapping may not always tally.
8999 ⌧ ⌧ \002327 0x2327 X in a rectangle box
By far the simplest way to use UniCode text is as Rich Text which you can on Windows Command Line (you don't need the lower left dialogue, its just to illustrate export settings) outPort as Port-AbleDocFile using Write.exe which can read TXT and /PrintTo PDF.
Its much simpler than XML where just one character requires:-
<w:rPr>
<w:rFonts w:ascii="Segoe UI Symbol" w:hAnsi="Segoe UI Symbol" w:cs="Segoe UI Symbol" w:eastAsia="Segoe UI Symbol"/>
<w:color w:val="auto"/>
<w:spacing w:val="0"/>
<w:position w:val="0"/>
<w:sz w:val="48"/>
<w:shd w:fill="auto" w:val="clear"/>
<w:vertAlign w:val="subscript"/>
</w:rPr>
<w:t xml:space="preserve">☑</w:t>

Adding sticky note to existing PDF. losing original PDF content [duplicate]

I want to add Annotations comment in existing PDF file using iTextSharp with C#.
Please give sample code to add Annotations in existing PDF file.
Here PS Script for my Annotation:
[/Contents (My Text contents) /Rect [100 600 150 550] /SrcPg 1 /Title (My Title text) /Color [0 0 1] /Subtype /Caret /ANN pdfmark
The iText(Sharp) example TimetableAnnotations1.java / TimetableAnnotations1.cs from chapter 7 of iText in Action — 2nd Edition shows how to add annotations to existing PDFs in general.
The central code is (in the C# example):
rect = GetPosition(screening);
annotation = PdfAnnotation.CreateText(
stamper.Writer, rect, movie.MovieTitle,
string.Format(INFO, movie.Year, movie.Duration),
false, "Help"
);
annotation.Color = WebColors.GetRGBColor(
"#" + movie.entry.category.color
);
stamper.AddAnnotation(annotation, page);
where stamper is a PdfStamper working on your PDF file; movie is a data structure the example retrieves title, text and color of the annotation from.
PdfAnnotation offers multiple other Create... methods to create other types of annotations.
rect = GetPosition(screening);
can someone plz explain why is this is used..is there any way to find the current cursor position (top,bottom,height,width)
as with the annotation,
Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"C:\Users\Asus\Desktop\Test.pdf", FileMode.OpenOrCreate));
doc.AddDocListener(writer);
doc.Open();
doc.Add(new Annotation("annotation", "The text displayed in the sticky note", 100f, 500f, 200f, 600f));
doc.Close();
this works fine to me..

How to generate different headers in first page and other pages in poi word XWPF?

I want to generate different headers in first page and other pages in poi word, So I used XWPFHeaderFooterPolicy.FIRST and XWPFHeaderFooterPolicy.DEFAULT. when I using XWPFHeaderFooterPolicy.DEFAULT I can insert my header successfully, but when I change to XWPFHeaderFooterPolicy.FIRST, I cannot see there is a header in my first page, this is my code in below, what's wrong with it? thanks!
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("header");
There is nothing in that class to tell Word to display different first page headers. You will need the most recent version of POI, and then create the header from XWPFDocument using:
XWPFDocument.createHeader(HeaderFooterType.FIRST);
Otherwise you need to break into the CT classes and in the section properties set the titlePg property. I don't recommend this approach though as it is likely to change.
That there is a different header set for first page only, means not that this header will also be shown. In Word GUI there is a checkbox [x] Different First Page in Header & Footer Tools to achieve that.
And according Office Open XML Part 4 - Markup Language Reference there must a boolean XML element titlePg be set to determine that there is a title page present.
In actual final apache poi version 3.15 this XML element titlePg can only be set using underlying low level objects using doc.getDocument().getBody().getSectPr().addNewTitlePg();.
But apache poi version 3.16 Beta 2 has doc.createHeader(HeaderFooterType.FIRST); which sets titlePg flag in XML.
Complete example:
import java.io.*;
import org.apache.poi.wp.usermodel.*;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordHeaderFooterDifferent {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum.... page 1");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 2");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 3");
// create first page header
XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The first page header:");
// create default page header
header = doc.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The default page header:");
// create footer
XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
doc.write(new FileOutputStream("CreateWordHeaderFooterDifferent.docx"));
}
}
But in my opinion the setting titlePg flag in XML automatically while HeaderFooterType.FIRST is created is not correct. Since Word can toggle between [x] Different First Page and [] Different First Page, apache poi should also be able to do so. So the setting the titlePg flag should be a method in XWPFDocument.

how change text direction(not paragraph alignment) in document in apache poi word?(XWPF)

I'm trying to use Apache poi word 3.8 to create word document in persian/arabic language.
My question is: how change text direction in document? ( it means changing text direction not changing just paragraph text alignment)
In MS Word we could use Right-to-left text direction to change text direction and Align Right to set alignment. What’s equivalent of the first one in poi set property?
This is bidirectional text direction support (bidi) and is not yet implemented in apache poi per default. But the underlying object org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase supports this. So we must get this underlying object from the XWPFParagraph.
Example:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
public class CreateWordRTLParagraph {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Paragraph 1 LTR");
paragraph = doc.createParagraph();
CTP ctp = paragraph.getCTP();
CTPPr ctppr;
if ((ctppr = ctp.getPPr()) == null) ctppr = ctp.addNewPPr();
ctppr.addNewBidi().setVal(STOnOff.ON);
run = paragraph.createRun();
run.setText("السلام عليكم");
paragraph = doc.createParagraph();
run = paragraph.createRun();
run.setText("Paragraph 3 LTR");
FileOutputStream out = new FileOutputStream("WordDocument.docx");
doc.write(out);
out.close();
doc.close();
}
}

Is it possible to set accent colors for pptx4j in groovy?

I'd like to set the accent colors belonging to ST_SchemeColorVal in Groovy.
Something like:
STSchemeColorVal.ACCENT_1 = new Color(0,0,0)
Is this possible? If yes, how?
I think you want to change the a:accent1 element in your theme part (or one of your theme parts), for example:
<a:theme name="Office Theme">
<a:themeElements>
<a:clrScheme name="Custom 1">
<a:dk1>
<a:srgbClr val="2F2B20"/>
</a:dk1>
<a:lt1>
<a:srgbClr val="FFFFFF"/>
</a:lt1>
<a:dk2>
<a:srgbClr val="675E47"/>
</a:dk2>
<a:lt2>
<a:srgbClr val="DFDCB7"/>
</a:lt2>
<a:accent1>
<a:srgbClr val="D6F23C"/>
</a:accent1>
<a:accent2>
<a:srgbClr val="9CBEBD"/>
</a:accent2>
:
<a:accent6>
<a:srgbClr val="B1A089"/>
</a:accent6>
<a:hlink>
<a:srgbClr val="D25814"/>
</a:hlink>
<a:folHlink>
<a:srgbClr val="849A0A"/>
</a:folHlink>
</a:clrScheme>
You need to get the theme part, then change the value of the relevant element.
Creating the content from scratch is a different question, but in Java would be something like:
org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();
CTColorScheme colorscheme = dmlObjectFactory.createCTColorScheme();
this.getContents().getThemeElements().setClrScheme(colorscheme);
// Create object for accent1
CTColor color = dmlObjectFactory.createCTColor();
colorscheme.setAccent1(color);
// Create object for srgbClr
CTSRgbColor srgbcolor = dmlObjectFactory.createCTSRgbColor();
color.setSrgbClr(srgbcolor);
srgbcolor.setVal(your byte[])

Resources