Page 1 of 1

some pages in portrait and some in landscape

PostPosted: Thu May 05, 2016 1:06 pm
by Chandra123
Hi,

I have to display some pages to display in portrait and some in landscape. I saw few topics for this but they are not helping. i m adding html using addAltChunk. We will have some iterative list of html content. during this process we have to some chunks as landscape or portrait. tried after addAltchunk , created sectPr with respective orientation and adding it to body but not helping. can you please help here. Other way tried to haave para and ppr and adding a paragraph helps but this results in empty page added.

Re: some pages in portrait and some in landscape

PostPosted: Thu May 05, 2016 1:28 pm
by Chandra123
tried to set the sectPR as mentioend
docx-java-f6/landscape-page-orientation-t1192.html
and maindocumentpart.getBody.setSectPr

did not help
tried to add paragrah approachmentioned ar\t

http://stackoverflow.com/questions/1498 ... dle-of-doc

creates empty page

Re: some pages in portrait and some in landscape

PostPosted: Thu May 05, 2016 6:38 pm
by jason
This is pretty straightforward.

To make some content landscape. Add w:p/w:pPr/w:sectPr immediately after that content (with the right stuff in sectPr, of course).

To see how to do this, create a sample docx (insert section break etc), then use it to generate corresponding code. To do this, upload the resulting docx to the docx4j webapp, or use the Helper Word AddIn.

If you are still having trouble, feel free to post your code and the result docx here.

Re: some pages in portrait and some in landscape

PostPosted: Sat May 07, 2016 5:37 pm
by Chandra123
Hi Jason
Thank you for the response .But i still see the issue. here is the code
Code: Select all
public static void main(String[] a){
      try{
      WordprocessingMLPackage mlPackage = WordprocessingMLPackage.createPackage();
      ArrayList<DocSection> sec=new ArrayList();
      sec.add(new DocSection("<html><p>hi</p></html>","portrait"));
      sec.add(new DocSection("<html><p>hi</p></html>","landscape"));
      sec.add(new DocSection("<html><p>hi</p></html>","portrait"));
      
      java.util.Iterator<DocSection> itr = sec.iterator();
      while(itr.hasNext()){
         DocSection temp=(DocSection) itr.next();
         SectPr sectPr=null;
         ObjectFactory objectFactory = new ObjectFactory();
         sectPr = objectFactory.createSectPr();
         PgSz landscape = new PgSz();
          landscape.setH(BigInteger.valueOf(11906));
          landscape.setW(BigInteger.valueOf(16383));
          
         if("landscape".equalsIgnoreCase(temp.orientation)){
          /*sectPr = (org.docx4j.wml.SectPr)org.docx4j.XmlUtils.unmarshalString("<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                    + "<w:pgSz w:w=\"16839\" w:h=\"11907\" w:orient=\""+temp.orientation+"\"/>"
                    + "</w:sectPr>");*/
            landscape.setOrient(STPageOrientation.LANDSCAPE);   
         }else{
             /*sectPr = (org.docx4j.wml.SectPr)org.docx4j.XmlUtils.unmarshalString("<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                          + "<w:pgSz w:w=\"16839\" w:h=\"11907\" />"                          + "</w:sectPr>");*/
            landscape.setOrient(STPageOrientation.PORTRAIT);
         }
         sectPr.setPgSz(landscape);
         P p = objectFactory.createP();
         PPr createPPr = objectFactory.createPPr();
         createPPr.setSectPr(sectPr);
         p.setPPr(createPPr);
         
         mlPackage.getMainDocumentPart().addAltChunk(
               org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType.Html,
               temp.htm.toString().getBytes());
         org.docx4j.wml.Br objBr = new org.docx4j.wml.Br();
         objBr.setType(org.docx4j.wml.STBrType.PAGE);
         //mlPackage.getMainDocumentPart().addObject(objBr);
         
         mlPackage.getMainDocumentPart().addObject(p);
                  
      }
      
      FileOutputStream out = new FileOutputStream("test.docx");
      mlPackage.save(out);
      }catch(Exception e){
         e.printStackTrace();
      }
      
}

DocSection is wrapper class to hold html chunks and page orientation. my requirement is to display each html in specified orientation. And here is the generated docx. I could not understand on generated out put. What i understood from generated document.xml from web app for similar docx uploaded , after each chunk there should be w:p\W:PPr\W.sectPR. But last chunk dont have ppr instead there is sectpr at the end before body. Tried all these but do not help me. Could you please check and let me know where i am going wrong.
Also i dont understand why an empty page generated at the end

Re: some pages in portrait and some in landscape

PostPosted: Sun May 08, 2016 1:29 pm
by jason
YOu have generated:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
    <w:body>
        <w:altChunk r:id="rId2"/>
        <w:p >
            <w:pPr>

                <w:sectPr>
                    <w:pgSz w:w="16383" w:h="11906" w:orient="portrait"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:altChunk r:id="rId3"/>
        <w:p>
            <w:pPr>
                <w:sectPr>
                    <w:pgSz w:w="16383" w:h="11906" w:orient="landscape"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:altChunk r:id="rId4"/>
        <w:p>
            <w:pPr>
                <w:sectPr>
                    <w:pgSz w:w="16383" w:h="11906" w:orient="portrait"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:sectPr>
            <w:pgSz w:w="11907" w:h="16839" w:code="9"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>
        </w:sectPr>
    </w:body>
</w:document>

 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


I'd guess the first few are the same orientation because Word takes w:w and w:h literally and ignores w:orient? (for your portrait sections)

You have the extra page because you have 2 sectPr at the end:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
                <w:sectPr>
                    <w:pgSz w:w="16383" w:h="11906" w:orient="portrait"/>
                </w:sectPr>
            </w:pPr>
        </w:p>
        <w:sectPr>
            <w:pgSz w:w="11907" w:h="16839" w:code="9"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>
        </w:sectPr>
    </w:body>
</w:document>

 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Get rid of one of them. The body level one is set on the Body object. You can set it to null.

Re: some pages in portrait and some in landscape

PostPosted: Thu May 12, 2016 1:46 pm
by Chandra123
Thank Jason for the reply. I was able to figure it out. Is there any step by step tutorials on dcx4j. When we see sample of header footer, some fiejd char etc coding is done. Really dont understand why is that required. When do we use CT* classes etc. Is there any tutorial that answers these questions.

Re: some pages in portrait and some in landscape

PostPosted: Thu May 12, 2016 4:34 pm
by jason
There is the "Getting Started" document, and the "cheat sheet".

With the webapp, you can see what class represents an element, and you can also click on an element to see the corresponding spec documentation.

Re: some pages in portrait and some in landscape

PostPosted: Wed Oct 26, 2016 1:13 pm
by Chandra123
Hi Json,

I have again some query on setting body sectpr to null. I was trying to set body sectPR null as below. But still its not helping to remove extra page generated.
Code: Select all
mlPackage.getMainDocumentPart().getJaxbElement().getBody().setSectPr(null);


Could you please let me know how to get rid of extra page.

Re: some pages in portrait and some in landscape

PostPosted: Wed Oct 26, 2016 5:28 pm
by jason
Please attach your docx or send it to me.