Results 1 to 4 of 4
  1. #1
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    180
    Rep Power
    10

    Default add a logo in header

    hi,
    can any body please help me to add a logo in header of docx.
    Java Code:
    public static void main(String[] args) {
            try {
                //XWPFDocument docx = new XWPFDocument();
                CustomXWPFDocument docx = new CustomXWPFDocument();
                CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
                XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
                File imgFile = new File("D:\\test.jpg");
                //write header content
                CTP ctpHeader = CTP.Factory.newInstance();
                CTR ctrHeader = ctpHeader.addNewR();
                CTText ctHeader = ctrHeader.addNewT();
                String headerText = "This is header";
                ctHeader.setStringValue(headerText);
    
                XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx);
    
                headerParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFParagraph[] parsHeader = new XWPFParagraph[1];
                String blipId = headerParagraph.getDocument().addPictureData(
                        new FileInputStream(imgFile),
                        Document.PICTURE_TYPE_JPEG);
    
                docx.createPicture(blipId,
                        docx.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG),
                        252, 200);
    
                parsHeader[0] = headerParagraph;
                policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
    
                //write footer content
                CTP ctpFooter = CTP.Factory.newInstance();
                CTR ctrFooter = ctpFooter.addNewR();
                CTText ctFooter = ctrFooter.addNewT();
                String footerText = "This is footer";
                ctFooter.setStringValue(footerText);
                XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, docx);
                XWPFParagraph[] parsFooter = new XWPFParagraph[1];
                parsFooter[0] = footerParagraph;
                policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
    
                //write body content
                XWPFParagraph bodyParagraph = docx.createParagraph();
                bodyParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun r = bodyParagraph.createRun();
                r.setBold(true);
                r.setText("This is body content.");
    
                FileOutputStream out = new FileOutputStream("D:/a/test.docx");
                docx.write(out);
                out.close();
                System.out.println("Done");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    please check what is the issue ,the image is added in doc not in header ,
    please help me

    thanks in advance

  2. #2
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    180
    Rep Power
    10

    Default Re: add a logo in header

    Java Code:
    public class CustomXWPFDocument extends XWPFDocument
    {
        public CustomXWPFDocument()    {
         super();
        }
    
        public CustomXWPFDocument(InputStream in) throws IOException
        {
            super(in);
        }
    
    
    
        public void createPicture(String blipId,int id, int width, int height)
        {
            final int EMU = 9525;
            width *= EMU;
            height *= EMU;
            //String blipId = getAllPictures().get(id).getPackageRelationship().getId();
    
    
            CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
    
            String picXml = "" +
                    "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
                    "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                    "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                    "         <pic:nvPicPr>" +
                    "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
                    "            <pic:cNvPicPr/>" +
                    "         </pic:nvPicPr>" +
                    "         <pic:blipFill>" +
                    "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
                    "            <a:stretch>" +
                    "               <a:fillRect/>" +
                    "            </a:stretch>" +
                    "         </pic:blipFill>" +
                    "         <pic:spPr>" +
                    "            <a:xfrm>" +
                    "               <a:off x=\"0\" y=\"0\"/>" +
                    "               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
                    "            </a:xfrm>" +
                    "            <a:prstGeom prst=\"rect\">" +
                    "               <a:avLst/>" +
                    "            </a:prstGeom>" +
                    "         </pic:spPr>" +
                    "      </pic:pic>" +
                    "   </a:graphicData>" +
                    "</a:graphic>";
    
            //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
            XmlToken xmlToken = null;
            try
            {
                xmlToken = XmlToken.Factory.parse(picXml);
            }
            catch(XmlException xe)
            {
                xe.printStackTrace();
            }
            inline.set(xmlToken);
            //graphicData.set(xmlToken);
    
            inline.setDistT(0);
            inline.setDistB(0);
            inline.setDistL(0);
            inline.setDistR(0);
    
            CTPositiveSize2D extent = inline.addNewExtent();
            extent.setCx(width);
            extent.setCy(height);
    
            CTNonVisualDrawingProps docPr = inline.addNewDocPr();
            docPr.setId(id);
            docPr.setName("Picture " + id);
            docPr.setDescr("Generated");
        }
    }

  3. #3
    allen is offline Member
    Join Date
    Jan 2016
    Posts
    4
    Rep Power
    0

    Default Re: add a logo in header

    hey,I am sharing a code try this out:

    <div id="header-image">
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
    <img src="<?php echo get_template_directory_uri(); ?>/images/logo.png" alt="Logo" width="HERE" height="HERE" />
    </a>
    </div>

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: add a logo in header

    Quote Originally Posted by allen View Post
    hey,I am sharing a code try this out:

    <div id="header-image">
    <a rel="nofollow" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
    <img src="<?php echo get_template_directory_uri(); ?>/images/logo.png" alt="Logo" width="HERE" height="HERE" />
    </a>
    </div>
    What the hell? You're posting WordPress PHP code in a Java forum with an Apache POI question? You mad bro? Stop posting code just because you can. Start posting because you can contribute to the actual problem.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Pascal header into java header ?
    By fredvs in forum New To Java
    Replies: 0
    Last Post: 02-28-2014, 11:18 PM
  2. Legal to use the Java Logo?
    By developjava in forum Forum Lobby
    Replies: 2
    Last Post: 06-02-2012, 08:16 PM
  3. I need help with putting a logo on my code
    By MikeJ39 in forum New To Java
    Replies: 13
    Last Post: 10-22-2011, 01:10 AM
  4. setting application logo to tab
    By sameerk in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-29-2010, 11:36 AM
  5. Adding Logo to my project
    By ibrahimyoussof in forum New To Java
    Replies: 0
    Last Post: 04-14-2010, 10:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •