Page 1 of 1

adding space between words in header

PostPosted: Wed Mar 05, 2014 12:47 am
by zhj89901390
I used the following code to generate the header:
Code: Select all
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.utils.BufferUtil;
import org.docx4j.wml.Br;
import org.docx4j.wml.FldChar;
import org.docx4j.wml.Hdr;
import org.docx4j.wml.HdrFtrRef;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.Jc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.PPr;
import org.docx4j.wml.STBrType;
import org.docx4j.wml.STFldCharType;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.Tabs;
import org.docx4j.wml.Text;

public class HeaderFooterCreate {

   private static ObjectFactory objectFactory = new ObjectFactory();

   public static void main(String[] args) throws Exception {

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .createPackage();
      
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
      Relationship styleRel = mdp.getStyleDefinitionsPart().getSourceRelationships().get(0);
      mdp.getRelationshipsPart().removeRelationship(styleRel);
      
      createHeaderReference(wordMLPackage, relationship);
      
      wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_Header_footer.docx"));
                  
   }
   
   public static Relationship createHeaderPart(
         WordprocessingMLPackage wordprocessingMLPackage)
         throws Exception {
      
      HeaderPart headerPart = new HeaderPart();
      Relationship rel =  wordprocessingMLPackage.getMainDocumentPart()
            .addTargetPart(headerPart);      
      
      headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));

      return rel;
   }

   public static void createHeaderReference(
         WordprocessingMLPackage wordprocessingMLPackage,
         Relationship relationship )
         throws InvalidFormatException {

      List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
         
      SectPr sectPr = sections.get(sections.size() - 1).getSectPr();      
      if (sectPr==null ) {
         sectPr = objectFactory.createSectPr();
         wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
         sections.get(sections.size() - 1).setSectPr(sectPr);
      }

      HeaderReference headerReference = objectFactory.createHeaderReference();
      headerReference.setId(relationship.getId());
      headerReference.setType(HdrFtrRef.DEFAULT);
      sectPr.getEGHdrFtrReferences().add(headerReference);   

   }
   
   public static Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
         Part sourcePart) throws Exception {

      Hdr hdr = objectFactory.createHdr();
      
      File file = new File(System.getProperty("user.dir")
            + "/windows.jpg" );
      java.io.InputStream is = new java.io.FileInputStream(file);      
      
      hdr.getContent().add(
            newImage(wordprocessingMLPackage,
                  sourcePart,
                  BufferUtil.getBytesFromInputStream(is),
                  "filename", "alttext", 1, 2, 1000
                  )
      );
      
      return hdr;

   }   
   
   public static org.docx4j.wml.P newImage( WordprocessingMLPackage wordMLPackage,
         Part sourcePart,
         byte[] bytes,
         String filenameHint, String altText,
         int id1, int id2, long cx) throws Exception {

        BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, sourcePart, bytes);

        Inline inline = imagePart.createImageInline( filenameHint, altText,
             id1, id2, cx, false);
       
        // Now add the inline in w:p/w:r/w:drawing
      org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
      org.docx4j.wml.P p = factory.createP();
      org.docx4j.wml.R run = factory.createR();      
      p.getContent().add(run);       
      org.docx4j.wml.Drawing drawing = factory.createDrawing();      
      run.getContent().add(drawing);   
      drawing.getAnchorOrInline().add(inline);      
      
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
         String footer_time = df.format(new Date());       
      
      Text text = new Text(); 
         text.setValue(footer_time);
      run.getContent().add(text);

      return p;
   }   
}


then got the following result:
header.jpg
header.jpg (4.68 KiB) Viewed 2521 times


but I want to add some spaces between the logo and the date string, e.g:
header2.jpg
header2.jpg (5.79 KiB) Viewed 2521 times


I tried to the following code:
Code: Select all
Text text = new Text(); 
text.setValue("                                         " + footer_time);
run.getContent().add(text);

, but failed!

how can I modify the code in newImage() to get this, thanks a lot!

Re: adding space between words in header

PostPosted: Fri Mar 14, 2014 6:36 pm
by jason
How do you prefer to do it?

Three obvious choices:

1. a tab before the date (right aligned)
2. using a table with 2 cells
3. right align the paragraph, and float the image left.

Is your screen shot a screen shot of a Word docx? If so, why not upload to the webapp to generate suitable code? If not, make one then do the same.

Re: adding space between words in header

PostPosted: Mon Mar 24, 2014 5:07 pm
by cninfoer
Hi jason,
which API in docx4j can create a tab in a paragraph?
thanks in advance!

Re: adding space between words in header

PostPosted: Mon Mar 24, 2014 5:49 pm
by cninfoer
Hi jason,
now I got the API to create a tab — public R.Tab createRTab().
A question is that how to make the created tab right aligned by certain docx4j APIs?
thanks in advance!