Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to set page orientation landscape and portrait in same word document using NPOI C#


I am using this code but this code is not running with NPOI in c#.

XWPFDocument doc=new XWPFDocument();
CT_Document document=doc.getDocument(); // getDocument() not showing in NPOI with c#
Please help me.

I am using NPOI Library for create word document.
Note: I am not using Apache.POI, and we can't use Apache.POI.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

private void changeOrientation(XWPFDocument document, String orientation){
    CTDocument1 doc = document.getDocument();
    CTBody body = doc.addNewBody();
    body.addNewSectPr();
    CTSectPr section = body.getSectPr();
    if(!section.isSetPgSz()) {
        section.addNewPgSz();
    }
    CTPageSz pageSize = section.getPgSz();
    if(orientation.equals("landscape")){
        pageSize.setOrient(STPageOrientation.LANDSCAPE);
        pageSize.setW(BigInteger.valueOf(842 * 20));
        pageSize.setH(BigInteger.valueOf(595 * 20));
    }
    else{
        pageSize.setOrient(STPageOrientation.PORTRAIT);
        pageSize.setH(BigInteger.valueOf(842 * 20));
        pageSize.setW(BigInteger.valueOf(595 * 20));
    }
}
Posted
Updated 1-Jul-19 18:54pm

1 solution

You can use this method in c# code.

NPOI.XWPF.UserModel.XWPFDocument finDoc = new NPOI.XWPF.UserModel.XWPFDocument();
CT_SectPr section = new CT_SectPr();
section.pgSz.orient = ST_PageOrientation.landscape;
section.pgSz.w = (ulong)(842 * 20);
section.pgSz.h = (ulong)(595 * 20);
finDoc.Document.body.sectPr = section;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900