Page 1 of 1

Insert HTML code into Header and Footer

PostPosted: Fri Nov 23, 2012 4:41 am
by syed.ali
I have 2 issues
1. HTML to header in word.
2. Importing table the spacing after is default to 10 pt is there any way that we can set spacing after to 0 pt

1. I can create word document from HTML file using the code below without any problem and I want insert data from HTML file into the header and footer.
Code: Select all
                                                                File source = new File("c:\\document.htm");
                                                                String link = "c:\\worDocument.docx";
                                          String stringFromFile = FileUtils.readFileToString(source);
                    XHTMLImporter.setHyperlinkStyle("Hyperlink");     
                       
                    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();   
                    ObjectFactory objectFactory = Context.getWmlObjectFactory();
                    
                    AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/letter.html"));
                    afiPart.setBinaryData(stringFromFile.getBytes());
                    afiPart.setContentType(new ContentType("text/html"));

                    Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
                    CTAltChunk ac = objectFactory.createCTAltChunk();
                    ac.setId(altChunkRel.getId());
                    wordMLPackage.getMainDocumentPart().addObject(ac);




                    wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
                    wordMLPackage.save(new java.io.File(link));




For adding just the string for Header I am using the code below and can do it without any problem but i want add the data from HTML file as I did for document above.
Code: Select all
                  /**Header Part start*/
     HeaderPart headerPart = new HeaderPart();
     Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
     String hdrXml = "<w:hdr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                            + "<w:p>"
                            + "<w:pPr>"
                            //+ "<w:pStyle w:val=\"Header\"/>"
                            + "<w:jc w:val=\"center\"/>"
                            + "</w:pPr>"
                            + "<w:r>"
                            + "<w:t xml:space=\"preserve\">"+ headerStringFromFile.toString()+"</w:t>"
                            + "</w:r>"
                          //  + "<w:fldSimple w:instr=\" PAGE \\* MERGEFORMAT \">"
                           // + "<w:r>"
                          //  + "<w:rPr>"
                          //  + "<w:noProof/>"
                          //  + "</w:rPr>"
                           // + "</w:r>"
                           // + "</w:fldSimple>"
                           + "</w:p>"
                            + "</w:hdr>";
                    
                    Hdr hdr = (Hdr)XmlUtils.unmarshalString(hdrXml);
                    wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getFirstHeader();
                    headerPart.setJaxbElement(hdr);
                    
                    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
                    
                    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
                    // There is always a section wrapper, but it might not contain a sectPr
                    
                    if (sectPr == null) {
                      sectPr = objectFactory.createSectPr();
                      wordMLPackage.getMainDocumentPart().addObject(sectPr);
                      sections.get(sections.size() - 1).setSectPr(sectPr);
                    }
                    
                    HeaderReference headerReference = objectFactory.createHeaderReference();
                    headerReference.setId(rel.getId());
                    headerReference.setType(HdrFtrRef.DEFAULT);
                    sectPr.getEGHdrFtrReferences().add(headerReference);
                    
                    /**Header Part End*/   



Can you please send me the code that i can use to insert data into header from HTMLfile.


2. Importing from HTML file into the word document the SPACING AFTER is default to 10 pt is there any way that we can set SPACING AFTER to 0 pt