[Solved] Set Margin on Pages - Java

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
boa-ig
Posts: 3
Joined: Mon Jul 03, 2023 4:26 pm

[Solved] Set Margin on Pages - Java

Post by boa-ig »

Good day.
I have set PaperOrientation and PaperFormat but I can't find where to set right,left,page margin.

Code: Select all

        static void setPaper(Object oDoc_To_Store, com.sun.star.view.PaperOrientation paperOrientation,com.sun.star.view.PaperFormat paperFormat) {
            com.sun.star.view.XPrintable xPrintable = 
                    UnoRuntime.queryInterface(
                    com.sun.star.view.XPrintable.class, oDoc_To_Store);
            com.sun.star.beans.PropertyValue[] pv = new com.sun.star.beans.PropertyValue[2];
            pv[0] = new PropertyValue();
            pv[0].Name="PaperOrientation";
            pv[0].Value=paperOrientation;//com.sun.star.view.PaperOrientation.LANDSCAPE;
           // pv[0].State=pv[0].State.DEFAULT_VALUE;
            pv[1] = new PropertyValue();
            pv[1].Name="PaperFormat";
            pv[1].Value=paperFormat;//com.sun.star.view.PaperFormat.A4;
          //  pv[1].State=pv[0].State.DEFAULT_VALUE;

            xPrintable.setPrinter(pv)
        	
        }

xPageProps Always = null

                          com.sun.star.beans.XPropertySet xPageProps = (com.sun.star.beans.XPropertySet)
                              UnoRuntime.queryInterface(
                                  com.sun.star.beans.XPropertySet.class, xDrawPage);
                        if ((xPageProps!=null)) {// Always = null,
                        	System.out.println("++++++++ xPageProps!=null");
                        	Property[] pp = xPageProps.getPropertySetInfo().getProperties();// 
                        	int ii =pp.length;
                        	for (int i = 0; i < pp.length; i++) {
                        		  System.out.println(pp[i].Name+" "+pp[i].Attributes+" "+pp[i].Type);
                        		}
                        }
please tell me how to set right,left,page margin.
Last edited by Hagar Delest on Tue Jul 04, 2023 7:53 am, edited 1 time in total.
Reason: tagged solved.
LibreOffice 7.4.2.3 (x64), Windows 11
JeJe
Volunteer
Posts: 2906
Joined: Wed Mar 09, 2016 2:40 pm

Re: Set Margin on Pages - Java

Post by JeJe »

I think that's solely a property of the page style not a printer setting.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
boa-ig
Posts: 3
Joined: Mon Jul 03, 2023 4:26 pm

Re: Set Margin on Pages - Java

Post by boa-ig »

thanks, I understood, I did not put the style name correctly

Code: Select all

    			XStyle xStyle = (XStyle) UnoRuntime.queryInterface(
   			   // XStyle.class, xFamily.getByName("Default Style"));//this option does not work
   			   // XStyle.class, xFamily.getByName("Default"));//this option does not work
   			    XStyle.class, xFamily.getByName("Standard"));
    			XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
    			    XPropertySet.class, xStyle);
    			xStyleProps.setPropertyValue ("TopMargin", 50*25.4);
    			xStyleProps.setPropertyValue ("BottomMargin", 50*25.4);
LibreOffice 7.4.2.3 (x64), Windows 11
JeJe
Volunteer
Posts: 2906
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Set Margin on Pages - Java

Post by JeJe »

Do you use MRI? I don't know Java but I get this from it. initial target is the document component.

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.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.IllegalArgumentException;
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("PageStyles"));
		
		XNameAccess xNameAccess2 = UnoRuntime.queryInterface(
			XNameAccess.class, xNameContainer);
		XStyle xStyle = UnoRuntime.queryInterface(
			XStyle.class, xNameAccess2.getByName("Standard"));
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xStyle);
		int nLeftMargin = AnyConverter.toInt(xPropSet.getPropertyValue("LeftMargin"));
		
	}
	catch (NoSuchElementException e1)
	{
		// getByName
		e1.printStackTrace();
	}
	catch (WrappedTargetException e2)
	{
		// getByName, getPropertyValue
		e2.printStackTrace();
	}
	catch (IllegalArgumentException e3)
	{
		// 
		e3.printStackTrace();
	}
	catch (UnknownPropertyException e4)
	{
		// getPropertyValue
		e4.printStackTrace();
	}
	catch (RuntimeException e5)
	{
		// getByName
		e5.printStackTrace();
	}
}
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
boa-ig
Posts: 3
Joined: Mon Jul 03, 2023 4:26 pm

Re: [Solved] Set Margin on Pages - Java

Post by boa-ig »

sorry, I don't know what MRI is. :)
but a solution has been found.

Code: Select all

	static void setPaperMarginPages(XTextDocument docText, int TopMargin,int BottomMargin) {
        
		try {
			XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
				    UnoRuntime.queryInterface(
				    XStyleFamiliesSupplier.class, docText);
				 XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface ( 
				    XNameAccess.class, xSupplier.getStyleFamilies());
				 XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface( 
				    XNameContainer.class, xFamilies.getByName("PageStyles"));
			    //	Standard, First Page, Left Page, Right Page, Envelope, Index, HTML, Footnote, Endnote, Landscape
				XStyle xStyle = (XStyle) UnoRuntime.queryInterface(
				    XStyle.class, xFamily.getByName("Standard"));
				XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
				    XPropertySet.class, xStyle);
				//System.out.println("TopMargin-"+xStyleProps.getPropertyValue("TopMargin"));
				xStyleProps.setPropertyValue ("TopMargin", TopMargin);//1000=1см,1500=1.5см
				xStyleProps.setPropertyValue ("BottomMargin", BottomMargin);
LibreOffice 7.4.2.3 (x64), Windows 11
User avatar
robleyd
Moderator
Posts: 5235
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: [Solved] Set Margin on Pages - Java

Post by robleyd »

Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.8.2.1; SlackBuild for 24.8.2 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue
Unexpected '{' on line 32
.
Post Reply