starting project, replace text with text or image
This commit is contained in:
parent
d43836e26f
commit
cb71e72a2a
|
@ -1,3 +1,5 @@
|
||||||
# Docx Editor Library
|
# Docx Editor Library
|
||||||
|
|
||||||
IN WIP
|
IN WIP
|
||||||
|
|
||||||
|
0.0.1
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
import 'package:docx/docx.dart';
|
|
||||||
import 'dart:io';
|
|
||||||
import 'package:docx/replace.dart';
|
|
||||||
|
|
||||||
void main(List<String> args) {
|
|
||||||
DocxEditor docx = DocxEditor(File('test/assets/docx_test.docx'), {
|
|
||||||
'Title': ReplaceContent(value: 'NewTitle'),
|
|
||||||
'A': ReplaceContent(value: 'AModified'),
|
|
||||||
'ImageCat': ReplaceContent(img: DocxImage(file: File('test/assets/cat.png'), sizeX: 200, sizeY: 400)),
|
|
||||||
'ImageDog': ReplaceContent(img: DocxImage(file: File('test/assets/dog.png'), sizeX: 400, sizeY: 300)),
|
|
||||||
'Footer': ReplaceContent(img: DocxImage(file: File('test/assets/dog.jpg'), sizeX: 400, sizeY: 300))
|
|
||||||
});
|
|
||||||
}
|
|
156
lib/docx.dart
156
lib/docx.dart
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:collection';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
@ -8,13 +9,19 @@ import 'package:docx/replace.dart';
|
||||||
|
|
||||||
class DocxEditor {
|
class DocxEditor {
|
||||||
|
|
||||||
File fileDocx;
|
File fileInDocx;
|
||||||
|
String? pathOutDocx;
|
||||||
|
|
||||||
/// [fileDocx] The Docx file...
|
/// [fileInDocx] The Docx file (required)
|
||||||
/// [replaceMap] All text and image to change
|
/// [pathOutDocx] The path where new file is save (optional)
|
||||||
DocxEditor(this.fileDocx, Map<String, ReplaceContent> replaceMap) {
|
/// [replaceMap] All text and image to change (required)
|
||||||
if (!fileDocx.existsSync())
|
DocxEditor({
|
||||||
throw(PathNotFoundException(fileDocx.path, OSError('File not found', 404)));
|
required this.fileInDocx,
|
||||||
|
this.pathOutDocx,
|
||||||
|
required Map<String, ReplaceContent> replaceMap
|
||||||
|
}) {
|
||||||
|
if (!fileInDocx.existsSync())
|
||||||
|
throw(PathNotFoundException(fileInDocx.path, OSError('File not found', 404)));
|
||||||
|
|
||||||
List<XmlDocument> documentsXml = editById(replaceMap);
|
List<XmlDocument> documentsXml = editById(replaceMap);
|
||||||
save(documentsXml, replaceMap);
|
save(documentsXml, replaceMap);
|
||||||
|
@ -22,67 +29,74 @@ class DocxEditor {
|
||||||
|
|
||||||
/// This function search ID in [replaceMap] and replace by his value or by a image
|
/// This function search ID in [replaceMap] and replace by his value or by a image
|
||||||
List<XmlDocument> editById(Map<String, ReplaceContent> replaceMap) {
|
List<XmlDocument> editById(Map<String, ReplaceContent> replaceMap) {
|
||||||
Uint8List content = fileDocx.readAsBytesSync();
|
Uint8List content = fileInDocx.readAsBytesSync();
|
||||||
|
|
||||||
Archive archive = ZipDecoder().decodeBytes(content);
|
Archive archive = ZipDecoder().decodeBytes(content);
|
||||||
ArchiveFile archiveFile = archive.firstWhere((f) => f.name == 'word/document.xml');
|
ArchiveFile archiveFile = archive.firstWhere((f) => f.name == 'word/document.xml');
|
||||||
XmlDocument documentXml = XmlDocument.parse(utf8.decode(archiveFile.content as List<int>));
|
XmlDocument documentXml = XmlDocument.parse(utf8.decode(archiveFile.content as List<int>));
|
||||||
|
|
||||||
XmlDocument documentXmlRels = addImageinArchive(archive, replaceMap);
|
XmlDocument documentXmlRels = addImageinArchive(archive, replaceMap);
|
||||||
|
|
||||||
print(documentXml.toXmlString(pretty: true, indent: ' '));
|
//print(documentXml.toXmlString(pretty: true, indent: ' '));
|
||||||
|
|
||||||
// TODO: Rework this function, doesn't work with ambiguous ID to search, or multiple Id in one paragraphe, separator like '_' or '-' or space
|
// sort descending orderz
|
||||||
// TODO: Essayer de parcourir par rapport au paragraphe (<w:p>) et non les runs (<w:r>)
|
final keys = replaceMap.keys.toList()..sort((a, b) => b.length.compareTo(a.length));
|
||||||
for (XmlElement node in documentXml.findAllElements('w:r')) {
|
|
||||||
String ndText = node.innerText.toLowerCase();
|
|
||||||
//Iterable<String> replacesId = replaceMap.keys.where((rm) => rm.toLowerCase() == ndText);
|
|
||||||
for (String key in replaceMap.keys) {
|
|
||||||
Iterable<String> replacesId = RegExp(key.toLowerCase()).allMatches(ndText).map((m) => m.group(0)!);
|
|
||||||
XmlNode? parent;
|
|
||||||
for (int i = 0; i < replacesId.length; i++) {
|
|
||||||
ReplaceContent replaceCnt = replaceMap[key]!;
|
|
||||||
if (replaceCnt.img != null)
|
|
||||||
parent = putImage(node, replaceCnt, parent);
|
|
||||||
else { /// Replace Text by other text
|
|
||||||
int start = node.innerXml.indexOf('<w:t>');
|
|
||||||
int end = node.innerXml.indexOf('</w:t>');
|
|
||||||
node.innerXml = node.innerXml.replaceRange(start + 5, end, replaceCnt.value!);
|
|
||||||
|
|
||||||
}
|
final pattern = RegExp(
|
||||||
}
|
keys.map((key) => '(?<=^|\\s)${RegExp.escape(key)}(?=\\s|\$)').join('|'),
|
||||||
}
|
);
|
||||||
|
|
||||||
|
// replace image
|
||||||
|
for (XmlElement node in documentXml.findAllElements('w:t')) {
|
||||||
|
String ndText = node.innerText.toLowerCase();
|
||||||
|
if (ndText.isEmpty)
|
||||||
|
continue;
|
||||||
|
XmlElement? parent;
|
||||||
|
for (String key in replaceMap.keys) {
|
||||||
|
ReplaceContent replaceCnt = replaceMap[key]!;
|
||||||
|
if (replaceCnt.img == null)
|
||||||
|
continue;
|
||||||
|
for (RegExpMatch regMatch in RegExp('(?<=^|\\s)${key.toLowerCase()}(?=\\s|\$)').allMatches(ndText))
|
||||||
|
parent = putImage(node.parentElement, replaceCnt, parent);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only replace text!
|
||||||
|
for (XmlElement paragraph in documentXml.findAllElements('w:p')) {
|
||||||
|
List<XmlElement> texts = paragraph.findAllElements('w:t').toList();
|
||||||
|
if (texts.isEmpty)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String original = texts.map((t) {
|
||||||
|
return t.innerText;
|
||||||
|
}).join();
|
||||||
|
|
||||||
|
String replaced = original.replaceAllMapped(pattern, (match) {
|
||||||
|
ReplaceContent rc = replaceMap[match[0]]!;
|
||||||
|
return rc.img == null ? rc.value! : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
if (original == replaced)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
final firstRun = paragraph.findElements('w:r').first;
|
||||||
|
final newRun = firstRun.copy();
|
||||||
|
newRun.findAllElements('w:t').first.innerText = replaced; // Update with new text
|
||||||
|
|
||||||
|
// replace the first w:r with the new
|
||||||
|
firstRun.replace(newRun);
|
||||||
|
}
|
||||||
|
|
||||||
return [documentXml, documentXmlRels];
|
return [documentXml, documentXmlRels];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add all image in [replaceMap] to Archive in 'word/_rels/document.xml.rels'
|
// TODO: Find a solution to put multiple image in only one <w:r>
|
||||||
XmlDocument addImageinArchive(Archive archive, Map<String, ReplaceContent> replaceMap) {
|
|
||||||
|
|
||||||
ArchiveFile archiveFile = archive.firstWhere((f) => f.name == 'word/_rels/document.xml.rels');
|
|
||||||
XmlDocument documentXmlRels = XmlDocument.parse(utf8.decode(archiveFile.content as List<int>));
|
|
||||||
String documentXmlStr = documentXmlRels.toXmlString();
|
|
||||||
RegExp exp = RegExp(r'Id="rId([0-9]+)"');
|
|
||||||
Iterable<RegExpMatch> matches = exp.allMatches(documentXmlStr);
|
|
||||||
int maxId = matches.isEmpty ? 1 : int.parse(matches.last.group(1)!);
|
|
||||||
|
|
||||||
for (ReplaceContent replaceCnt in replaceMap.values.where((rm) => rm.img != null)) {
|
|
||||||
replaceCnt.img!.id = ++maxId;
|
|
||||||
String text = '<Relationship Id="rId$maxId" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/${replaceCnt.img!.file.path}"/>\n';
|
|
||||||
int lastIndex = documentXmlStr.lastIndexOf('/>') + 3;
|
|
||||||
documentXmlStr = documentXmlStr.replaceRange(lastIndex, lastIndex, text);
|
|
||||||
}
|
|
||||||
documentXmlRels = XmlDocument.parse(documentXmlStr);
|
|
||||||
return documentXmlRels;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replaces the text ([node]) with an image ([replaceContent])
|
/// Replaces the text ([node]) with an image ([replaceContent])
|
||||||
XmlNode? putImage(XmlElement node, ReplaceContent replaceContent, XmlNode? newParent) {
|
XmlElement? putImage(XmlElement? node, ReplaceContent replaceContent, XmlElement? newParent) {
|
||||||
|
|
||||||
if (node.parent == null && newParent != null)
|
if (node!.parentElement == null && newParent != null)
|
||||||
node.attachParent(newParent);
|
node.attachParent(newParent);
|
||||||
XmlNode? parent = node.parent;
|
XmlElement? parent = node.parentElement;
|
||||||
String nodeStr = node.toXmlString();
|
String nodeStr = node.toXmlString();
|
||||||
node.replace(XmlDocumentFragment.parse("""
|
node.replace(XmlDocumentFragment.parse("""
|
||||||
$nodeStr
|
$nodeStr
|
||||||
|
@ -124,12 +138,32 @@ class DocxEditor {
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recreate a Archive to add modification and save it on [fileDocx]
|
/// Add all image in [replaceMap] to Archive in 'word/_rels/document.xml.rels'
|
||||||
|
XmlDocument addImageinArchive(Archive archive, Map<String, ReplaceContent> replaceMap) {
|
||||||
|
|
||||||
|
ArchiveFile archiveFile = archive.firstWhere((f) => f.name == 'word/_rels/document.xml.rels');
|
||||||
|
XmlDocument documentXmlRels = XmlDocument.parse(utf8.decode(archiveFile.content as List<int>));
|
||||||
|
String documentXmlStr = documentXmlRels.toXmlString();
|
||||||
|
RegExp exp = RegExp(r'Id="rId([0-9]+)"');
|
||||||
|
Iterable<RegExpMatch> matches = exp.allMatches(documentXmlStr);
|
||||||
|
int maxId = matches.isEmpty ? 1 : int.parse(matches.last.group(1)!);
|
||||||
|
|
||||||
|
for (ReplaceContent replaceCnt in replaceMap.values.where((rm) => rm.img != null)) {
|
||||||
|
replaceCnt.img!.id = ++maxId;
|
||||||
|
String text = '<Relationship Id="rId$maxId" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/${replaceCnt.img!.file.path}"/>\n';
|
||||||
|
int lastIndex = documentXmlStr.lastIndexOf('/>') + 3;
|
||||||
|
documentXmlStr = documentXmlStr.replaceRange(lastIndex, lastIndex, text);
|
||||||
|
}
|
||||||
|
documentXmlRels = XmlDocument.parse(documentXmlStr);
|
||||||
|
return documentXmlRels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recreate a Archive to add modification and save it on [fileInDocx]
|
||||||
/// [documentsXml] is list -> ['word/document.xml', 'word/_rels/document.xml.rels']
|
/// [documentsXml] is list -> ['word/document.xml', 'word/_rels/document.xml.rels']
|
||||||
/// [replaceMap] for upload file image in docx file
|
/// [replaceMap] for upload file image in docx file
|
||||||
void save(List<XmlDocument> documentsXml, Map<String, ReplaceContent> replaceMap) {
|
File save(List<XmlDocument> documentsXml, Map<String, ReplaceContent> replaceMap) {
|
||||||
try {
|
try {
|
||||||
final originalBytes = fileDocx.readAsBytesSync();
|
final originalBytes = fileInDocx.readAsBytesSync();
|
||||||
final archive = ZipDecoder().decodeBytes(originalBytes);
|
final archive = ZipDecoder().decodeBytes(originalBytes);
|
||||||
|
|
||||||
final Uint8List uListDocumentXml = utf8.encode(documentsXml[0].toXmlString());
|
final Uint8List uListDocumentXml = utf8.encode(documentsXml[0].toXmlString());
|
||||||
|
@ -167,9 +201,13 @@ class DocxEditor {
|
||||||
}
|
}
|
||||||
|
|
||||||
final zipData = ZipEncoder().encode(newArchive);
|
final zipData = ZipEncoder().encode(newArchive);
|
||||||
fileDocx.writeAsBytesSync(zipData);
|
|
||||||
|
File newFile = pathOutDocx == null ? fileInDocx : File(pathOutDocx!);
|
||||||
|
newFile.writeAsBytesSync(zipData);
|
||||||
|
return newFile;
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw StateError('DOCX BUILDER ERROR: $e');
|
throw StateError('DOCX BUILDER ERROR: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ environment:
|
||||||
dependencies:
|
dependencies:
|
||||||
xml: ^6.6.0
|
xml: ^6.6.0
|
||||||
archive: ^4.0.7
|
archive: ^4.0.7
|
||||||
|
xml2json: ^6.2.7
|
||||||
# path: ^1.8.0
|
# path: ^1.8.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -6,12 +6,17 @@ import 'dart:io';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('Try to generate a file without error', () {
|
test('Try to generate a file without error', () {
|
||||||
DocxEditor docx = DocxEditor(File('test/assets/docx_test.docx'), {
|
DocxEditor docx = DocxEditor(
|
||||||
|
fileInDocx: File('test/assets/docx_test_copy.docx'),
|
||||||
|
pathOutDocx: 'test/assets/docx_test.docx',
|
||||||
|
replaceMap: {
|
||||||
'Title': ReplaceContent(value: 'NewTitle'),
|
'Title': ReplaceContent(value: 'NewTitle'),
|
||||||
'A': ReplaceContent(value: 'AModified'),
|
'A': ReplaceContent(value: 'AModified'),
|
||||||
'Image-Chat': ReplaceContent(img: DocxImage(file: File('test/assets/cat.png'), sizeX: 200, sizeY: 400)),
|
'Image-Dog': ReplaceContent(value: 'NotImage-Dog'),
|
||||||
'Image-Dog': ReplaceContent(img: DocxImage(file: File('test/assets/dog.png'), sizeX: 400, sizeY: 300)),
|
'ImageCat': ReplaceContent(img: DocxImage(file: File('test/assets/cat.png'), sizeX: 200, sizeY: 400)),
|
||||||
|
'Image-Cat': ReplaceContent(img: DocxImage(file: File('test/assets/cat.png'), sizeX: 200, sizeY: 400)),
|
||||||
'Footer': ReplaceContent(img: DocxImage(file: File('test/assets/dog.jpg'), sizeX: 400, sizeY: 300))
|
'Footer': ReplaceContent(img: DocxImage(file: File('test/assets/dog.jpg'), sizeX: 400, sizeY: 300))
|
||||||
});
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue