com.sun.star.text.XText Java Examples

The following examples show how to use com.sun.star.text.XText. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlContentInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
private void insertHTML(XText destination, XTextRange textRange, String htmlContent)
        throws Exception {
    File tempFile = null;
    try {
        tempFile = File.createTempFile(UUID.randomUUID().toString(), ".htm");

        StringBuilder contentBuilder = new StringBuilder();
        contentBuilder.append(ENCODING_HEADER);
        contentBuilder.append(OPEN_HTML_TAGS);
        contentBuilder.append(htmlContent);
        contentBuilder.append(CLOSE_HTML_TAGS);

        FileUtils.writeByteArrayToFile(tempFile, contentBuilder.toString().getBytes());
        String fileUrl = "file:///" + tempFile.getCanonicalPath().replace("\\", "/");

        XTextCursor textCursor = destination.createTextCursorByRange(textRange);
        XDocumentInsertable insertable = as(XDocumentInsertable.class, textCursor);

        insertable.insertDocumentFromURL(fileUrl, new PropertyValue[0]);
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}
 
Example #2
Source File: HtmlContentInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
public void inlineToDoc(OfficeComponent officeComponent,
                        XTextRange textRange, XText destination,
                        Object paramValue, Matcher matcher) throws Exception {
    try {
        boolean inserted = false;
        if (paramValue != null) {
            String htmlContent = paramValue.toString();
            if (!StringUtils.isEmpty(htmlContent)) {
                insertHTML(destination, textRange, htmlContent);
                inserted = true;
            }
        }

        if (!inserted)
            destination.getText().insertString(textRange, "", true);
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting html to doc file", e);
    }
}
 
Example #3
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected void insertImage(XComponent document, OfficeResourceProvider officeResourceProvider, XText destination, XTextRange textRange,
                           Image image) throws Exception {
    XMultiServiceFactory xFactory = as(XMultiServiceFactory.class, document);
    XComponentContext xComponentContext = officeResourceProvider.getXComponentContext();
    XMultiComponentFactory serviceManager = xComponentContext.getServiceManager();

    Object oImage = xFactory.createInstance(TEXT_GRAPHIC_OBJECT);
    Object oGraphicProvider = serviceManager.createInstanceWithContext(GRAPHIC_PROVIDER_OBJECT, xComponentContext);

    XGraphicProvider xGraphicProvider = as(XGraphicProvider.class, oGraphicProvider);

    XPropertySet imageProperties = buildImageProperties(xGraphicProvider, oImage, image.imageContent);
    XTextContent xTextContent = as(XTextContent.class, oImage);
    destination.insertTextContent(textRange, xTextContent, true);
    setImageSize(image.width, image.height, oImage, imageProperties);
}
 
Example #4
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
@Override
public void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue,
                        Matcher paramsMatcher) throws Exception {
    try {
        if (paramValue != null) {
            Image image = new Image(paramValue, paramsMatcher);

            if (image.isValid()) {
                XComponent xComponent = officeComponent.getOfficeComponent();
                insertImage(xComponent, officeComponent.getOfficeResourceProvider(), destination, textRange, image);
            }
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to doc file", e);
    }
}
 
Example #5
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns content type.
 * 
 * @return content type
 * 
 * @author Andreas Bröker
 */
public int getContentType() {
  String formula = xCell.getFormula();
  if(formula != null) {
    if(formula.length() != 0)
      return ITextTableCell.TYPE_FORMULA;
  }
  double value = xCell.getValue();
  if(value != 0)
    return ITextTableCell.TYPE_VALUE;
  XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
  String content = xText.getString();
  if(content.length() == 0)
    return ITextTableCell.TYPE_EMPTY;
  char chars[] = content.toCharArray();
  for(int i=0; i<chars.length; i++) {
    if(Character.isDigit(chars[i])) {
      if(chars[i] != 0)
        return ITextTableCell.TYPE_TEXT;
    }
  }
  return ITextTableCell.TYPE_TEXT;    
}
 
Example #6
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns text table of the cell.
 * 
 * @return text table of the cell
 * 
 * @throws TextException if the text table is not available
 * 
 * @author Andreas Bröker
 * @author Markus Krüger
 */
public ITextTable getTextTable() throws TextException {
  if(textTable == null) {
    try {
      XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
      XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xText.getStart());
      Any any = (Any)xPropertySet.getPropertyValue("TextTable");
      XTextTable xTextTable = (XTextTable)any.getObject();
      textTable =  new TextTable(textDocument, xTextTable);
    }
    catch(Exception exception) {
      TextException textException = new TextException(exception.getMessage());
      textException.initCause(exception);
      throw textException;
    }
  }
  return textTable;
}
 
Example #7
Source File: HelloWorld.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
public static void printHW(XScriptContext xSc) {

    // getting the text document object
    XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xSc.getDocument());
    XText xText = xtextdocument.getText();
    XTextRange xTextRange = xText.getEnd();
    xTextRange.setString( "Hello World (in Java)" );

  }
 
Example #8
Source File: TableManager.java    From yarg with Apache License 2.0 5 votes vote down vote up
public XText findFirstEntryInRow(Pattern pattern, int row) {
    try {
        for (int i = 0; i < xTextTable.getColumns().getCount(); i++) {
            XText xText = as(XText.class, getXCell(i, row));
            String templateText = xText.getString();

            if (pattern.matcher(templateText).find()) {
                return xText;
            }
        }
    } catch (Exception e) {
        throw new ReportFormattingException(e);
    }
    return null;
}
 
Example #9
Source File: TextContentService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the XTextContent interface of the paragraph just added,
 * or null if not found.
 * 
 * @param oldParagraphsBeforeInsert the old paragraphs array before the new paragraph is added
 * 
 * @return  the XTextContent interface of the paragraph just added,
 * or null if not found
 * 
 * @throws TextException if something fails
 * 
 * @author Markus Krüger
 * @date 21.06.2010
 */
private XTextContent getNewParagraphTextContent(IParagraph[] oldParagraphsBeforeInsert)
    throws TextException {
  try {
    IParagraph[] newParagraphs = getRealParagraphs();
    for (int i = 0; i < newParagraphs.length; i++) {
      IParagraph newParagraph = newParagraphs[i];
      IParagraph oldParagraph = null;

      if (i < oldParagraphsBeforeInsert.length) {
        oldParagraph = oldParagraphsBeforeInsert[i];
      }
      else {
        return newParagraphs[newParagraphs.length - 1].getXTextContent();
      }

      XText text = newParagraph.getXTextContent().getAnchor().getText();
      text = textDocument.getTextService().getText().getXText();
      XTextRangeCompare comparator = (XTextRangeCompare) UnoRuntime.queryInterface(XTextRangeCompare.class,
          text);

      if (comparator.compareRegionStarts(newParagraph.getXTextContent().getAnchor().getStart(),
          oldParagraph.getXTextContent().getAnchor().getStart()) != 0) {
        return newParagraph.getXTextContent();
      }
    }
    return null;
  }
  catch (Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #10
Source File: Paragraph.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Sets new text to the paragraph.
 * 
 * @param text the text that should be placed
 * 
 * @author Sebastian Rösgen
 */
public void setParagraphText(String text) {
  if (text != null) {
    XTextRange anchor = getXTextContent().getAnchor();
    XText xText = anchor.getText();
    xText.insertString(anchor, text, false);
  }
}
 
Example #11
Source File: TextService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs new TextService.
 * 
 * @param textDocument text document to be used
 * @param xMultiServiceFactory OpenOffice.org XMultiServiceFactory interface
 * @param xText OpenOffice.org XText interface
 *  
 * @throws IllegalArgumentException if the submitted text document or OpenOffice.org XText interface is not valid
 * 
 * @author Andreas Bröker
 * @author Sebastian Rösgen
 * @author Markus Krüger
 */
public TextService(ITextDocument textDocument, XMultiServiceFactory xMultiServiceFactory, XText xText) throws IllegalArgumentException {
  if(textDocument == null)
    throw new IllegalArgumentException("The submitted text document is not valid.");
  
  if(xText == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org interface is not valid.");

  if(xMultiServiceFactory == null)
    throw new IllegalArgumentException("Submitted multi service factory is not valid.");
  
  this.xText = xText;
  this.xMultiServiceFactory = xMultiServiceFactory;
  this.textDocument = textDocument;
}
 
Example #12
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns text service.
 * 
 * @return text service
 * 
 * @author Andreas Bröker
 * @author Markus Krüger
 */
public ITextService getTextService() {
  if(textService == null) {
    XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
    XMultiServiceFactory xMultiServiceFactory = 
      (XMultiServiceFactory)UnoRuntime.queryInterface(
          XMultiServiceFactory.class, getTextDocument().getXTextDocument());
    textService = new TextService(textDocument, xMultiServiceFactory, xText);
  }
  return textService;
}
 
Example #13
Source File: HelloWorld.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
public static void printHW(XScriptContext xSc) {

    // getting the text document object
    XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xSc.getDocument());
    XText xText = xtextdocument.getText();
    XTextRange xTextRange = xText.getEnd();
    xTextRange.setString( "Hello World (in Java)" );

  }
 
Example #14
Source File: Snippet15.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void main(String[] args) {
    try {
        IOfficeApplication officeAplication = OfficeApplicationRuntime.getApplication(configuration);
        officeAplication.setConfiguration(configuration);
        officeAplication.activate();
        IDocumentService documentService = officeAplication.getDocumentService();
        IDocument document = documentService.constructNewDocument(IDocument.CALC, DocumentDescriptor.DEFAULT);
        ISpreadsheetDocument spreadsheetDocument = (ISpreadsheetDocument) document;
        XSpreadsheets spreadsheets = spreadsheetDocument.getSpreadsheetDocument().getSheets();
        String sheetName= "Sheet1";
        Object[][] rows = new Object[][]{
            new Object[]{"DataCell1","DataCell2","DataCell3","DataCell4"},
            new Object[]{new Integer(10),new Integer(20),new Integer(30),new Integer(40)},
            new Object[]{new Double(11.11),new Double(22.22),new Double(33.33),new Double(44.44)}};

        XSpreadsheet spreadsheet1 = (XSpreadsheet)UnoRuntime.queryInterface(XSpreadsheet.class,spreadsheets.getByName(sheetName));
        //insert your Data
        XSheetCellCursor cellCursor = spreadsheet1.createCursor();

        for(int i = 0; i < rows.length; i++) {
          Object[] cols = rows[i];

          for(int j = 0; j < cols.length; j++) {
            XCell cell= cellCursor.getCellByPosition(j,i);
            XText cellText = (XText)UnoRuntime.queryInterface(XText.class, cell);
            Object insert = cols[j];
            if(insert instanceof Number)
              cell.setValue(((Number)insert).doubleValue());
            else if(insert instanceof String)
              cellText.setString((String)insert);
            else
              cellText.setString(insert.toString());
           }
        }
    }
    catch (Throwable exception) {
        exception.printStackTrace();
    }
}
 
Example #15
Source File: TextContentService.java    From noa-libre with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ITextDocumentTextShape constructNewTextShape(TextInfo textInfo) throws TextException {
  try {
    if (xMultiServiceFactory == null)
      throw new TextException("OpenOffice.org XMultiServiceFactory interface not valid.");

    if (xTextShapeContainer == null)
      xTextShapeContainer = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class,
          xMultiServiceFactory.createInstance("com.sun.star.drawing.MarkerTable"));

    Object textShape = xMultiServiceFactory.createInstance("com.sun.star.text.TextFrame");

    XTextContent xTextShape = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
        textShape);

    XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextShape);

    String tempId = "tempTextShapeId" + System.currentTimeMillis();
    xTextShapeContainer.insertByName(tempId, textInfo.getName());

    XNamed xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, xTextShape);
    xNamed.setName(textInfo.getName());

    xProps.setPropertyValue("AnchorType", textInfo.getAnchor());

    int minWidth = textInfo.getMinimumWidth();
    int minHeight = textInfo.getMinimumHeight();
    if (minWidth != -1) {
      xProps.setPropertyValue("Width", new Integer(minWidth));
    }
    if (minHeight != -1) {
      xProps.setPropertyValue("Height", new Integer(minHeight));
    }
    if (textInfo.isAutoWidth()) {
      xProps.setPropertyValue("WidthType", new Short((short) 2)); //automatic
    }
    if (textInfo.isAutoHeight()) {
      xProps.setPropertyValue("FrameIsAutomaticHeight", new Boolean(true)); //automatic
      xProps.setPropertyValue("SizeType", SizeType.MIN);
    }
    else {
      xProps.setPropertyValue("SizeType", SizeType.FIX);
    }

    xProps.setPropertyValue("HoriOrient", Short.valueOf(textInfo.getHorizontalAlignment()));
    xProps.setPropertyValue("VertOrient", Short.valueOf(textInfo.getVerticalAlignment()));
    int backColor = textInfo.getBackColor();
    if (backColor != -1) {
      xProps.setPropertyValue("BackColor", new Integer(textInfo.getBackColor()));
      xProps.setPropertyValue("BackColorTransparency", new Short((short) 0));
      xProps.setPropertyValue("BackTransparent", new Boolean(false));
    }
    else {
      xProps.setPropertyValue("BackColorTransparency", new Short((short) 100));
      xProps.setPropertyValue("BackTransparent", new Boolean(true));
    }

    ITextDocumentTextShape textDocumentTextShape = new TextDocumentTextShape(textDocument,
        xTextShape,
        textInfo);

    ////////
    XText xShapeText = (XText) UnoRuntime.queryInterface(XText.class, textShape);
    textDocumentTextShape.setXText(xShapeText);
    ////////

    if (textShapeToTextShapeIds == null)
      textShapeToTextShapeIds = new HashMap<ITextDocumentTextShape, String>();
    textShapeToTextShapeIds.put(textDocumentTextShape, tempId);

    return textDocumentTextShape;
  }
  catch (Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #16
Source File: TextDocumentTextShape.java    From noa-libre with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void setXText(XText xText) {
  this.xText = xText;
}
 
Example #17
Source File: TextDocumentTextShape.java    From noa-libre with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public XText getXText() {
  return xText;
}
 
Example #18
Source File: TextContentService.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new TextContentService.
 * 
 * @param textDocument text document to be used
 * @param xText OpenOffice.org XText interface
 * @param xMultiServiceFactory OpenOffice.org XMultiServiceFactory interface
 * 
 * @throws IllegalArgumentException if the submitted text document or OpenOffice.org XText interface 
 * is not valid
 * 
 * @author Andreas Bröker
 * @author Sebastian Rösgen
 * @author Markus Krüger
 */
public TextContentService(ITextDocument textDocument, XMultiServiceFactory xMultiServiceFactory,
    XText xText) throws IllegalArgumentException {
  if (xText == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org XText interface is not valid.");
  if (textDocument == null)
    throw new IllegalArgumentException("Submitted text document is not valid.");
  if (xMultiServiceFactory == null)
    throw new IllegalArgumentException("Submitted multi service factory is not valid.");
  this.xText = xText;
  this.xMultiServiceFactory = xMultiServiceFactory;
  this.textDocument = textDocument;
}
 
Example #19
Source File: TextCursorService.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new TextCursorService.
 * 
 * @param textDocument text document to be used
 * @param xText OpenOffice.org XText interface to be used
 * 
 * @throws IllegalArgumentException if the submitted text document or OpenOffice.org XText 
 * interface is not valid
 * 
 * @author Andreas Bröker
 */
public TextCursorService(ITextDocument textDocument, XText xText) throws IllegalArgumentException {
  if(textDocument == null)
    throw new IllegalArgumentException("The submitted text document is not valid.");
  this.textDocument = textDocument;
  
  if(xText == null)
    throw new IllegalArgumentException("The submitted OpenOffice.org XText interface is not valid.");
  this.xText = xText;
}
 
Example #20
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Sets data of cell with the submitted name.
 * 
 * @param cellName name of the cell
 * @param xTextContent content to be inserted
 * 
 * @throws Exception if any error occurs
 * 
 * @author Andreas Bröker
 */
public void setCellData(String cellName, XTextContent xTextContent) throws Exception {
  XCell xCell = xTextTable.getCellByName(cellName);
  XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
  xText.setString("");
  xText.insertTextContent(xText.getStart(), xTextContent, true);
}
 
Example #21
Source File: Text.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new Text.
 * 
 * @param textDocument text document to be used
 * @param xText OpenOffice.org XText interface
 *  
 * @throws IllegalArgumentException if the submitted text document or OpenOffice.org XText 
 * interface is not valid
 * 
 * @author Andreas Bröker
 * @author Sebastian Rösgen
 */
public Text(ITextDocument textDocument, XText xText) throws IllegalArgumentException {
  if(textDocument == null)
    throw new IllegalArgumentException("The submitted text document is not valid.");
  this.textDocument = textDocument;
  
  if(xText == null)
    throw new IllegalArgumentException("The submitted OpenOffice.org XText interface is not valid.");
  this.xText = xText;
}
 
Example #22
Source File: TextTableCell.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Returns related page style of the text table cell.
 * 
 * @return related page style of the text table cell
 * 
 * @throws TextException if the page style is not available
 * 
 * @author Andreas Bröker
 */
public IPageStyle getPageStyle() throws TextException {
  if(textRange == null) {
    XText xText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
    textRange = new TextRange(textDocument, xText.getStart());
  }
  return textRange.getPageStyle();
}
 
Example #23
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Sets data of the cell with the submitted name.
 * 
 * @param cellName name of the cell
 * @param content content to be inserted
 * 
 * @throws Exception if any error occurs
 * 
 * @author Andreas Bröker
 */
public void setCellData(String cellName, String content) throws Exception {
  XCell xCell = xTextTable.getCellByName(cellName);
  XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
  xText.setString(content);
}
 
Example #24
Source File: CursorService.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs new CursorService.
 * 
 * @param xText OpenOffice.org XText interface
 * 
 * @throws IllegalArgumentException if the OpenOffice.org interface is not valid
 * 
 * @author Andreas Bröker
 */
public CursorService(XText xText) throws IllegalArgumentException {
  if(xText == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org interface is not valid.");
  this.xText = xText;
}
 
Example #25
Source File: TextTable.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Adds submitted text content to the cell with the submitted name.
 * 
 * @param cellName name of the cell
 * @param xTextContent text content to be added
 * 
 * @throws Exception if any error occurs
 * 
 * @author Andreas Bröker
 */
public void addCellData(String cellName, XTextContent xTextContent) throws Exception {
  XCell xCell = xTextTable.getCellByName(cellName);
  XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
  xText.insertTextContent(xText.getEnd(), xTextContent, true);
}
 
Example #26
Source File: Text.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns XText interface.
 * 
 * @return XText interface
 * 
 * @author Andreas Bröker
 * @date 19.09.2006
 */
public XText getXText() {
  return xText;
}
 
Example #27
Source File: ITextDocumentTextShape.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Sets the XText object
 * 
 * @param xText the XText object
 * 
 * @author Jan Reimann
 * @date 20.08.2009
 */
public void setXText(XText xText);
 
Example #28
Source File: ITextDocumentTextShape.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns the XText object for setting the text after it was inserted into the doc.
 * 
 * @return the XText object
 * 
 * @author Jan Reimann
 * @date 20.08.2009
 */
public XText getXText();
 
Example #29
Source File: IText.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns XText interface.
 * 
 * @return XText interface
 * 
 * @author Andreas Bröker
 * @date 19.09.2006
 */
public XText getXText();
 
Example #30
Source File: ContentInliner.java    From yarg with Apache License 2.0 2 votes vote down vote up
/**
 * Inline content into doc template
 */
void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue, Matcher paramsMatcher)
        throws Exception;