Hide XTextRange using setPropertyValue

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Hide XTextRange using setPropertyValue

Post by Lumberjack »

Hi
I want hide XTextRange using setPropertyValue. What propertyName should i use to do it.

Above is example. SetPropertyValue set text color to Red

Code: Select all

 xprops.setPropertyValue("CharColor", new uno.Any(0xFF0000));
but i want hide text (no change color to white)

Code: Select all

                XSearchDescriptor xSearchDecriptor2 = ((unoidl.com.sun.star.util.XSearchable)newDoc).createSearchDescriptor();
                xSearchDecriptor2.setSearchString(klucz);
                object obj2 = ((unoidl.com.sun.star.util.XSearchable)newDoc).findFirst(xSearchDecriptor2);

                unoidl.com.sun.star.beans.XPropertySet xprops = (unoidl.com.sun.star.beans.XPropertySet)obj2;
                xprops.setPropertyValue("CharColor", new uno.Any(0xFF0000));
OpenOffice 3.1 on Windows Vista
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hide XTextRange using setPropertyValue

Post by Villeroy »

The following Java snippet has been generated by the MRI extension. It applies some character style "example" to some text range.

Code: Select all

import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
			XIndexAccess.class, oInitialTarget);
		XTextRange xTextRange = UnoRuntime.queryInterface(
			XTextRange.class, xIndexAccess.getByIndex(0));
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xTextRange);
		
		String sCharStyleName = AnyConverter.toString(xPropSet.getPropertyValue("CharStyleName"));
		
		xPropSet.setPropertyValue("CharStyleName", "Example");
		
	}
	catch (IndexOutOfBoundsException e1)
	{
		// getByIndex
		e1.printStackTrace();
	}
	catch (WrappedTargetException e2)
	{
		// getByIndex, getPropertyValue, setPropertyValue
		e2.printStackTrace();
	}
	catch (UnknownPropertyException e3)
	{
		// getPropertyValue, setPropertyValue
		e3.printStackTrace();
	}
	catch (IllegalArgumentException e4)
	{
		// , setPropertyValue
		e4.printStackTrace();
	}
	catch (PropertyVetoException e5)
	{
		// setPropertyValue
		e5.printStackTrace();
	}
}
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Hide XTextRange using setPropertyValue

Post by JeJe »

Are you just looking for the term CharHidden?

MRI will help you find it, as said above, or the API page for Character Properties

https://www.openoffice.org/api/docs/com ... CharHidden
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Lumberjack
Posts: 13
Joined: Thu Jan 28, 2021 4:02 pm

Re: Hide XTextRange using setPropertyValue

Post by Lumberjack »

Thanks a lot.

I looking for this page.

https://www.openoffice.org/api/docs/com ... CharHidden
OpenOffice 3.1 on Windows Vista
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hide XTextRange using setPropertyValue

Post by Villeroy »

CharHidden in MRI:

Code: Select all

import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
			XIndexAccess.class, oInitialTarget);
		XTextRange xTextRange = UnoRuntime.queryInterface(
			XTextRange.class, xIndexAccess.getByIndex(0));
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xTextRange);
		
		xPropSet.setPropertyValue("CharHidden", true);
		
	}
	catch (IndexOutOfBoundsException e1)
	{
		// getByIndex
		e1.printStackTrace();
	}
	catch (WrappedTargetException e2)
	{
		// getByIndex, setPropertyValue
		e2.printStackTrace();
	}
	catch (UnknownPropertyException e3)
	{
		// setPropertyValue
		e3.printStackTrace();
	}
	catch (PropertyVetoException e4)
	{
		// setPropertyValue
		e4.printStackTrace();
	}
	catch (IllegalArgumentException e5)
	{
		// setPropertyValue
		e5.printStackTrace();
	}
}
CharHidden is also a property of a character style. Whatever formatting you want to apply, it is by far easier to do with styles.

Code: Select all

import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.RuntimeException;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XStyleFamiliesSupplier xStyleFamiliesSupplier = UnoRuntime.queryInterface(
			XStyleFamiliesSupplier.class, oInitialTarget);
		XNameAccess xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
		
		XNameContainer xNameContainer = UnoRuntime.queryInterface(
			XNameContainer.class, xNameAccess.getByName("CharacterStyles"));
		
		XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
			XIndexAccess.class, xNameContainer);
		XStyle xStyle = UnoRuntime.queryInterface(
			XStyle.class, xIndexAccess.getByIndex(0));
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xStyle);
		boolean bCharHidden = AnyConverter.toBoolean(xPropSet.getPropertyValue("CharHidden"));
		
	}
	catch (NoSuchElementException e1)
	{
		// getByName
		e1.printStackTrace();
	}
	catch (WrappedTargetException e2)
	{
		// getByName, getByIndex, getPropertyValue
		e2.printStackTrace();
	}
	catch (RuntimeException e3)
	{
		// getByName
		e3.printStackTrace();
	}
	catch (IndexOutOfBoundsException e4)
	{
		// getByIndex
		e4.printStackTrace();
	}
	catch (UnknownPropertyException e5)
	{
		// getPropertyValue
		e5.printStackTrace();
	}
	catch (IllegalArgumentException e6)
	{
		// 
		e6.printStackTrace();
	}
}
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Hide XTextRange using setPropertyValue

Post by Villeroy »

OOops. The code in the original posting is C++ or C#. Apply "CharHidden" in C# generated by MRI:

Code: Select all

using System;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.uno;

public class Snippet {
public void snippet(XComponentContext xContext, object oInitialTarget)
{
	try
	{
		XIndexAccess xIndexAccess = (XIndexAccess) oInitialTarget;
		XTextRange xTextRange = (XTextRange) xIndexAccess.getByIndex(0).Value;
		XPropertySet xPropSet = (XPropertySet)xTextRange;
		
		xPropSet.setPropertyValue("CharHidden", new uno.Any(typeof(bool), true));
	}
	catch (IndexOutOfBoundsException e)
	{
		// getByIndex
		Console.WriteLine(e.Message);
	}
	catch (WrappedTargetException e)
	{
		// getByIndex, setPropertyValue
		Console.WriteLine(e.Message);
	}
	catch (UnknownPropertyException e)
	{
		// setPropertyValue
		Console.WriteLine(e.Message);
	}
	catch (PropertyVetoException e)
	{
		// setPropertyValue
		Console.WriteLine(e.Message);
	}
	catch (IllegalArgumentException e)
	{
		// setPropertyValue
		Console.WriteLine(e.Message);
	}
}
}
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply