Ignore:
Timestamp:
07/09/11 06:42:23 (11 months ago)
Author:
jharrop
Message:

docx4j.properties, supports configuration of default page size, margins, orientation; also ability to set some of the doc props metadata (Application & AppVersion?; dc.creator & dc.lastModifiedBy).

Location:
trunk/docx4j/src/main/java/org/docx4j
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/SaveToZipFile.java

    r1482 r1604  
    4141 
    4242import org.apache.log4j.Logger; 
     43import org.docx4j.Docx4jProperties; 
    4344import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; 
     45import org.docx4j.docProps.core.CoreProperties; 
     46import org.docx4j.docProps.extended.Properties; 
    4447import org.docx4j.jaxb.Context; 
    4548import org.docx4j.jaxb.NamespacePrefixMapperUtils; 
     
    4851import org.docx4j.openpackaging.exceptions.Docx4JException; 
    4952import org.docx4j.openpackaging.packages.OpcPackage; 
     53import org.docx4j.openpackaging.parts.DocPropsCorePart; 
     54import org.docx4j.openpackaging.parts.DocPropsExtendedPart; 
    5055import org.docx4j.openpackaging.parts.Part; 
    5156import org.docx4j.openpackaging.parts.PartName; 
     
    195200                                                 
    196201                        if (part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) { 
     202                                 
     203                                // From docx4j 2.7.1, support setting some basic metadata 
     204                                // from docx4j.properties 
     205                                if (part instanceof DocPropsCorePart) { 
     206                                         
     207                                        boolean dcWrite= Boolean.parseBoolean( 
     208                                                        Docx4jProperties.getProperties().getProperty("docx4j.dc.write", "false")); 
     209                                        if (dcWrite) {                                   
     210                                                CoreProperties cp = ((DocPropsCorePart)part).getJaxbElement(); 
     211                                                 
     212                                                // Only set creator if not already present 
     213                                                String creator= Docx4jProperties.getProperties().getProperty("docx4j.dc.creator.value", "docx4j"); 
     214                                                if (cp.getCreator()==null) { 
     215                                                        org.docx4j.docProps.core.dc.elements.ObjectFactory of = new org.docx4j.docProps.core.dc.elements.ObjectFactory();  
     216                                                        cp.setCreator(of.createSimpleLiteral() ); 
     217                                                        cp.getCreator().getContent().add(creator); 
     218                                                }  
     219                                                 
     220                                                String modifier= Docx4jProperties.getProperties().getProperty("docx4j.dc.lastModifiedBy.value", "docx4j"); 
     221                                                cp.setLastModifiedBy(modifier); 
     222                                        } 
     223                                }                                
     224                                if (part instanceof DocPropsExtendedPart) { 
     225                                        boolean appWrite= Boolean.parseBoolean( 
     226                                                        Docx4jProperties.getProperties().getProperty("docx4j.App.write", "false")); 
     227                                        if (appWrite) { 
     228                                                Properties cp = ((DocPropsExtendedPart)part).getJaxbElement(); 
     229                                                 
     230                                                cp.setApplication(  
     231                                                                Docx4jProperties.getProperties().getProperty("docx4j.Application", "docx4j") 
     232                                                                ); 
     233                                                 
     234                                                String version = Docx4jProperties.getProperties().getProperty("docx4j.AppVersion"); 
     235                                                if ( version!=null ) { 
     236                                                        cp.setAppVersion(version); 
     237                                                } 
     238                                                 
     239                                        } 
     240                                } 
    197241 
    198242                        // Add ZIP entry to output stream. 
     
    218262 
    219263                        } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart) { 
    220  
     264                                 
    221265                        // Add ZIP entry to output stream. 
    222266                        out.putNextEntry(new ZipEntry(zipEntryName));                    
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages/WordprocessingMLPackage.java

    r1567 r1604  
    3939 
    4040import org.apache.log4j.Logger; 
     41import org.docx4j.Docx4jProperties; 
    4142import org.docx4j.XmlUtils; 
    4243import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; 
     
    385386         
    386387 
     388        /** 
     389         * Creates a WordprocessingMLPackage, using default page size and orientation. 
     390         * From 2.7.1, these are read from docx4j.properties, or if not found, default 
     391         * to A4 portrait. 
     392         */ 
    387393        public static WordprocessingMLPackage createPackage() throws InvalidFormatException { 
    388  
    389                 return createPackage(PageSizePaper.A4, false);  
     394                 
     395                String papersize= Docx4jProperties.getProperties().getProperty("docx4j.PageSize", "A4"); 
     396                log.info("Using paper size: " + papersize); 
     397                 
     398                String landscapeString = Docx4jProperties.getProperties().getProperty("docx4j.PageOrientationLandscape", "false"); 
     399                boolean landscape= Boolean.parseBoolean(landscapeString); 
     400                log.info("Landscape orientation: " + landscape); 
     401                 
     402                return createPackage( 
     403                                PageSizePaper.valueOf(papersize), landscape);  
    390404        } 
    391405         
     
    437451                        log.error(e); 
    438452                } 
     453                 
     454                // Metadata: docx4j 2.7.1 can populate some of this from docx4j.properties 
     455                DocPropsCorePart core = new DocPropsCorePart(); 
     456                org.docx4j.docProps.core.ObjectFactory coreFactory = new org.docx4j.docProps.core.ObjectFactory(); 
     457                core.setJaxbElement(coreFactory.createCoreProperties() ); 
     458                wmlPack.addTargetPart(core); 
     459                 
     460                DocPropsExtendedPart app = new DocPropsExtendedPart(); 
     461                org.docx4j.docProps.extended.ObjectFactory extFactory = new org.docx4j.docProps.extended.ObjectFactory(); 
     462                app.setJaxbElement(extFactory.createProperties() ); 
     463                wmlPack.addTargetPart(app);              
     464                 
    439465                // Return the new package 
    440466                return wmlPack; 
Note: See TracChangeset for help on using the changeset viewer.