by mithilesh.jha » Mon Mar 14, 2022 3:16 pm
I am using docx4j to convert xhtml to docx using docx4j library . I want to set different page margins for cover page and rest of pages . I am able to set it usinf SectPr. But problem here is 1st page is coming blank with no content. I am attaching my code here. Please tell what wrong I am doing .
- Code: Select all
public class Header {
public static org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();
public static void main( String[] args ) throws Exception
{
String xhtml=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta name=\"caid\" content=\"109927\"/><meta name=\"letterLanguage\" content=\"LANG_ITA\"/><meta name=\"brandCd\" content=\"USBC\"/></head><body><div><p><span style=\"font-family: Frutiger; font-size: 12pt;\">Event: <span class=\"letterElementNonEditable\">109927</span> Area: Notifica Date: <span class=\"selectable\">11/08/2021 12:21:59</span> </span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Language: Italian Variant:DefaultVariant</span></p><p> </p>\r\n" +
"<table style=\"border-collapse: collapse; width: 99.8636%; border-color: #000000; border-style: groove;\" border=\"0\">\r\n" +
"<tbody>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 31.3817%; border-style: hidden;\">\r\n" +
"<p><span style=\"font-size: 17pt;\">Urgente</span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">HERR</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">FRITZ MUSTER</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">C/O</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">HANS MUSTER</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">ROESLISTRASSE 2</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">8006 ZUERICH</span></p>\r\n" +
"</td>\r\n" +
"<td style=\"width: 41.1%;\">\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 16pt;\"><strong>UBS SA</strong></span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Casella Postale, CH-8098 Zurigo </span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">Per informazioni:</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">ABCD-EFG</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Tel. (01) 234 11 11</span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\"><strong>DEPOSITO</strong></span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">No. di deposito 230-999999.S1</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Rubrica</span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">No. di cliente 230-999999</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Titolare FRITZ MUSTER</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\"> 8070 ZUERICH</span></p>\r\n" +
"<p> </p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">Avviso no. 200716060</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">No. di conto 230-999999.A1Z CHF</span></p>\r\n" +
"<p> </p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">Allestito il <span class=\"letterElementNonEditable\"></span> </span></p>\r\n" +
"</td>\r\n" +
"<td style=\"width: 21.7772%; border-style: hidden;\">\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">LEIT-WEG</span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 31.3817%; border-style: hidden;\">\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">Offerta pubblica di acquisto</span></p>\r\n" +
"</td>\r\n" +
"<td style=\"width: 41.1%;\">\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\"><strong> </strong></span></p>\r\n" +
"</td>\r\n" +
"<td style=\"width: 21.7772%; border-style: hidden;\">\r\n" +
"<p> </p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table><p><span class=\"letterElementNonEditable\" style=\"font-family: Frutiger; font-size: 12pt;\"></span>
<span style=\"font-family: Frutiger; font-size: 12pt;\"><span class=\"letterElementNonEditable\"></span> </span></p><table style=\"border-collapse: collapse; width: 100%; height: 394px;\" border=\"0\">\r\n" +
"<tbody>\r\n" +
"<tr style=\"height: 43px;\">\r\n" +
"<td style=\"width: 91.2871%; height: 43px;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">Risposta entro il 30luglio2020</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">(Data di ricezione degli ordini all'UBS Switzerland AG)</span></td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 22px;\">\r\n" +
"<td style=\"width: 91.2871%; height: 22px;\">\r\n" +
"<p> </p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\">Tenete in custodia per me/noi in data <span class=\"letterElementNonEditable\">24/06/2021 23:59:59</span> :</span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 329px;\">\r\n" +
"<td style=\"width: 91.2871%; height: 329px;\">\r\n" +
"<p> </p>\r\n" +
"<table style=\"border-collapse: collapse; width: 97.9472%; border-style: none;\" border=\"0\">\r\n" +
"<tbody>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 31.2431%;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">Quantità/Nominale</span></td>\r\n" +
"<td style=\"width: 31.2431%;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">SDescrizione del titolo</span></td>\r\n" +
"<td style=\"width: 31.3539%;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">No. di valore</span></td>\r\n" +
"</tr>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 31.2431%;\"><span class=\"letterElementNonEditable\" style=\"font-family: Frutiger; font-size: 12pt;\">15000</span></td>\r\n" +
"<td style=\"width: 31.2431%;\"><span class=\"letterElementNonEditable\" style=\"font-family: Frutiger; font-size: 12pt;\">LO FUNDS SOVEREIGN TREASURY 1-3(CHF) -T</span></td>\r\n" +
"<td style=\"width: 31.3539%;\"><span class=\"letterElementNonEditable\" style=\"font-family: Frutiger; font-size: 12pt;\">CH0002772660</span></td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table>\r\n" +
"<p> </p>\r\n" +
"<p> </p>\r\n" +
"<p>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Con riferimento alla vostra lettera, vogliate procedere come da seguenti istruzioni:</span></p>\r\n" +
"<p>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Conferma da parte degli azionisti:</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">----------------------------------</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">Confermo/Confermiamo di essere a conoscenza della legislazione del mio/nostro paese e</span>
<span style=\"font-family: Frutiger; font-size: 12pt;\">di essere in regola con essa. </span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table><table style=\"border-collapse: collapse; width: 100%;\" border=\"0\">\r\n" +
"<tbody>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 98.0057%;\">\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\"><span class=\"letterElementNonEditable\"></span> </span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\"><span class=\"letterElementNonEditable\"></span> </span></p>\r\n" +
"<p><span style=\"font-family: Frutiger; font-size: 12pt;\"><span class=\"letterElementNonEditable\"></span> </span></p>\r\n" +
"<p> </p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table><table style=\"border-collapse: collapse; width: 97.8601%;\" border=\"0\">\r\n" +
"<tbody>\r\n" +
"<tr>\r\n" +
"<td style=\"width: 33.6618%;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">Data:</span></td>\r\n" +
"<td style=\"width: 50.3594%;\"><span style=\"font-family: Frutiger; font-size: 12pt;\">Firma:</span></td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table>\r\n" +
"<p> </p><p>Italian</p></div>"
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "Mkii"
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "<table style=\"border-collapse: collapse; width: 99.5804%; height: 190px; border-style: hidden;\" border=\"1\">\r\n" +
"<tbody>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Subscription rights:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">For every unit held 1 right will be distributed.</span></td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Subscription ratio:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\">\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">9 subscription rights (SN 110329599) entitle the holder to buy 1 new unit UBS Foncipars (SN 1442085).</span></p>\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Only 9 subscription rights or a multiple thereof can be exercised.</span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Issue price:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">CHF 93.15 per new unit of UBS Foncipars</span></td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Ex-date subscription right:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span class=\"letterElementNonEditable\">24 February 2022</span>;</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Subscription period:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">from 14 Feburary to 7 March 2022, 12:00 a.m.</span></td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Subscription rights trading:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\">\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">from 14 Feburary to 5 March 2022</span></p>\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Subscription rights not exercised will expire on 7 March 2022, 12:00 a.m., without any value.;</span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Type of issue:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\">\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">The issue is being carried out on a commission basis (\"best-effort\") in the context of a public subscription offer in Switzerland. At the end of the subscription period, subscription rights that have not been exercised will expire as worthless. </span></p>\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">The number of new units to be issued is determined by the fund management company UBS Fund Management (Switzerland) AG, on the basis of the exercised subscription rights after expiry of the subscription period. As a result, the actual number of new units issued may fall short of the specified maximum that can be issued. </span></p>\r\n" +
"<p><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">In addition, the value of subscription rights traded on the SIX Swiss Exchange may fluctuate due to supply and demand. Their intrinsic value cannot therefore be guaranteed.</span></p>\r\n" +
"</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Payment:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\">;</td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Distribution:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">The new units carry a dividend entitlement backdated for the full current financial year as of 1st January 2022.</span></td>\r\n" +
"</tr>\r\n" +
"<tr style=\"height: 19px;\">\r\n" +
"<td style=\"width: 38.1716%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">Restrictions:</span></td>\r\n" +
"<td style=\"width: 57.92%; height: 19px;\"><span style=\"font-family: Frutiger45Light; font-size: 10.5pt;\">for US persons and unit-holders domiciled in the USA (incl. QIBs) or in a member states of the European Economic Area (except for subscriptions received from Germany, France and Liechtenstein)</span></td>\r\n" +
"</tr>\r\n" +
"</tbody>\r\n" +
"</table>"
+ "jsfdsdhfjdfjd"
+ "Mithilesh"
+ ""
+ "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
+ ""
+ "It is a long estab"
+ "]]lished fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."
+ "</body></html>"
;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart main_part = wordMLPackage.getMainDocumentPart();
// Create object for sectPr
SectPr sectpr = wmlObjectFactory.createSectPr();
wordMLPackage.getMainDocumentPart().getContents().getBody().setSectPr(sectpr);
// Create object for pgSz
SectPr.PgSz sectprpgsz = wmlObjectFactory.createSectPrPgSz();
sectpr.setPgSz(sectprpgsz);
sectprpgsz.setW( BigInteger.valueOf( 12240) );
sectprpgsz.setH( BigInteger.valueOf( 15840) );
// Create object for pgMar
SectPr.PgMar sectprpgmar = wmlObjectFactory.createSectPrPgMar();
sectpr.setPgMar(sectprpgmar);
sectprpgmar.setFooter( BigInteger.valueOf( 720) );
sectprpgmar.setLeft( BigInteger.valueOf( 1440) );
sectprpgmar.setRight( BigInteger.valueOf( 1440) );
sectprpgmar.setTop( BigInteger.valueOf( 1440) );
sectprpgmar.setBottom( BigInteger.valueOf( 1440) );
sectprpgmar.setGutter( BigInteger.valueOf( 0) );
sectprpgmar.setHeader( BigInteger.valueOf( 720) );
// Create object for cols
// Create object for p
P p = wmlObjectFactory.createP();
wordMLPackage.getMainDocumentPart().getContent().add(p);
// Create object for pPr
PPr ppr = wmlObjectFactory.createPPr();
p.setPPr(ppr);
// Create object for sectPr
SectPr sectpr2 = wmlObjectFactory.createSectPr();
ppr.setSectPr(sectpr2);
// Create object for pgSz
SectPr.PgSz sectprpgsz2 = wmlObjectFactory.createSectPrPgSz();
sectpr2.setPgSz(sectprpgsz2);
sectprpgsz2.setW( BigInteger.valueOf( 12240) );
sectprpgsz2.setH( BigInteger.valueOf( 15840) );
// Create object for pgMar
SectPr.PgMar sectprpgmar2 = wmlObjectFactory.createSectPrPgMar();
sectpr2.setPgMar(sectprpgmar2);
sectprpgmar2.setFooter( BigInteger.valueOf( 720) );
sectprpgmar2.setLeft( BigInteger.valueOf( 1440) );
sectprpgmar2.setRight( BigInteger.valueOf( 1440) );
sectprpgmar2.setTop( BigInteger.valueOf( 1440) );
sectprpgmar2.setBottom( BigInteger.valueOf( 1440) );
sectprpgmar2.setGutter( BigInteger.valueOf( 0) );
sectprpgmar2.setHeader( BigInteger.valueOf( 2304) );
HeaderPart cover_hdr_part = new HeaderPart(new PartName("/word/cover-header.xml"));
HeaderPart content_hdr_part = new HeaderPart(new PartName("/word/content-header.xml"));
Hdr cover_hdr = wmlObjectFactory.createHdr();
Hdr content_hdr = wmlObjectFactory.createHdr();
cover_hdr_part.setJaxbElement(cover_hdr);
content_hdr_part.setJaxbElement(content_hdr);
Relationship cover_hdr_rel = main_part.addTargetPart(cover_hdr_part);
Relationship content_hdr_rel = main_part.addTargetPart(content_hdr_part);
// Create object for headerReference
HeaderReference headerreference = wmlObjectFactory.createHeaderReference();
sectpr2.getEGHdrFtrReferences().add( headerreference);
headerreference.setType(org.docx4j.wml.HdrFtrRef.DEFAULT);
headerreference.setId( content_hdr_rel.getId());
// Create object for headerReference
HeaderReference headerreference2 = wmlObjectFactory.createHeaderReference();
sectpr2.getEGHdrFtrReferences().add( headerreference2);
headerreference2.setType(org.docx4j.wml.HdrFtrRef.FIRST);
headerreference2.setId( cover_hdr_rel.getId());
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
XHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert( xhtml, null) );
File fileName = new File("C:\\Users\\mithilesh.jha\\Documents\\UBS\\Docx\\header.docx");
wordMLPackage.save(fileName);
}
}