Page 1 of 1

variables not replaced if all of them are not in same font

PostPosted: Wed Dec 05, 2018 6:35 pm
by monika_thakran
Hi Team,

I am facing a an issue where if all of my variables in a docx document are not in same font, then few of them don't get replaced with actual values.

Below is the piece of code I use to replace variables and generate pdf, please advise what is missing

tempDir = correctDirectoryPath(tempDir);
bytes = IOUtils.toByteArray(inputStream);
file = new File(tempDir + fileName + this.formatter.format(new Date()) + ".pdf");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new ByteArrayInputStream(bytes));
VariablePrepare.prepare(wordMLPackage);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
if (params != null) {
fields = new HashMap<>(params);
documentPart.variableReplace((HashMap<String, String>) fields);
}
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);

List<SectionWrapper> sectionWrappers = replaceHeaders(params, wordMLPackage);

FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
if (!file.exists()) {
file.createNewFile();
}
os = new FileOutputStream(file);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
System.out.println("Done!");
return file;

Re: variables not replaced if all of them are not in same fo

PostPosted: Wed Dec 05, 2018 7:29 pm
by jason
I see you are running VariablePrepare, so I suspect your text slips through its heuristics for fixing split runs.

Can you attach your docx or email it to me?

Re: variables not replaced if all of them are not in same fo

PostPosted: Wed Dec 05, 2018 8:17 pm
by monika_thakran
Hey Jason,

Thanks a lot for replying, shall I not use VariablePrepare, what would I loose if I don't.

I am attaching a sample docx document, which I am using to convert to PDF.

I have removed few lines from between as that is some company stuff.

Awaiting your response.

Re: variables not replaced if all of them are not in same fo

PostPosted: Wed Dec 05, 2018 9:56 pm
by monika_thakran
Attaching one more file for reference

Re: variables not replaced if all of them are not in same fo

PostPosted: Fri Dec 07, 2018 10:00 am
by jason
Running your documents through:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public static void main(String[] args) throws Exception {

                boolean save=true;
               
                String inputfilepath = System.getProperty("user.dir") + "/monika/Welcome.docx";
               
                WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
               
                // Before .. note attributes w:rsidRDefault="00D15781" w:rsidR="00D15781"
                System.out.println(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));
               
                prepare(wmlPackage);
               
                System.out.println(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(), true, true));

                // Save it
                if (save) {
                        SaveToZipFile saver = new SaveToZipFile(wmlPackage);
                        saver.save(System.getProperty("user.dir") + "/OUT_VariablePrepare.docx");
                        System.out.println("Saved");
                }
        }
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


I can see the runs are still "split":

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
            <w:r>
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                    <w:b/>
                    <w:color w:val="000000" w:themeColor="text1"/>
                    <w:sz w:val="18"/>
                    <w:szCs w:val="20"/>
                </w:rPr>
                <w:t>}, ${</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                    <w:b/>
                    <w:color w:val="000000" w:themeColor="text1"/>
                    <w:sz w:val="18"/>
                    <w:szCs w:val="20"/>
                    <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/>
                </w:rPr>
                <w:t>CLIENT_STATE</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                    <w:b/>
                    <w:color w:val="000000" w:themeColor="text1"/>
                    <w:sz w:val="18"/>
                    <w:szCs w:val="20"/>
                </w:rPr>
                <w:t>}, ${</w:t>
            </w:r>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


The runs are only joined up if the rPr are identical:
https://github.com/plutext/docx4j/blob/ ... .java#L144

Notice here that one only of the rPr contains <w:shd w:val="clear" w:color="auto" w:fill="FFFFFF"/> so the runs stay split and the variable isn't recognised.