net.sf.jasperreports.engine.export.ooxml.DocxParagraphHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasperreports Show documentation
Show all versions of jasperreports Show documentation
Free Java Reporting Library
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.export.ooxml;
import java.io.Writer;
import net.sf.jasperreports.engine.JRParagraph;
import net.sf.jasperreports.engine.JRPrintImage;
import net.sf.jasperreports.engine.JRPrintText;
import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.TabStop;
import net.sf.jasperreports.engine.export.LengthUtil;
import net.sf.jasperreports.engine.type.HorizontalImageAlignEnum;
import net.sf.jasperreports.engine.type.HorizontalTextAlignEnum;
import net.sf.jasperreports.engine.type.TabStopAlignEnum;
/**
* @author Sanda Zaharia ([email protected])
*/
public class DocxParagraphHelper extends BaseHelper
{
/**
*
*/
private static final String HORIZONTAL_ALIGN_LEFT = "left";
private static final String HORIZONTAL_ALIGN_RIGHT = "right";
private static final String HORIZONTAL_ALIGN_CENTER = "center";
private static final String HORIZONTAL_ALIGN_BOTH = "both";
/**
*
*/
private static final String TAB_STOP_ALIGN_LEFT = "left";
private static final String TAB_STOP_ALIGN_RIGHT = "right";
private static final String TAB_STOP_ALIGN_CENTER = "center";
protected static final int LINE_SPACING_FACTOR = 240; //(int)(240 * 2/3f);
/**
*
*/
private boolean pageBreak;
/**
*
*/
public DocxParagraphHelper(JasperReportsContext jasperReportsContext, Writer writer, boolean pageBreak)
{
super(jasperReportsContext, writer);
this.pageBreak = pageBreak;
}
/**
*
*/
public void exportProps(JRStyle style)
{
exportPropsHeader(null, style.getParagraph());
exportAlignment(
getHorizontalTextAlign(
style.getOwnHorizontalTextAlign()
)
);
exportTabStops(
style.getParagraph()
);
exportLineSpacing(
style.getParagraph()
);
exportPropsFooter();
}
/**
*
*/
public void exportProps(JRPrintText text)
{
JRStyle baseStyle = text.getDefaultStyleProvider().getStyleResolver().getBaseStyle(text);
exportPropsHeader(baseStyle == null ? null : baseStyle.getName(), text.getParagraph()); //javadoc says getStyleNameReference is not supposed to work for print elements
exportAlignment(
getHorizontalTextAlign(
text.getOwnHorizontalTextAlign()
)
);
exportTabStops(
text.getParagraph()
);
exportLineSpacing(
text.getParagraph()
);
// exportRunDirection(text.getRunDirection() == JRPrintText.RUN_DIRECTION_RTL ? "rl" : null);
exportPropsFooter();
}
/**
*
*/
public void exportProps(JRPrintImage image)
{
JRStyle baseStyle = image.getDefaultStyleProvider().getStyleResolver().getBaseStyle(image);
exportPropsHeader(baseStyle == null ? null : baseStyle.getName(), null);
exportAlignment(
getHorizontalImageAlign(
image.getOwnHorizontalImageAlign()
)
);
write(" \n");
exportPropsFooter();
}
/**
*
*/
private void exportPropsHeader(String styleNameReference, JRParagraph paragraph)
{
write(" \n");
if (styleNameReference != null)
{
write(" \n");
}
if(paragraph != null)
{
write(" \n");
}
if (pageBreak)
{
write(" \n");
pageBreak = false;
}
}
/**
*
*/
private void exportAlignment(String horizontalAlignment)
{
if (horizontalAlignment != null)
{
write(" \n");
}
}
/**
*
*/
private void exportTabStops(JRParagraph paragraph)
{
TabStop[] tabStops = paragraph.getTabStops();
if (tabStops != null && tabStops.length > 0)
{
write(" \n");
for (int i = 0; i < tabStops.length; i++)
{
TabStop tabStop = tabStops[i];
write(" \n");
}
write(" \n");
}
}
/**
*
*/
private void exportLineSpacing(JRParagraph paragraph)
{
if (
paragraph.getOwnLineSpacing() != null
|| paragraph.getOwnLineSpacingSize() != null
|| paragraph.getOwnSpacingBefore() != null
|| paragraph.getOwnSpacingAfter() != null
)
{
String lineRule;
String lineSpacing;
switch (paragraph.getLineSpacing())
{
case AT_LEAST :
{
lineRule = "atLeast";
lineSpacing = String.valueOf(LengthUtil.twip(paragraph.getLineSpacingSize()));
break;
}
case FIXED :
{
lineRule = "exact";
lineSpacing = String.valueOf(LengthUtil.twip(paragraph.getLineSpacingSize()));
break;
}
case PROPORTIONAL :
{
lineRule = "auto";
lineSpacing = String.valueOf((int)(paragraph.getLineSpacingSize() * LINE_SPACING_FACTOR));
break;
}
case DOUBLE :
{
lineRule = "auto";
lineSpacing = String.valueOf((int)(2f * LINE_SPACING_FACTOR));
break;
}
case ONE_AND_HALF :
{
lineRule = "auto";
lineSpacing = String.valueOf((int)(1.5f * LINE_SPACING_FACTOR));
break;
}
case SINGLE :
default :
{
lineRule = "auto";
lineSpacing = String.valueOf((int)(1f * LINE_SPACING_FACTOR));
}
}
write(" \n");
}
}
/**
*
*/
private void exportPropsFooter()
{
write(" \n");
}
/**
*
*/
public void exportEmptyParagraph()
{
exportEmptyParagraph(false, 0l, null);
}
/**
*
*/
public void exportEmptyParagraph(boolean startPage, long bookmarkIndex, String pageAnchor)
{
write(" \n");
if (pageBreak)
{
write(" \n");
pageBreak = false;
}
write(" ");
if(startPage)
{
write(" ");
}
write(" \n");
}
/**
*
*/
public static String getHorizontalTextAlign(HorizontalTextAlignEnum horizontalAlignment)
{
if (horizontalAlignment != null)
{
switch (horizontalAlignment)
{
case RIGHT :
return HORIZONTAL_ALIGN_RIGHT;
case CENTER :
return HORIZONTAL_ALIGN_CENTER;
case JUSTIFIED :
return HORIZONTAL_ALIGN_BOTH;
case LEFT :
default :
return HORIZONTAL_ALIGN_LEFT;
}
}
return null;
}
/**
*
*/
public static String getHorizontalImageAlign(HorizontalImageAlignEnum horizontalAlignment)
{
if (horizontalAlignment != null)
{
switch (horizontalAlignment)
{
case RIGHT :
return HORIZONTAL_ALIGN_RIGHT;
case CENTER :
return HORIZONTAL_ALIGN_CENTER;
case LEFT :
default :
return HORIZONTAL_ALIGN_LEFT;
}
}
return null;
}
/**
*
*/
public static String getTabStopAlignment(TabStopAlignEnum alignment)
{
if (alignment != null)
{
switch (alignment)
{
case RIGHT :
return TAB_STOP_ALIGN_RIGHT;
case CENTER :
return TAB_STOP_ALIGN_CENTER;
case LEFT :
default :
return TAB_STOP_ALIGN_LEFT;
}
}
return null;
}
/**
*
*/
public void exportEmptyPage(String pageAnchor, long bookmarkIndex, boolean twice)
{
if(twice)
{
write(" \n");
}
write(" \n");
write(" \n");
}
}