processParagraph static method
Function for processing paragraph
Implementation
static List<WordPage> processParagraph(ProcessParagraphParams params) {
List<WordPage> pages = params.wordDocument.pages;
var pStyle = "";
var chkPProperties = params.paragraphElement.findAllElements("w:pPr");
int seqNo = 0;
Map<String, String> tabs = {};
String paraShadingColor = "";
Map<String, String> paraFormat = {};
if (chkPProperties.isNotEmpty) {
var chkPStyle = chkPProperties.first.findAllElements("w:pStyle");
if (chkPStyle.isNotEmpty) {
var tempPStyle = chkPStyle.first.getAttribute("w:val");
if (tempPStyle != null) {
pStyle = tempPStyle;
}
}
var chkTabs = chkPProperties.first.findAllElements("w:tab");
if (chkTabs.isNotEmpty) {
var tempVal = chkTabs.first.getAttribute("w:val");
if (tempVal != null) {
tabs["val"] = tempVal;
}
var tempLeader = chkTabs.first.getAttribute("w:leader");
if (tempLeader != null) {
tabs["leader"] = tempLeader;
}
}
var shadingColorProperty = chkPProperties.first.findAllElements("w:shd");
if (shadingColorProperty.isNotEmpty) {
var tempShadingColor = shadingColorProperty.first.getAttribute("w:fill");
if (tempShadingColor != null) {
paraShadingColor = tempShadingColor;
}
}
var justifyProp = chkPProperties.first.findAllElements("w:jc");
if (justifyProp.isNotEmpty) {
var tempJc = justifyProp.first.getAttribute("w:val");
if (tempJc != null) {
paraFormat["jc"] = tempJc;
}
}
}
Paragraph paragraph = Paragraph(seqNo, pStyle);
if (tabs.isNotEmpty) {
paragraph.tabDetails = tabs;
}
if (paraShadingColor.isNotEmpty) {
paragraph.shadingColor = paraShadingColor;
}
if (paraFormat.isNotEmpty) {
paragraph.formats = paraFormat;
}
seqNo++;
int pSeqNo = 0;
var runElements = params.paragraphElement.findAllElements("w:r");
if (runElements.isNotEmpty) {
for (var run in runElements) {
List<String> formats = [];
String tStyle = "";
int fontSize = 0;
String textColor = "";
String highlightColor = "";
String shadingColor = "";
Map<String, String> fonts = {};
var runProperty = run.findAllElements("w:rPr");
if (runProperty.isNotEmpty) {
var boldProperty = runProperty.first.findAllElements("w:b");
if (boldProperty.isNotEmpty) {
formats.add("bold");
}
var italicProperty = runProperty.first.findAllElements("w:i");
if (italicProperty.isNotEmpty) {
formats.add("italic");
}
var underlineProperty = runProperty.first.findAllElements("w:u");
if (underlineProperty.isNotEmpty) {
if (underlineProperty.first.getAttribute("w:val") == "single") {
formats.add("single-underline");
} else if (underlineProperty.first.getAttribute("w:val") == "double") {
formats.add("double-underline");
}
}
var strikeProperty = runProperty.first.findAllElements("w:strike");
if (strikeProperty.isNotEmpty) {
formats.add("strike");
}
var scriptProperty = runProperty.first.findAllElements("w:vertAlign");
if (scriptProperty.isNotEmpty) {
if (scriptProperty.first.getAttribute("w:val") == "superscript") {
formats.add("superscript");
} else if (scriptProperty.first.getAttribute("w:val") == "subscript") {
formats.add("subscript");
}
}
var colorProperty = runProperty.first.findAllElements("w:color");
if (colorProperty.isNotEmpty) {
var tempTextColor = colorProperty.first.getAttribute("w:val");
if (tempTextColor != null) {
textColor = tempTextColor;
}
}
var shadingColorProperty = runProperty.first.findAllElements("w:shd");
if (shadingColorProperty.isNotEmpty) {
var tempShadingColor = shadingColorProperty.first.getAttribute("w:fill");
if (tempShadingColor != null) {
shadingColor = tempShadingColor;
}
}
var highlightColorProperty = runProperty.first.findAllElements("w:highlight");
if (highlightColorProperty.isNotEmpty) {
var tempHighlightColor = highlightColorProperty.first.getAttribute("w:val");
if (tempHighlightColor != null) {
highlightColor = tempHighlightColor;
}
}
var styleProperty = runProperty.first.findAllElements("w:rStyle");
if (styleProperty.isNotEmpty) {
var tempStyle = styleProperty.first.getAttribute("w:val");
if (tempStyle != null) {
tStyle = tempStyle;
}
}
var fontSizeProperty = runProperty.first.findAllElements("w:sz");
if (fontSizeProperty.isNotEmpty) {
var tempFontSize = fontSizeProperty.first.getAttribute("w:val");
if (tempFontSize != null) {
fontSize = int.parse(tempFontSize);
}
}
var fontsProperty = runProperty.first.findAllElements("w:rFonts");
if (fontsProperty.isNotEmpty) {
var tempAscii = fontsProperty.first.getAttribute("w:ascii");
if (tempAscii != null) {
fonts["ascii"] = tempAscii;
}
var temphAnsi = fontsProperty.first.getAttribute("w:hAnsi");
if (temphAnsi != null) {
fonts["hAnsi"] = temphAnsi;
}
}
}
var textElements = run.findAllElements("w:t");
if (textElements.isNotEmpty) {
if (paragraph.tabDetails.isNotEmpty) {
for (int i = 0; i < textElements.length; i++) {
if (pSeqNo > 0) {
paragraph.textSpans.add(MsTextSpan(
pSeqNo, textElements.elementAt(i).innerText, tStyle, formats, fontSize, textColor, highlightColor, fonts, shadingColor));
pSeqNo++;
} else {
String innerTex = textElements.elementAt(i).innerText;
if (paragraph.tabDetails["leader"] == "dot") {
if (paragraph.tabDetails["val"] == "left") {
innerTex = "......................$innerTex";
} else {
innerTex = "$innerTex......................";
}
} else if (paragraph.tabDetails["leader"] == "hyphen") {
if (paragraph.tabDetails["val"] == "left") {
innerTex = "--------------------$innerTex";
} else {
innerTex = "$innerTex--------------------";
}
} else if (paragraph.tabDetails["leader"] == "space") {
if (paragraph.tabDetails["val"] == "left") {
innerTex = " $innerTex";
} else {
innerTex = "$innerTex ";
}
}
paragraph.textSpans.add(MsTextSpan(pSeqNo, innerTex, tStyle, formats, fontSize, textColor, highlightColor, fonts, shadingColor));
pSeqNo++;
}
}
} else {
for (var textE in textElements) {
paragraph.textSpans.add(MsTextSpan(pSeqNo, textE.innerText, tStyle, formats, fontSize, textColor, highlightColor, fonts, shadingColor));
pSeqNo++;
}
}
}
var drawingElements = run.findAllElements("w:drawing");
if (drawingElements.isNotEmpty) {
for (var draw in drawingElements) {
var imageBlip = draw.findAllElements("a:blip");
if (imageBlip.isNotEmpty) {
String imagePath = "";
String imageType = "";
int imgCX = 0;
int imgCY = 0;
var imageRid = imageBlip.first.getAttribute("r:embed");
var imageRelation = params.relationShips.firstWhere((rel) {
return rel.id == imageRid;
});
String imageName = imageRelation.target.split("/").last;
imagePath = "${params.wordOutputDirectory}/$imageName";
var imageInline = draw.findAllElements("wp:inline");
if (imageInline.isNotEmpty) {
imageType = "inline";
var imageExtent = imageInline.first.findAllElements("wp:extent");
if (imageExtent.isNotEmpty) {
var tempImageCX = imageExtent.first.getAttribute("cx");
if (tempImageCX != null) {
imgCX = int.parse(tempImageCX);
}
var tempImageCY = imageExtent.first.getAttribute("cy");
if (tempImageCY != null) {
imgCY = int.parse(tempImageCY);
}
}
}
var imageAnchor = draw.findAllElements("wp:anchor");
if (imageAnchor.isNotEmpty) {
imageType = "anchor";
var imageExtent = imageAnchor.first.findAllElements("wp:extent");
if (imageExtent.isNotEmpty) {
var tempImageCX = imageExtent.first.getAttribute("cx");
if (tempImageCX != null) {
imgCX = int.parse(tempImageCX);
}
var tempImageCY = imageExtent.first.getAttribute("cy");
if (tempImageCY != null) {
imgCY = int.parse(tempImageCY);
}
}
}
paragraph.images.add(MsImage(pSeqNo, imagePath, imageType, imgCX, imgCY));
pSeqNo++;
}
}
}
var chkFootNotes = run.findAllElements("w:footnoteReference");
if (chkFootNotes.isNotEmpty) {
for (var footNt in chkFootNotes) {
var footNtId = footNt.getAttribute("w:id");
if (footNtId != null) {
int noteId = 0;
if (params.wordDocument.pages.isNotEmpty) {
noteId = params.wordDocument.pages.last.footNotes.length + params.wordDocument.pages.last.endNotes.length;
}
paragraph.textSpans
.add(MsTextSpan(pSeqNo, (noteId + 1).toString(), tStyle, formats, fontSize, textColor, highlightColor, fonts, shadingColor));
pSeqNo++;
Map<String, String> footNoteDetails = {};
footNoteDetails["id"] = footNtId;
footNoteDetails["refNo"] = (noteId + 1).toString();
footNoteDetails["style"] = tStyle;
if (pages.isEmpty) {
WordPage wordPage = WordPage(pages.length + 1);
pages.add(wordPage);
}
pages.last.footNotes.add(footNoteDetails);
}
}
}
var chkEndNotes = run.findAllElements("w:endnoteReference");
if (chkEndNotes.isNotEmpty) {
for (var endNt in chkEndNotes) {
var endNtId = endNt.getAttribute("w:id");
if (endNtId != null) {
paragraph.textSpans.add(MsTextSpan(
pSeqNo,
(params.wordDocument.pages.last.endNotes.length + params.wordDocument.pages.last.footNotes.length + 1).toString(),
tStyle,
formats,
fontSize,
textColor,
highlightColor,
fonts,
shadingColor));
pSeqNo++;
Map<String, String> endNoteDetails = {};
endNoteDetails["id"] = endNtId;
endNoteDetails["refNo"] =
(params.wordDocument.pages.last.footNotes.length + params.wordDocument.pages.last.endNotes.length + 1).toString();
endNoteDetails["style"] = tStyle;
if (pages.isEmpty) {
WordPage wordPage = WordPage(pages.length + 1);
pages.add(wordPage);
}
pages.last.endNotes.add(endNoteDetails);
}
}
}
}
}
bool newPage = false;
for (var style in params.stylesList) {
if (style.styleId == paragraph.style) {
if (style.pageBreakBefore != null && style.pageBreakBefore == true) {
newPage = true;
}
}
}
if (newPage || pages.isEmpty) {
WordPage wordPage = WordPage(pages.length + 1);
pages.add(wordPage);
}
pages.last.components.add(paragraph);
return pages;
}