简体   繁体   中英

Error in CTPageSZ class using Apache POI Java NetBeans Word Document

I try to produce a Microsoft Word document by using Apache POI and Java. I was really happy with the result until I try to change the orientation of the document to landscape. I did search and find some solutions. One of them is: https://stackoverflow.com/questions/28833418/switch-page-to-landscape-from-portrait-apache-poi .

When I try to enter the codes below. I have encountered some problems. I use Netbeans IDE.

XWPFDocument document = new XWPFDocument();
CTDocument1 doc = document.getDocument();
CTBody body = doc.getBody();
CTSectPr section = body.addNewSectPr();
XWPFParagraph paragraph = document.createParagraph();
CTP ctp = paragraph.getCTP();
CTPPr br = ctp.addNewPPr();
br.setSectPr(section);
CTPageSz pageSize = section.getPgSz();
pageSize.setOrient(STPageOrientation.LANDSCAPE);

Line 9 and 10 gives me error regarding "CTPageSz".

I encounter the following error messages from NetBeans:

  1. Create class "CTPageSz" in package apachetable1
  2. Create class "CTPageSz" in apachetable1.ApacheTable1
  3. Configure "Throwable method result is ignored" Hint

I search about CTPageSz but could not really know how to fix this error. I use Apache POI 3.13.

Are there any simpler ways to change orientation to landscape ? Thank you.

I was facing a similar issue with CTPageSz! I guess you are using the wrong jar!

Basically there are 2 jars, one is the poi-ooxml-schemas jar and the other being simply the ooxml-schemas jar.

What I've read around the internet is that the poi variant of the jar is smaller and contains only classes that one generally requires. And you get this variant when you download the apache-poi package from their website.

You need to include the other variant in your project ie the simple ooxml-jar which can be found here . The jar is 14.8MB in size, significantly bigger than the poi variant.

Hope this helps. All the best :)

i had the same issue, i did compile this dependencies, and the problem has been resolve

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.3</version>
    </dependency>

try to used it, and tell me if it work to you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM