com.sun.star.text.XTextRange Java Examples

The following examples show how to use com.sun.star.text.XTextRange. 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: BookmarkService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the bookmark for the specified name, or null if none was found with this name.
 * 
 * @param name the bookmark name to be used
 * 
 * @return the bookmark for the specified name, or null
 * 
 * @author Markus Krüger
 */
public IBookmark getBookmark(String name) {
  try {
    if (name == null)
      return null;
    XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class,
        textDocument.getXTextDocument());
    if (xBookmarksSupplier == null)
      return null;
    Object bookmark = xBookmarksSupplier.getBookmarks().getByName(name);
    XTextContent xBookmarkContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
        bookmark);
    XNamed xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, bookmark);
    if (xBookmarkContent == null)
      return null;
    XTextRange xBookmarkRange = xBookmarkContent.getAnchor();
    if (xBookmarkRange == null)
      return null;
    return new Bookmark(textDocument, xBookmarkRange, xNamed);
  }
  catch (Exception exception) {
    return null;
  }
}
 
Example #6
Source File: PageService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns page with the submitted index. The first page has the index 0.
 * 
 * @param index index of the page
 *  
 * @return page with the submitted index
 * 
 * @throws TextException if the page is not available
 * 
 * @author Andreas Bröker
 */
public IPage getPage(int index) throws TextException {
  try {
    XController xController = textDocument.getXTextDocument().getCurrentController();
    XTextViewCursorSupplier xTextViewCursorSupplier = (XTextViewCursorSupplier)UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
    XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor();
    XPageCursor pageCursor = (XPageCursor)UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
    pageCursor.jumpToPage((short)index);
    pageCursor.jumpToStartOfPage();
    XTextRange pageStart = textViewCursor.getStart();   
    PagePosition startPagePosition = new PagePosition(textDocument, pageStart);       
    pageCursor.jumpToEndOfPage();    
    XTextRange pageEnd = textViewCursor.getStart();
    PagePosition endPagePosition = new PagePosition(textDocument, pageEnd);
    return new Page(textDocument, startPagePosition, endPagePosition); 
  }
  catch(Exception exception) {
    TextException textException = new TextException(exception.getMessage());
    textException.initCause(exception);
    throw textException;
  }
}
 
Example #7
Source File: Paragraph.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Gets the text contained in this pragraph
 * 
 * @return the paragraph text or null if text cannot be gained
 * 
 * @throws TextException if there occurs an error while fetching the text
 * 
 * @author Sebastian Rösgen 
 */
public String getParagraphText() throws TextException {
  StringBuffer buffer = new StringBuffer();
  XEnumerationAccess contentEnumerationAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class,
      getXTextContent());
  XEnumeration enumeration = contentEnumerationAccess.createEnumeration();

  while (enumeration.hasMoreElements()) {
    try {
      Any any = (Any) enumeration.nextElement();
      XTextRange content = (XTextRange) any.getObject();

      // since one paragraph can be made out of several portions, we have to put'em together
      buffer.append(content.getString());
    }
    catch (Exception exception) {
      System.out.println("Error getting elements from enumeration while search paragraph text.");
    }
  }

  return buffer.toString();
}
 
Example #8
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 #9
Source File: BookmarkService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns all bookmarks.
 * 
 * @return all bookmarks
 * 
 * @author Markus Krüger
 */
public IBookmark[] getBookmarks() {
  try {
    XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class,
        textDocument.getXTextDocument());
    if (xBookmarksSupplier == null)
      return new IBookmark[0];
    XNameAccess nameAccess = xBookmarksSupplier.getBookmarks();
    String[] names = nameAccess.getElementNames();
    if (names.length < 1)
      return new IBookmark[0];
    List bookmarks = new ArrayList();
    for (int i = 0; i < names.length; i++) {
      Object bookmark = nameAccess.getByName(names[i]);
      XTextContent xBookmarkContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class,
          bookmark);
      XNamed xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, bookmark);
      if (xBookmarkContent == null)
        continue;
      XTextRange xBookmarkRange = xBookmarkContent.getAnchor();
      if (xBookmarkRange == null)
        continue;
      bookmarks.add(new Bookmark(textDocument, xBookmarkRange, xNamed));
    }
    return (IBookmark[]) bookmarks.toArray(new IBookmark[bookmarks.size()]);
  }
  catch (Exception exception) {
    return new IBookmark[0];
  }
}
 
Example #10
Source File: TextContentService.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ITextRange constructNewTextRange(IDocument doc, XTextRange xTextRange)
    throws TextException {
  try {
    TextRange range = new TextRange(doc, xTextRange);
    return range;
  }
  catch (Exception e) {
    TextException textException = new TextException(e.getMessage());
    textException.initCause(e);
    throw textException;
  }
}
 
Example #11
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 #12
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 #13
Source File: PagePosition.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new PagePosition.
 * 
 * @param textDocument text document to be used
 * @param xTextRange OpenOffice.org XTextRange interface
 * 
 * @throws IllegalArgumentException if one of the OpenOffice.org interfaces is not valid
 * 
 * @author Andreas Bröker
 */
public PagePosition(ITextDocument textDocument, XTextRange xTextRange) throws IllegalArgumentException {
  if(textDocument == null)
    throw new IllegalArgumentException("Submitted text document is not valid.");
  this.textDocument = textDocument;
  
  if(xTextRange == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org XTextRange interface is not valid.");
  this.xTextRange = xTextRange;
}
 
Example #14
Source File: TextContentEnumeration.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new TextContentEnumeration.
 * 
 * @param textDocument text document to be used
 * @param xTextRange OpenOffice.org XTextRange interface
 * 
 * @throws IllegalArgumentException IllegalArgumentException IllegalArgumentException if the OpenOffice.org interface 
 * is not valid
 * 
 * @author Andreas Bröker
 * @author Sebastian Rösgen
 */
public TextContentEnumeration(ITextDocument textDocument, XTextRange xTextRange) throws IllegalArgumentException {
  if(textDocument == null)
    throw new IllegalArgumentException("Submitted text document is not valid.");
  
  if(xTextRange == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org XTextRange interface is not valid.");
  
  this.xTextRange = xTextRange;
  this.textDocument = textDocument;
}
 
Example #15
Source File: TextRange.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Constructs new TextRange.

 * @param document OpenOffice.org document of the text range
 * @param xTextRange OpenOffice.org XTextRange interface
 *  
 * @throws IllegalArgumentException if one the OpenOffice.org interface is not valid
 * 
 * @author Andreas Bröker
 */
public TextRange(IDocument document, XTextRange xTextRange) throws IllegalArgumentException {
  if(xTextRange == null)
    throw new IllegalArgumentException("Submitted OpenOffice.org XTextRange interface is not valid.");
  this.xTextRange = xTextRange;    
  this.document = document;
}
 
Example #16
Source File: ViewCursor.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Moves to the given text range.
 * 
 * @param textRange the text range to go to
 * @param select if to extend the selection
 * 
 * @author Markus Krüger
 */
public void goToRange(ITextRange textRange, boolean select) {
  XTextRange xTextRange = textRange.getXTextRange();
  xTextViewCursor.gotoRange(xTextRange, select);
}
 
Example #17
Source File: TextRange.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns OpenOffice.org XTextRange interface. 
 * 
 * This method is not part of the public API. It should
 * never used from outside.
 * 
 * @return OpenOffice.org XTextRange interface
 * 
 * @author Andreas Bröker
 */
public XTextRange getXTextRange() {
  return xTextRange;
}
 
Example #18
Source File: Bookmark.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs new TextRange.
 * 
 * @param textDocument text document to be used
 * @param xTextRange OpenOffice.org XTextRange interface
 * @param bookmarkName the name of the bookmark to be used
 * 
 * @author Markus Krüger
 */
public Bookmark(ITextDocument textDocument, XTextRange xTextRange, XNamed bookmarkName) {
  super(textDocument, xTextRange);
  Assert.isNotNull(bookmarkName, XNamed.class, this);
  name = bookmarkName;
}
 
Example #19
Source File: ITextContentService.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructs a new TextRange.
 * 
 * @param doc the document to construct text range for
 * @param xTextRange the xTextRange to be used
 * 
 * @return new text range
 * 
 * @throws TextException if the text range cannot be constructed
 * 
 * @author Jan Reimann
 * @date 20.08.2009
 */
public ITextRange constructNewTextRange(IDocument doc, XTextRange xTextRange)
    throws TextException;
 
Example #20
Source File: ITextRange.java    From noa-libre with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns OpenOffice.org XTextRange interface. 
 * 
 * This method is not part of the public API. It should
 * never used from outside.
 * 
 * @return OpenOffice.org XTextRange interface
 * 
 * @author Andreas Bröker
 */
public XTextRange getXTextRange();
 
Example #21
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;