您的位置:首页 > 运维架构

OpenOffice.org Code Snippets--Accessing document properties

2007-05-15 23:49 351 查看
Recently, I had the task to access document properties before converting a .xls Excel document to a .pdf document. Because , I couldn't find any code-snippets, I'm posting the code here, might be useful to other people.

Code:
Reference< XStyleFamiliesSupplier > xSupplier;
Reference< XNameAccess > xFamilies;
Reference< XNameContainer > xFamily;

// set string for retrieval of OO styles
OUString strPageStyleName( OUString::createFromAscii( "PageStyles") );

Reference< XSpreadsheetDocument > xSSDocument( xComponent, UNO_QUERY );
Reference< XTextDocument > xTextDocument( xComponent, UNO_QUERY );
Reference< XDrawPagesSupplier > xDrawPagesSupplier( xComponent, UNO_QUERY );

if ( xSSDocument.is() )
{
xSupplier = Reference<XStyleFamiliesSupplier>( xSSDocument,UNO_QUERY );
}
else if ( xTextDocument.is() )
{
// TextDocuments
xSupplier = Reference< XStyleFamiliesSupplier >( xTextDocument,UNO_QUERY );
}
else if ( xDrawPagesSupplier.is() )
{
// DrawingDocuments
Reference< XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();

if ( xDrawPages.is() )
{
// get the page count for standard pages
int nPageCount = xDrawPages->getCount();

for( int i = 0; i < nPageCount; i++ )
{
Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY );

if ( xDrawPage.is() )
{
Reference< XPropertySet > xStyleProps( xDrawPage, UNO_QUERY );

if ( xStyleProps.is() )
{
Any Width = xStyleProps->getPropertyValue(OUString::createFromAscii("Width"));
Any Height = xStyleProps->getPropertyValue(OUString::createFromAscii("Height"));

//do changes
xStyleProps->setPropertyValue(OUString::createFromAscii("Width"), Width);
xStyleProps->setPropertyValue(OUString::createFromAscii("Height"), Height);

}
}
}

}
}

// process doc families

if ( xSupplier.is() )
{
xFamilies = Reference< XNameAccess >( xSupplier->getStyleFamilies(),UNO_QUERY );
if ( xFamilies.is() )
{
xFamily = Reference< XNameContainer >( xFamilies->getByName( strPageStyleName ), UNO_QUERY );
if ( xFamily.is() )
{
Sequence<OUString> rNames = xFamily->getElementNames();

// loop trough all page-properties and setup styles where needed

for( int i = 0; i < rNames.getLength(); i++ )
{
Reference< XStyle > xStyle( xFamily->getByName( rNames[i] ), UNO_QUERY );
if ( ! xStyle.is() )
continue;

Reference< XPropertySet > xStyleProps(xStyle, UNO_QUERY);
if ( ! xStyleProps.is() )
continue;

Any Width = xStyleProps->getPropertyValue(OUString::createFromAscii("Width"));
Any Height = xStyleProps->getPropertyValue(OUString::createFromAscii("Height"));

// do changes here...

xStyleProps->setPropertyValue(OUString::createFromAscii("Width"), Width);
xStyleProps->setPropertyValue(OUString::createFromAscii("Height"), Height);
}
}
}
}

Now, you'll notice the else if blocks, where I check for different document-types . Three document types are checked: TextDocument, SpreadSheets and DrawingDocument .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: