Storing Documents

From Apache OpenOffice Wiki
Jump to: navigation, search



After loading an office component successfully, the returned interface is used to manipulate the component. Document specific interfaces, such as the interfaces com.sun.star.text.XTextDocument, com.sun.star.sheet.XSpreadsheetDocument or com.sun.star.drawing.XDrawPagesSupplier are retrieved using queryInterface().

If the office component supports the com.sun.star.frame.XStorable interface applying to every component implementing the service com.sun.star.document.OfficeDocument, it can be stored:

  void store ( ) 
  void storeAsURL ( [in] string sURL, 
                    [in] sequence< com::sun::star::beans::PropertyValue > lArguments ) 
  void storeToURL ( [in] string sURL, 
                    [in] sequence< com::sun::star::beans::PropertyValue > lArguments ) 
  boolean hasLocation ()
  string getLocation ()
  boolean isReadonly ()

The XStorable offers the methods store(), storeAsURL() and storeToURL() for storing. The latter two methods are called with a media descriptor.

The method store() overwrites an existing file. Calling this method on a document that was created from scratch using a private:factory/... URL leads to an exception.

The other two methods storeAsURL() and storeToURL() leave the original file untouched and differ after the storing procedure. The storeToURL() method saves the current document to the desired location without touching the internal state of the document. The method storeAsURL sets the Modified attribute of the document, accessible through its com.sun.star.util.XModifiable interface, to false and updates the internal media descriptor of the document with the parameters passed in the call. This changes the document URL.

The following example exports a Writer document, Writer/Web document or Calc sheet to HTML.

  // Conditions: sURL      = "file:///home/target.htm" 
  //             xDocument = m_xLoadedDocument  
  // Export can be achieved by saving the document and using 
  // a special filter which can write the desired format. 
  // Normally this filter should be searched inside the filter 
  // configuration (using service com.sun.star.document.FilterFactory) 
  // but here we use well known filter names directly. 
  String sFilter = null;
 
  // Detect document type by asking XServiceInfo
   com.sun.star.lang.XServiceInfo xInfo = 
  (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface ( 
   com.sun.star.lang.XServiceInfo.class, xDocument); 
  // Determine suitable HTML filter name for export.
  if(xInfo!=null) 
  { 
  if(xInfo.supportsService ("com.sun.star.text.TextDocument") == true) 
      sFilter = new String("HTML (StarWriter)"); 
  else 
  if(xInfo.supportsService ("com.sun.star.text.WebDocument") == true) 
      sFilter = new String("HTML"); 
  else 
  if(xInfo.supportsService ("com.sun.star.sheet.SpreadsheetDocument") == true) 
      sFilter = new String("HTML (StarCalc)");
  } 
  if(sFilter!=null) 
  { 
  // Build necessary argument list for store properties. 
  // Use flag "Overwrite" to prevent exceptions, if file already exists. 
  com.sun.star.beans.PropertyValue[] lProperties = 
      new com.sun.star.beans.PropertyValue[2]; 
  lProperties[0]       = new com.sun.star.beans.PropertyValue(); 
  lProperties[0].Name  = "FilterName"; 
  lProperties[0].Value = sFilter; 
  lProperties[1]       = new com.sun.star.beans.PropertyValue(); 
  lProperties[1].Name  = "Overwrite"; 
  lProperties[1].Value = new Boolean(true);  
 
   com.sun.star.frame.XStorable xStore = 
  (com.sun.star.frame.XStorable)UnoRuntime.queryInterface (
        com.sun.star.frame.XStorable.class, xDocument); 
   xStore.storeAsURL (sURL, lProperties); 
  }

If a model is loaded or stored successfully, all parts of the media descriptor not explicitly excluded according to the media descriptor table in section MediaDescriptor must be provided by the methods getURL() and getArgs() in the com.sun.star.frame.XModel interface. The separation of the URL and the other arguments is used, because the URL is often the most wanted part for its performance optimized access.

Tip.png The XModel offers a method attachResource() that changes the media descriptor of the document, but this method should only be used in special cases, for example, by the implementer of a new document model and controller. The method attachResource() does not force reloading of the document. Validation checks are done when a document is loaded through MediaDescriptor. For example, if the resource is write protected, add Readonly to the MediaDescriptor and the filter name must match the data. A possible use for attachResource() could be creating a document from a template, where after loading successfully, the document's resource is changed to an "unnamed" state by deleting the URL.


Printing Documents

Printing revolves around the interface com.sun.star.view.XPrintable. Its methods and special printing features for the various document types are described in the document chapters Printing Text Documents, Printing Spreadsheet Documents, Printing Drawing Documents and Printing Presentation Documents.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages