Top > OOoJava > Calc > headerfooter

ページヘッダとフッタ Edit

右ページの中央のヘッダに書き込みます。

"Default" のページスタイルを XStyleFamiliesSupplier を通じて取得します。そのあとで,RightPageHeader プロパティー (右ページのヘッダ) を利用して中央のヘッダ getCenterText() を設定します。ヘッダテキストは XText インターフェースです。そのため,setString() 以外の複雑な方法で設定が可能です (文字のサイズなど)。

import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.table.XCell;
import com.sun.star.text.XText;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.container.XNameContainer;
import com.sun.star.container.XNameAccess;
import com.sun.star.sheet.XHeaderFooterContent;
public class CalcPageHeader extends java.lang.Object {
	private XComponentContext xRemoteContext = null;
	private XMultiComponentFactory xRemoteServiceManager = null;
	
	public static void main(String[] args) {
		CalcPageHeader Connection = new CalcPageHeader();
		try {
			Connection.Connect();
		}
		catch (java.lang.Exception e) {
			e.printStackTrace();
		}
		finally {
			System.exit(0);
		}
	}
	
	protected void Connect() throws java.lang.Exception {
		try {
			xRemoteServiceManager = this.getRemoteServiceManager(
				"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
			Object desktop = xRemoteServiceManager.createInstanceWithContext(
					"com.sun.star.frame.Desktop", xRemoteContext);
			XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
					XComponentLoader.class, desktop);
			PropertyValue[] loadProps = new PropertyValue[0];
			XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
					"private:factory/scalc", "_blank", 0, loadProps);
			XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
					XSpreadsheetDocument.class, xSpreadsheetComponent);
			XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
			Object sheet = xSpreadsheets.getByName("Sheet1");
			XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
					XSpreadsheet.class, sheet);
			XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
			xCell.setValue(1);
			
			XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xSpreadsheetDocument);
			XNameAccess xStyleFamilies = (XNameAccess) xStyleFamiliesSupplier.getStyleFamilies();
			XNameContainer xPageStyleContainer = (XNameContainer) UnoRuntime.queryInterface(XNameContainer.class, xStyleFamilies.getByName("PageStyles"));
			XNameAccess xPageStyles = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xPageStyleContainer);
			Object xStyle = xPageStyles.getByName("Default");
			XPropertySet xStylePropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStyle);
			Object RightPageHeader = xStylePropertySet.getPropertyValue("RightPageHeaderContent");
			XHeaderFooterContent xRightPageHeader = (XHeaderFooterContent)UnoRuntime.queryInterface(XHeaderFooterContent.class, RightPageHeader);
			
			Object CenterText = xRightPageHeader.getCenterText();
			XText xCenterText = (XText)UnoRuntime.queryInterface(XText.class, CenterText);
			xCenterText.setString("Header Text!\nOpenOffice.org\nVersion 1.1.2");
			
			xStylePropertySet.setPropertyValue("RightPageHeaderContent", RightPageHeader);
			
		}
		catch (com.sun.star.lang.DisposedException e) {
			xRemoteContext = null;
			throw e;			
		}
	}
	
	protected XMultiComponentFactory getRemoteServiceManager(String unoUrl) throws java.lang.Exception {
		if (xRemoteContext == null) {
			XComponentContext xLocalContext =
				com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);
				XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
				
				Object urlResolver = xLocalServiceManager.createInstanceWithContext(
					"com.sun.star.bridge.UnoUrlResolver", xLocalContext );
				XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
					XUnoUrlResolver.class, urlResolver);
				
				Object initialObject = xUnoUrlResolver.resolve(unoUrl);
				XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
					XPropertySet.class, initialObject);
				Object context = xPropertySet.getPropertyValue("DefaultContext");
				xRemoteContext = (XComponentContext)UnoRuntime.queryInterface(
					XComponentContext.class, context);
		}
		return xRemoteContext.getServiceManager();
	}
	
}

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes