Java源码示例:com.sun.star.text.XTextRange

示例1
/**
 * 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;
  }
}
 
示例2
/**
 * 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();
}
 
示例3
/**
 * 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;
  }
}
 
示例4
@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);
    }
}
 
示例5
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);
}
 
示例6
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);
    }
}
 
示例7
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);
    }
}
 
示例8
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)" );

  }
 
示例9
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)" );

  }
 
示例10
/**
 * 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);
  }
}
 
示例11
/**
 * {@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;
  }
}
 
示例12
/**
 * 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];
  }
}
 
示例13
/**
 * 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;
}
 
示例14
/**
 * 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;
}
 
示例15
/**
 * 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;
}
 
示例16
/**
 * 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();
 
示例17
/**
 * 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;
 
示例18
/**
 * 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;
}
 
示例19
/**
 * 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);
}
 
示例20
/**
 * 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;
}
 
示例21
/**
 * Inline content into doc template
 */
void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue, Matcher paramsMatcher)
        throws Exception;