Hi Nicole,
Nicole Scholz escribió:
Hi!
I try to deactivate the underlining of hyperlinks in my Writer Document. Hence I accessed the CharacterStyles of the document. But now I don't know how to deactivate the hyperlinks for the whole document.
Here is my code:
*******************************************************
xDocumentFactory = xWriterComponent~XMultiServiceFactory
xTextDocument = xWriterComponent~XTextDocument
xText = xTextDocument~getText()
xTextCursor = xText~createTextCursor()
xPropertySet = xTextCursor~xPropertySet
xDocumentFactory = xWriterComponent~XMultiServiceFactory
xTextDocument = xWriterComponent~XTextDocument
xText = xTextDocument~getText()
xTextCursor = xText~createTextCursor()
Textprops = xTextCursor~xPropertySet
xFamiliesSupplier = xTextDocument~XStyleFamiliesSupplier
xParaStyleFamily =
xFamiliesSupplier~getStyleFamilies~getByName("CharacterStyles")~XNameContainer
*******************************************************
Can someone give me an example how to achieve this?
if hyperlinks have the default character style, things are as simple as:
* getting the character styles family
* getting from this family the style used for hyperlinks ("visited" and
"unvisited")
* change the property "CharUnderline", set it to css.awt.FontUnderline.NONE
If they have been set with hard formatting or custom styles, you can
also try to search in the document. See the demo attached [I'm too lazy
so it's copy & paste from here & there, but clear any way].
Regards
Ariel.
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
http://www.ArielConstenlaHaile.com.ar/ooo/
"Aus der Kriegsschule des Lebens
- Was mich nicht umbringt,
macht mich härter."
Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.
package org.openoffice.sdk.writer;
import com.sun.star.awt.FontUnderline;
import com.sun.star.awt.FontWeight;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XMultiPropertyStates;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XPropertyReplace;
import com.sun.star.util.XSearchDescriptor;
import com.sun.star.util.XSearchable;
import java.util.Vector;
/**
*
* @author ariel
*/
public class HyperlinkDemo {
private final XComponentContext m_xContext;
public HyperlinkDemo(XComponentContext xContext) {
this.m_xContext = xContext;
}
protected void run() {
try {
XTextDocument xTextDocument = createNewTextDoc(m_xContext);
if (xTextDocument == null) {
return;
}
XStyleFamiliesSupplier xStyleFamiliesSupplier =
(XStyleFamiliesSupplier) UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, xTextDocument);
XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();
XNameAccess xCharStyleFamily = (XNameAccess) UnoRuntime.queryInterface(
XNameAccess.class, xStyleFamilies.getByName("CharacterStyles"));
XPropertySet xInternetLinkStyleProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xCharStyleFamily.getByName("Internet Link"));
XPropertySet xVisitedInternetLinkStyleProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xCharStyleFamily.getByName("Visited Internet Link"));
xInternetLinkStyleProps.setPropertyValue("CharUnderline", FontUnderline.NONE);
xVisitedInternetLinkStyleProps.setPropertyValue("CharUnderline", FontUnderline.NONE);
// test it
XText xText = xTextDocument.getText();
XTextCursor xTextCursor = xText.createTextCursor();
XPropertySet xTextCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextCursor);
XMultiPropertyStates xCursorMultiProps = (XMultiPropertyStates) UnoRuntime.queryInterface(
XMultiPropertyStates.class, xTextCursorProps);
insertText(xTextCursor, "A demo about ");
xTextCursorProps.setPropertyValue("HyperLinkURL", "http://api.openofficce.org/" +
"docs/common/ref/com/sun/star/style/CharacterProperties.html");
xTextCursorProps.setPropertyValue("HyperLinkTarget", "_blank");
insertText(xTextCursor, "character properties");
xCursorMultiProps.setPropertiesToDefault(new String[]{"HyperLinkTarget", "HyperLinkURL"});
insertText(xTextCursor, ", in particular about ");
xTextCursorProps.setPropertyValue("HyperLinkURL", "http://api.openofficce.org/" +
"docs/common/ref/com/sun/star/style/CharacterProperties.html#HyperLinkURL");
xTextCursorProps.setPropertyValue("HyperLinkTarget", "_blank");
insertText(xTextCursor, "HyperLinkURL");
xCursorMultiProps.setPropertiesToDefault(new String[]{"HyperLinkTarget", "HyperLinkURL"});
insertText(xTextCursor, " and ");
xTextCursorProps.setPropertyValue("HyperLinkURL", "http://api.openofficce.org/" +
"docs/common/ref/com/sun/star/style/CharacterProperties.html#CharUnderline");
xTextCursorProps.setPropertyValue("HyperLinkTarget", "_blank");
insertText(xTextCursor, "CharUnderline");
xCursorMultiProps.setPropertiesToDefault(new String[]{"HyperLinkTarget", "HyperLinkURL"});
insertText(xTextCursor, ".");
///////////////////////////////////////////////////////////////////
//
//
// Search Demo
XSearchable xSearchable = (XSearchable) UnoRuntime.queryInterface(
XSearchable.class, xTextDocument);
XSearchDescriptor xSearchDescriptor = xSearchable.createSearchDescriptor();
XPropertyReplace xPropertyReplace = (XPropertyReplace) UnoRuntime.queryInterface(
XPropertyReplace.class, xSearchDescriptor);
/**
* specifies if specific property values are searched,
* or just the existence of the specified properties.
*/
xPropertyReplace.setValueSearch( false );
PropertyValue[] aSearchAttrs = new PropertyValue[1];
aSearchAttrs[0] = new PropertyValue();
aSearchAttrs[0].Name = "HyperLinkURL";
// we only search if this attribute exists, so there is no value
// aSearchAttrs[0].Value = "";
/**
* sets the properties to search for.
*/
xPropertyReplace.setSearchAttributes( aSearchAttrs );
XIndexAccess xTextRanges = xSearchable.findAll(xSearchDescriptor);
Vector<String> aHyperLinkStyles = new Vector<String>();
for (int i = 0; i < xTextRanges.getCount(); i++) {
XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
XTextRange.class, xTextRanges.getByIndex(i));
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextRange);
if (xTextRange != null && xPropertySet != null){
/**
* Here different things could be done, depending on
* if default styles were applied, or custom ones, or
* hard formatting:
*
* * set new style names
* * change the existing ones
* * hard formatting (never a good idea)
* * ...
*/
//**********************************************************
// if default styles, this strings are empty:
String sVisitedCharStyleName = AnyConverter.toString(
xPropertySet.getPropertyValue("VisitedCharStyleName"));
String sUnvisitedCharStyleName = AnyConverter.toString(
xPropertySet.getPropertyValue("UnvisitedCharStyleName"));
// so this is usefull only when custom style where applied
if (
sVisitedCharStyleName.length() > 0 &&
!aHyperLinkStyles.contains(sVisitedCharStyleName)
)
{
aHyperLinkStyles.add(sVisitedCharStyleName);
XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xCharStyleFamily.getByName(sVisitedCharStyleName) );
//xStyleProps.setPropertyValue("CharUnderline", FontUnderline.NONE);
}
// repeat the same for unvisited...
//*********************************************************
// set a new style
//xPropertySet.setPropertyValue("VisitedCharStyleName", "The name of a new style");
//*********************************************************
// hard formatting [never a good idea]
//xPropertySet.setPropertyValue("CharUnderline", FontUnderline.NONE);
xPropertySet.setPropertyValue("CharWeight", FontWeight.BOLD);
//**********************************************************
//....
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//**************************************************************************
public static XTextDocument createNewTextDoc(XComponentContext xContext) {
XTextDocument xTextDocument = null;
try {
XComponentLoader xComponentLoader =
(XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
if (xComponentLoader != null) {
xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class,
xComponentLoader.loadComponentFromURL(
"private:factory/swriter",
"_default", 0, new PropertyValue[]{}));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return xTextDocument;
}
}
public static void insertText(XTextRange xTextRange, String string) {
xTextRange.getText().insertString(xTextRange, string, false);
}
public static void insertParaBreak(XTextRange xTextRange) {
try {
xTextRange.getText().insertControlCharacter(
xTextRange, ControlCharacter.PARAGRAPH_BREAK, false);
} catch (com.sun.star.lang.IllegalArgumentException ex) {
ex.printStackTrace();
}
}
//**************************************************************************
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// get the remote office component context
XComponentContext xContext = Bootstrap.bootstrap();
if (xContext == null) {
System.err.println("ERROR: Could not bootstrap default Office.");
}
HyperlinkDemo demo = new HyperlinkDemo(xContext);
demo.run();
} catch (java.lang.Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]