Dear all,

Thanks to the input from Ian, I managed to reuse the styles as follows 

As the method of ODFDocument.loadTemplate () is protected and it takes 
Resources Object,  I had to write a special class to inherit from 
OdtTextDocument to create a new method called loadTemplate and takes 
InputStream

This is worked fine for me when applying it to "Heading 1" and other 
Headings.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;

import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.incubator.doc.text.OdfTextParagraph;
import org.odftoolkit.odfdom.pkg.OdfPackage;
import org.odftoolkit.odfdom.pkg.OdfPackageDocument;
import org.xml.sax.SAXException;


/**
 * Creating class TextWorkProduct.org
 * 
 * @author aibrahim
 * 
 *         Created at: Apr 8, 2014 - 12:35:30 AM
 */
public class TextWorkProduct extends OdfTextDocument {

        protected TextWorkProduct(OdfPackage pkg, String internalPath, 
OdfMediaType odfMediaType) throws SAXException {
                super(pkg, internalPath, odfMediaType);
        }
 
        private static InputStream getTemplate (String templatePath) 
throws FileNotFoundException{
                if (templatePath == null)
                        return null;
 
                File templateFile = new File (templatePath);
                return getTemplate (templateFile);
        }
 
        private static InputStream getTemplate (File templateFile) throws 
FileNotFoundException{
                if (templateFile == null)
                        return null;
 
                InputStream templateStream;
                templateStream = new FileInputStream(templateFile);
                return templateStream;
        }
 
        /**
         * Loads the ODF root document from the given Resource.
         * 
         * NOTE: Initial meta data (like the document creation time) will 
be added in this method.
         * 
         * @param res a resource containing a package with a root document
         * @param odfMediaType the media type of the root document
         * @return the OpenDocument document
         *                or NULL if the media type is not supported by 
ODFDOM.
         * @throws java.lang.Exception - if the document could not be 
created.
         */
        protected static OdfDocument loadTemplate(InputStream 
templateStream, OdfMediaType odfMediaType) throws Exception {
                OdfPackage pkg = null;
                try {
                        pkg = OdfPackage.loadPackage(templateStream);
                } finally {
                        templateStream.close();
                }
 
                OdfDocument newDocument = new TextWorkProduct(pkg, 
ROOT_DOCUMENT_PATH, odfMediaType);
 
                //add creation time, the metadata have to be explicitly 
set
                Calendar aCreationDate = Calendar.getInstance();
                // set creation date´
                if (aCreationDate != null) {
 newDocument.getOfficeMetadata().setCreationDate(aCreationDate);
                }
                return newDocument;
        }


        /**
         * Create a text work product document from the given template 
file. If the template is null
         * then a default document will be created.
         * 
         * @param templatePath
         * @return
         * @throws Exception
         */
        public static TextWorkProduct newTextWorkProduct(String 
templatePath) throws WorkProductGenerationException {
                InputStream templateStream = null;
                TextWorkProduct aDocument = null;
 
                try {
                        templateStream = getTemplate (templatePath);
 
                        if (templateStream != null)
                        aDocument = (TextWorkProduct) 
loadTemplate(templateStream, OdfTextDocument.OdfMediaType.TEXT);
 
                } catch (Exception exception) {
                        //Failed to load the template then we create a 
blank document.
                        fLogger.logError("Failed create document from 
template: " + templatePath, exception);
                        try {
                                aDocument = 
(TextWorkProduct)OdfTextDocument.newTextDocument();
                        } catch (Exception e) {
                                throw new WorkProductGenerationException 
("Failed create document from template: " + templatePath, exception);
                        }
                }
 
                return aDocument;
        }
 
        public void addHeader (String header, int level) throws 
WorkProductGenerationException{
                try {
                        OdfTextParagraph aHeader = newParagraph();
                        // aHeader.addContent(header);
                        // aHeader.setStyleName("Heading 1");
                        aHeader.addStyledContent("Heading " + level, 
header);
                } catch (Exception exception) {
                        throw new WorkProductGenerationException ("Failed 
to create a header: " + header, exception);
                }
 
        }
 
        public void addParagraph (String paragaraphText)  throws Exception 
{
                try {
                        newParagraph(paragaraphText);
                } catch (Exception exception) {
                        throw new Exception ("Failed to create a 
paragraph: " + paragaraphText, exception);
                }
 
        }
 
        public void includeFile (String filePath) throws Exception {
                OdfPackageDocument newDocument;
                try {
                        newDocument = 
OdfPackageDocument.loadDocument(filePath);
                        insertDocument(newDocument, "");
                } catch (Exception exception) {
                        throw new Exception ("Failed to include the file: 
" + filePath, exception);
                }
        }

}

The question now, where can I add this code so it will be available for 
every body as a sample code? Is there some community other than this 
mailing list? 

I have a long list of other questions, is there some repository for the 
questions to review before I submit these questions here?



Thanks & Best Regards / السلام عليكم 

Ahmed Ibrahim
MBA ,Senior IT Architect, SWG Cairo Lab Services
BPM Client Solution Manager, MEA 
IT Architect Egypt Profession Lead
Mobile: +20 100 1615 506



From:   Ian C <[email protected]>
To:     "[email protected]" <[email protected]>, 

Date:   03/30/2014 01:35 PM
Subject:        Re: How to reuse styles from input template document? No 
answer till now :(
Sent by:        [email protected]



Hi Ahmed,

have you tried the loadTemplate function of the ODFDocument class in the
ODFDom?

Then look at the the setStyleName function of a Paragraph.

So if you create a document from your template then get/created paragraphs
call the setStyleName if "Heading1" and save.

Then open the document is OpenOffice and see that it looks like you 
expect?

Good luck.


On Sat, Mar 29, 2014 at 9:39 PM, Ahmed I Ibrahim 
<[email protected]>wrote:

> Dear all
>
> I am in the middle of generating an important report and I really need a
> help here.
>
> How to reuse styles create in a template file in the generated document. 
I
> am trying to get my way in the odf-simple apis but no luck. I hope some
> one can help me in this.
>
> Below is the details of my question again. I tried to sample demos, as
> well as the cookbook with no luck. If you have some documentation that I
> can use, that will be great.
>
> Thanks & Best Regards / السلام عليكم
>
> Ahmed Ibrahim
> MBA ,Senior IT Architect, SWG Cairo Lab Services
> BPM Client Solution Manager, MEA
> IT Architect Egypt Profession Lead
> Mobile: +20 100 1615 506
>
>
>
> From:   Ahmed I Ibrahim/Egypt/IBM@IBMEG
> To:     [email protected],
> Date:   03/27/2014 01:16 AM
> Subject:        How to reuse styles from input template document?
>
>
>
> Dear all,
>
> I am trying to do the following:
>
> I am trying to create a document doc.odt from a template of another
> document template.odt
>
> The template contains some custom styles for Heading 1, 2, and 3 as well
> as Style for a table and chart.
>
> For Heading 1, when I use
>  para1.applyHeading(true, 1);
>
> It is not reuse the heading 1 style from the template. While I use 
setting
>
> the font it is applied.
>
> It is the same problem for the rest of the styles.
>
> Am, I missing some parts here? How can I reuse the styles (that I 
defined
> by name) from the template in the generated text document?
>
> I am stucked and I appreciate your help here.
>
>
> Thanks & Best Regards / السلام عليكم
>
> Ahmed Ibrahim
> MBA ,Senior IT Architect, SWG Cairo Lab Services
> BPM Client Solution Manager, MEA
> IT Architect Egypt Profession Lead
> Mobile: +20 100 1615 506
>
>
>


-- 
Cheers,

Ian C


Reply via email to