Search code examples
javaapache-poiopenxmldocx4jxmlbeans

Passing an Enum to a method as a parameter in Java?


I am trying to call a docx4j method "setAlgn" in the interface CTTextParagraphProperties, which, per the docx4j jar I am using and the compiler takes an Enum type as a parameter. I am passing the actual argument STTextAlignType.CTR which I believe should resolve to the value 'ctr' (citation: http://grepcode.com/file/repo1.maven.org/maven2/org.docx4j/docx4j/3.0.1/org/docx4j/dml/STTextAlignType.java?av=f, I am running this same code).

Here is my code:

import java.lang.Enum;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFChildAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFShapeGroup;
import org.apache.poi.xssf.usermodel.XSSFTextBox;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.docx4j.dml.*;


        public static XSSFTextBox createTextBox(XSSFSheet sh, String message, int row1, int col1, int row2, int col2, boolean is_bold, boolean is_italics, boolean is_underline, boolean centered, int fontSize){

        //Various apache-poi stuff
        XSSFWorkbook wb = sh.getWorkbook();
        XSSFDrawing drawing = sh.createDrawingPatriarch();
        XSSFClientAnchor clientanchor = new XSSFClientAnchor(0,0,0,0,(short)col1,row1,(short)col2,row2);
        XSSFChildAnchor childanchor = new XSSFChildAnchor(0,0,0,0);
        XSSFShapeGroup group = drawing.createGroup(clientanchor);
        XSSFTextBox textbox = group.createTextbox(childanchor);
        XSSFRichTextString richMessage = new XSSFRichTextString(message);
        XSSFFont textFont = wb.createFont();
        textFont.setFontHeightInPoints((short)fontSize);
        textFont.setFontName("Verdana");
        if(is_bold){
            textFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        }
        textFont.setItalic(is_italics);
        if(is_underline){
            textFont.setUnderline(XSSFFont.U_SINGLE);
        }
        if(centered){
        //Here is the code in question. 
        textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);
        }
        richMessage.applyFont(textFont);
        textbox.setText(richMessage);
        return textbox;
    }

My compiler returns the following error message:

com\tem\POIStuff.java:1105: error: method setAlgn in interface CTTextParagraphProperties cannot be applied to given types;
                    textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);

Ultimately, my question is how can I get the "setAlgn" method to accept 'STTextAlignType.CTR' as an Enum and not as object type 'STTextAlignType'? Thank you in advance very much for your help!


Solution

  • The problem is actually on the first line of your code snippet! Your issue is with

    import java.lang.Enum;
    

    CTTextParagraphProperties.setAlgn does take a class of type Enum, but it's not that kind of Enum. It has to be a org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum

    I'd suggest you switch your imports to be:

    import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType;
    import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum; 
    

    You can then set the alignment with things like STTextAlignType.L and it'll work fine