0

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!

Tyler Wood
  • 35
  • 8
  • Can you link to the relevant documentation? I suspect we can help you without knowing the details of the libraries involved, but we can do so quicker if we can see the types involved. – Jon Skeet Jun 17 '14 at 16:40
  • There seems to be something weird going on. `CTTextParagraphProperties` is not an interface, it's a regular class. Are you sure you have right dependencies on your classpath? – geoand Jun 17 '14 at 16:52
  • @JonSkeet Yes, definitely. Actually though, I hate to say this, could you let me know what documentation you mean? The javadoc or equivalent for the docx4j library, and the apache poi library I am using? Sorry, still kind of a novice :/ – Tyler Wood Jun 17 '14 at 17:09
  • @geoand I do not believe I would have any overwrites in my classpath, I will research into that though. The jar files I have there include jcifs-1.3.17.jar, poi-ooxml-3.9.jar, ooxml-schemas-1.1.jar, and xbean.jar, just in case anything conflicting pops out at anyone from those pieces of info. Thank you for your post! – Tyler Wood Jun 17 '14 at 17:14
  • I am trying to mimic the implementation seen in the attached page in post two, by Yegor Kozlov. http://apache-poi.1045710.n5.nabble.com/How-to-align-text-in-the-shape-td5711188.html – Tyler Wood Jun 17 '14 at 17:17
  • @JonSkeet The best docx4j documentation I can find is the following page. http://www.docx4java.org/docx4j/plutext-docx4j_on_a_page-v300.pdf. Here is a group of code examples from the Docx4j website. Sorry I cannot be more thorough. – Tyler Wood Jun 17 '14 at 17:25

1 Answers1

3

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

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • Extreme thanks and gratitude for your advice here, I was able to defeat this compile error and move forward in my project. Marked as answered! Much appreciated! – Tyler Wood Jun 24 '14 at 22:48