Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.classpath
/target
/.repository/
.idea
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public class DocxDocumentHandler
private boolean underlining;

private boolean striking;

private boolean subscripting;

private boolean superscripting;

private Stack<ContainerProperties> paragraphsStack;

private Stack<SpanProperties> spansStack;
Expand Down Expand Up @@ -231,6 +231,7 @@ public void handleString( String content )
boolean strike = striking;
boolean subscript = subscripting;
boolean superscript = superscripting;
String color = null;
SpanProperties properties = getCurrentSpanProperties();
if ( properties != null )
{
Expand Down Expand Up @@ -259,17 +260,21 @@ public void handleString( String content )
{
superscript = properties.isSuperscript();
}
if (null == color)
{
color = properties.getColor();
}
}
super.write( "<w:r>" );
// w:RP
processRunProperties( false, bold, italic, underline, strike, subscript, superscript );
processRunProperties( false, bold, italic, underline, strike, subscript, superscript, color );
// w:br
for ( int i = 0; i < addLineBreak; i++ )
{
super.write( "<w:br/>" );
}
addLineBreak = 0;
if(!content.isEmpty())
if(!content.isEmpty())
{
// w:t
super.write( "<w:t xml:space=\"preserve\" >" );
Expand All @@ -281,16 +286,20 @@ public void handleString( String content )
}

private void processRunProperties( boolean isInsidePPr, boolean bold, boolean italics, boolean underline,
boolean strike, boolean subscript, boolean superscript )
boolean strike, boolean subscript, boolean superscript, String color )
throws IOException
{
if ( bold || italics || underline || strike || subscript || superscript )
if ( bold || italics || underline || strike || subscript || superscript || color != null )
{
if ( isInsidePPr )
{
startPPrIfNeeded();
}
super.write( "<w:rPr>" );
if (color != null)
{
super.write("<w:color w:val=\""+ color +"\"/>");
}
if ( bold )
{
super.write( "<w:b />" );
Expand Down Expand Up @@ -408,7 +417,7 @@ public void startListItem( ListItemProperties properties )

/**
* Generate wpPr docx element.
*
*
* @param properties
* @param pStyle
* @param isList
Expand Down Expand Up @@ -473,7 +482,7 @@ private void processParagraphProperties( ContainerProperties properties, String
}
// rPPr
processRunProperties( true, properties.isBold(), properties.isItalic(), properties.isUnderline(),
properties.isStrike(), properties.isSubscript(), properties.isSuperscript() );
properties.isStrike(), properties.isSubscript(), properties.isSuperscript(), properties.getColor() );
}

endPPrIfNeeded();
Expand Down Expand Up @@ -563,7 +572,7 @@ public void endSpan()
protected void doStartOrderedList( ListProperties properties )
throws IOException
{
if(this.addLineBreak>0)
if(this.addLineBreak>0)
{
handleString("");
}
Expand All @@ -583,7 +592,7 @@ protected void doStartOrderedList( ListProperties properties )
protected void doStartUnorderedList( ListProperties properties )
throws IOException
{
if(this.addLineBreak>0)
if(this.addLineBreak>0)
{
handleString("");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static Map<String, String> parse( String style )

/**
* Create {@link ParagraphProperties} from inline style.
*
*
* @param style
* @return
*/
Expand All @@ -96,7 +96,7 @@ public static ParagraphProperties createParagraphProperties( String style )

/**
* Create {@link HeaderProperties} from inline style.
*
*
* @param style
* @return
*/
Expand All @@ -114,7 +114,7 @@ public static HeaderProperties createHeaderProperties( String style )

/**
* Create {@link ListItemProperties} from inline style.
*
*
* @param style
* @return
*/
Expand All @@ -132,7 +132,7 @@ public static ListItemProperties createListItemProperties( String style )

/**
* Create {@link ListProperties} from inline style.
*
*
* @param style
* @return
*/
Expand All @@ -150,7 +150,7 @@ public static ListProperties createListProperties( String style )

/**
* Create {@link SpanProperties} from inline style.
*
*
* @param style
* @return
*/
Expand Down Expand Up @@ -204,6 +204,13 @@ private static void processContainerproperties( ContainerProperties properties,
}
}

// color
String color = stylesMap.get( "color" );
if ( color != null )
{
properties.setColor(color);
}

// text-decoration
String textDecoration = stylesMap.get( "text-decoration" );
properties.setStrike( false );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public enum ContainerType

private final ContainerType type;

private String color;

public ContainerProperties( ContainerType type )
{
this.type = type;
Expand All @@ -67,6 +69,15 @@ public ContainerType getType()
return type;
}

public String getColor() {
return color;
}

public ContainerProperties setColor(String color) {
this.color = color;
return this;
}

public boolean isPageBreakBefore()
{
return pageBreakBefore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Logger;

import org.apache.poi.xwpf.converter.core.BorderSide;
Expand Down Expand Up @@ -107,6 +109,9 @@
import fr.opensagres.xdocreport.itext.extension.ExtendedPdfPTable;
import fr.opensagres.xdocreport.itext.extension.IITextContainer;
import fr.opensagres.xdocreport.itext.extension.font.FontGroup;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PdfMapper extends
XWPFDocumentVisitor<IITextContainer, PdfOptions, StylableMasterPage> {
Expand Down Expand Up @@ -144,6 +149,8 @@ public class PdfMapper extends

private Integer expectedPageCount;

private Map<String, String> anchorMap;

public PdfMapper(XWPFDocument document, OutputStream out,
PdfOptions options, Integer expectedPageCount) throws Exception {
super(document, options != null ? options : PdfOptions.getDefault());
Expand All @@ -158,13 +165,15 @@ protected IITextContainer startVisitDocument() throws Exception {
// Create instance of PDF document
this.pdfDocument = new StylableDocument(out, options.getConfiguration());
this.pdfDocument.setMasterPageManager(getMasterPageManager());
anchorMap = new HashMap<String, String>();
return pdfDocument;

}

@Override
protected void endVisitDocument() throws Exception {
pdfDocument.close();
anchorMap.clear();
out.close();
}

Expand Down Expand Up @@ -517,6 +526,12 @@ protected void visitRun(XWPFRun docxRun, boolean pageNumber, String url,
if (url != null) {
// URL is not null, generate a PDF hyperlink.
StylableAnchor pdfAnchor = new StylableAnchor();
//use in anchor link
if (!anchorMap.containsKey(url)) {
if (url.startsWith("#")) {
anchorMap.put(url.substring(1), url);
}
}
pdfAnchor.setReference(url);
pdfAnchor.setITextContainer(container);
container = pdfAnchor;
Expand Down Expand Up @@ -580,7 +595,7 @@ private Font getFont(String fontFamily, Float fontSize, int fontStyle,

/**
* Returns true if the iText font exists and false otherwise.
*
*
* @param font
* @return
*/
Expand All @@ -592,9 +607,30 @@ private boolean isFontExists(Font font) {
@Override
protected void visitText(CTText docxText, boolean pageNumber,
IITextContainer pdfParagraphContainer) throws Exception {

//Get color value when w:rPr inside a w:t
//ex: <w:t> <w:r><w:rPr></w:rPr></w:r></w:t>
Node domNode = docxText.getDomNode();
java.awt.Color color = null;
if (domNode != null) {
try {
Node colorNode = domNode.getChildNodes().item(0).getChildNodes().item(0).getChildNodes().item(0);
if (null != colorNode) {
Node valNode = colorNode.getAttributes().getNamedItem("w:val");
color = java.awt.Color.decode(valNode.getNodeValue());
}
} catch (Exception e) {
}
}
Font font = currentRunFontAscii;
Font fontAsian = currentRunFontEastAsia;
Font fontComplex = currentRunFontHAnsi;
if (color != null) {
//set text color
font.setColor(color);
fontAsian.setColor(color);
fontComplex.setColor(color);
}
createAndAddChunks(pdfParagraphContainer, docxText.getStringValue(),
currentRunUnderlinePatterns, currentRunBackgroundColor,
pageNumber, font, fontAsian, fontComplex);
Expand Down Expand Up @@ -639,6 +675,9 @@ private Chunk createTextChunk(String text, boolean pageNumber,
if (currentRunX != null) {
this.currentRunX += textChunk.getWidthPoint();
}
if (anchorMap.containsKey(text)) {
textChunk.setLocalDestination(text);
}
return textChunk;
}

Expand Down