test.docx (317.8 KB)
this is my test file, I want to change the paragraph style and font style while keep the cross references unchanged , I HAVE Try many ways, but still can’t achieve it. It keeps saying ‘Error! Reference source not found’ , same thing happens when there’s a FieldType.ADD_IN.
code snippet like :
Run previousRun = new Run(null);
for (int i = 0; i < paragraph.getRuns().getCount(); i++)
{
Run run = paragraph.getRuns().get(i);
String previousRunText = previousRun.getText();
String currentRunText = run.getText();
if (run.getText().contains("ADDIN") || currentRunText.matches("\\[\\d+(-\\d+)?(,\\d+)*]")
|| run.getText().contains("REF"))
continue;
previousRunText = previousRunText.trim();
currentRunText = currentRunText.trim();
run.setText(ConverterUtils.formatPunctuationAndAbbreviation(previousRunText, currentRunText));
StyleUtils.merge(run.getFont(), styleConfigDto.getText());
previousRun = run;
}
for (Field f : paragraph.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_ADDIN || f.getType() == FieldType.FIELD_REF)
{
Node current = f.getSeparator();
while (current != null && current != f.getEnd())
{
if (current.getNodeType() == NodeType.RUN)
((Run)current).getFont().setSuperscript(true);
current = current.getNextSibling();
}
}
}
I really need help :<<<<
@Madecho Most likely the problem is caused by the following line of code:
run.setText(ConverterUtils.formatPunctuationAndAbbreviation(previousRunText, currentRunText));
This line of code might change the Run text that contains REF field code, that might cause the broken cross references in the output document.
@alexey.noskov After countless debugging sessions, I finally pinpointed the issue! the code that caused the trouble is :
private void deleteSectionContinuous() {
SectionCollection sections = formattingProcessShareVar.getStudetDocument().getSections();
for (Section s : sections) {
if (s == sections.get(0))
continue;
if (s.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE ||
s.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS) {
((Section) s.getPreviousSibling()).appendContent(s);
s.remove();
}
}
// HERE, AFTER USING updateFields ===> all the cross references show ‘Error! Reference source not found.’
try {
formattingProcessShareVar.getStudetDocument().updateFields();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
so, how can i delete the SectionStart.CONTINUOUS
and SectionStart.NEW_PAGE
while the cross references remain useful ?
@Madecho Please try modifying the code like this:
Document doc = new Document("C:\\Temp\\in.docx");
SectionCollection sections = doc.getSections();
for (Section s : sections)
{
if (s == sections.get(0))
continue;
if (s.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE ||
s.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS)
{
Section prevSect = (Section)s.getPreviousSibling();
while (s.getBody().hasChildNodes())
prevSect.getBody().appendChild(s.getBody().getFirstChild());
s.remove();
}
}
doc.save("C:\\Temp\\out.docx");
1 Like