Versão com a implementação da classe estilos

parent 27bb600a
......@@ -64,9 +64,9 @@ import mprj.doerj.caderno.util.GeraCaderno;
"javax.portlet.expiration-cache=0",
"javax.portlet.init-param.portlet-title-based-navigation=true",
"javax.portlet.init-param.template-path=/",
"javax.portlet.info.title=Caderno do Diário Oficial",
"javax.portlet.info.short-title=Caderno do Diário Oficial",
"javax.portlet.info.keywords=Caderno do Diário Oficial",
"javax.portlet.info.title=Caderno do Dirio Oficial",
"javax.portlet.info.short-title=Caderno do Dirio Oficial",
"javax.portlet.info.keywords=Caderno do Dirio Oficial",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.init-param.config-template=/configuration.jsp",
......
package mprj.doerj.caderno.util;
import java.math.BigInteger;
import java.util.List;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.Style;
import org.docx4j.wml.Styles;
import org.docx4j.wml.U;
import org.docx4j.wml.UnderlineEnumeration;
public class Estilos {
private static WordprocessingMLPackage wordMLPackage;
/**
* First we create the package, then we alter the style sheet and add some
* styled paragraphs. Finally we save the package.
*/
public static void main (String[] args) throws Docx4JException {
wordMLPackage = WordprocessingMLPackage.createPackage();
alterStyleSheet();
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title",
"Hello World! This title is now in Arial.");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",
"Subtitle, this subtitle is now Arial too");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1",
"As is Heading1");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading2",
"Heading2 is now Arial, no longer bold and has an underline " +
"and fontsize 12");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading3",
"Heading3 is now Arial");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Normal",
"And normal text has changed to Arial and fontsize 10");
wordMLPackage.save(new java.io.File("C:/Users/diogo.souza/Documents/Caderno DO/HelloWord12.docx") );
}
/**
* This method alters the default style sheet that is part of each document.
*
* To do this, we first retrieve the style sheet from the package and then
* get the Styles object from it. From this object, we get the list of actual
* styles and iterate over them.
* We check against all styles we want to alter and apply the alterations if
* applicable.
*
* @param wordMLPackage
*/
public static void alterStyleSheet() {
StyleDefinitionsPart styleDefinitionsPart =
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();
Styles styles = styleDefinitionsPart.getJaxbElement();
List<Style> stylesList = styles.getStyle();
for (Style style : stylesList) {
if (style.getStyleId().equals("Normal")) {
alterNormalStyle(style);
} else if (style.getStyleId().equals("Heading2")) {
alterHeading2Style(style);
} else if (style.getStyleId().equals("Heading1") ||
style.getStyleId().equals("Heading3") ||
style.getStyleId().equals("Title") ||
style.getStyleId().equals("Subtitle")) {
getRunPropertiesAndRemoveThemeInfo(style);
}
}
}
public static void alterStyleSheet(WordprocessingMLPackage wordPackage) {
StyleDefinitionsPart styleDefinitionsPart =
wordPackage.getMainDocumentPart().getStyleDefinitionsPart();
Styles styles = styleDefinitionsPart.getJaxbElement();
List<Style> stylesList = styles.getStyle();
for (Style style : stylesList) {
if (style.getStyleId().equals("Normal")) {
alterNormalStyle(style);
} else if (style.getStyleId().equals("Heading2")) {
alterHeading2Style(style);
} else if (style.getStyleId().equals("Heading1")){
alterHeading1Style(style);
}else if (style.getStyleId().equals("Heading3") ||
style.getStyleId().equals("Title") ||
style.getStyleId().equals("Subtitle")){
getRunPropertiesAndRemoveThemeInfo(style);
}
}
}
/**
* First we create a run properties object as we want to remove nearly all of
* the existing styling. Then we change the font and font size and set the
* run properties on the given style. As in previous examples, the font size
* is defined to be in half-point size.
*/
private static void alterNormalStyle(Style style) {
// we want to change (or remove) almost all the run properties of the
// normal style, so we create a new one.
RPr rpr = new RPr();
changeFontToSpranq(rpr);
changeFontSize(rpr, 20);
style.setRPr(rpr);
}
/**
* For this style, we get the existing run properties from the style and
* remove the theme font information from them. Then we also remove the bold
* styling, change the font size (half-points) and add an underline.
*/
private static void alterHeading2Style(Style style) {
RPr rpr = getRunPropertiesAndRemoveThemeInfo(style);
removeBoldStyle(rpr);
changeFontSize(rpr, 24);
// addUnderline(rpr);
}
private static void alterHeading1Style(Style style) {
RPr rpr = getRunPropertiesAndRemoveThemeInfo(style);
removeBoldStyle(rpr);
changeFontSize(rpr, 28);
// addUnderline(rpr);
}
private static RPr getRunPropertiesAndRemoveThemeInfo(Style style) {
// We only want to change some settings, so we get the existing run
// properties from the style.
RPr rpr = style.getRPr();
removeThemeFontInformation(rpr);
return rpr;
}
/**
* Change the font of the given run properties to Arial.
*
* A run font specifies the fonts which shall be used to display the contents
* of the run. Of the four possible types of content, we change the styling of
* two of them: ASCII and High ANSI.
* Finally we add the run font to the run properties.
*
* @param runProperties
*/
private static void changeFontToSpranq(RPr runProperties) {
RFonts runFont = new RFonts();
runFont.setAscii("Spranq eco sans");
runFont.setHAnsi("Spranq eco sans");
runProperties.setRFonts(runFont);
}
/**
* Change the font size of the given run properties to the given value.
*
* @param runProperties
* @param fontSize Twice the size needed, as it is specified as half-point value
*/
private static void changeFontSize(RPr runProperties, int fontSize) {
HpsMeasure size = new HpsMeasure();
size.setVal(BigInteger.valueOf(fontSize));
runProperties.setSz(size);
runProperties.setColor(null);
RFonts runFont = new RFonts();
runFont.setAscii("Spranq eco sans");
runFont.setHAnsi("Spranq eco sans");
runProperties.setRFonts(runFont);
}
/**
* Removes the theme font information from the run properties.
* If this is not removed then the styles based on the normal style won't
* inherit the Arial font from the normal style.
*
* @param runProperties
*/
private static void removeThemeFontInformation(RPr runProperties) {
runProperties.getRFonts().setAsciiTheme(null);
runProperties.getRFonts().setHAnsiTheme(null);
}
/**
* Removes the Bold styling from the run properties.
*
* @param runProperties
*/
private static void removeBoldStyle(RPr runProperties) {
runProperties.getB().setVal(false);
}
/**
* Adds a single underline to the run properties.
*
* @param runProperties
*/
private static void addUnderline(RPr runProperties) {
U underline = new U();
underline.setVal(UnderlineEnumeration.SINGLE);
runProperties.setU(underline );
}
}
......@@ -64,7 +64,7 @@ public class GeraCaderno {
//System.out.println(XmlUtils.marshaltoString(templatePackage.getMainDocumentPart().getJaxbElement(), true, true));
WordprocessingMLPackage acumuladoPackage = WordprocessingMLPackage.createPackage();
//Preenche os dados do cabeçalho
//Preenche os dados do cabealho
MainDocumentPart headerDocumentPart = templatePackage.getMainDocumentPart();
......@@ -83,7 +83,7 @@ public class GeraCaderno {
//Cria o hash map com as variáveis para substuição dos dados dos cabeçalhos e expediente
//Cria o hash map com as variveis para substuio dos dados dos cabealhos e expediente
HashMap<String, String> variables = new HashMap<String, String>();
variables.put("edicao", edicao);
variables.put("disponibilizacao", disponibilizacao);
......@@ -171,13 +171,13 @@ public class GeraCaderno {
mainPart.variableReplace(variables);
//AINDA FALTA IMPLEMENTAR A CLASSE Estilos estilo = new Estilos();
//AINDA FALTA IMPLEMENTAR A CLASSE estilo.alterStyleSheet(acumuladoPackage);
Estilos estilo = new Estilos();
estilo.alterStyleSheet(acumuladoPackage);
WordprocessingMLPackage firstPagePackage = WordprocessingMLPackage.createPackage();
boolean firstPage=true;
//faz a leitura da listagem de todos os conteúdos recebidos pelo serviço Rest
//faz a leitura da listagem de todos os contedos recebidos pelo servio Rest
for(int i=0;i<listaConteudos.length();i++){
WordprocessingMLPackage wordCurrentPackage = WordprocessingMLPackage.createPackage();
......@@ -207,7 +207,7 @@ public class GeraCaderno {
MainDocumentPart acumuladoDocumentPart = acumuladoPackage.getMainDocumentPart();
//AINDA FALTA IMPLEMANTAR A CLASSE estilo.alterStyleSheet(templatePackage);
estilo.alterStyleSheet(templatePackage);
templatePackage.getMainDocumentPart().getContent().addAll(acumuladoDocumentPart.getContent());
......@@ -220,7 +220,7 @@ public class GeraCaderno {
// InputStream nomeArquivoInputStream = getClass().getResourceAsStream("/META-INF/resources/template/"+nomeArquivo);
// File exportFile = new File(nomeArquivoInputStream.toString());
//cria o arquivo temporário que será utilizado na resposta da requisição
//cria o arquivo temporrio que ser utilizado na resposta da requisio
File exportFile = File.createTempFile("caderno_doerj_tmp_", ".docx");
try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment