Hi,
I have problems with some word documents, like this attached, you can see that field NUMPAGES doesn’t have field end, but when I open and save doc with your api I have valied field. How could I dinamically know that field is invalid, how You do this? Also, it would help me if you have way to I know are run nodes from field ( field code and field result ) or from out of field?
public void test() throws Exception {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(
"word/H9P-MC-LNBN_Investigator ECG Manual_Ver.1_3rd May 2011_FR.doc");
Document document = getAsposeFactory().createDocumentInstance(
inputStream);
document.save("Desktop/T-H9P-MC-LNBN_Investigator ECG Manual_Ver.1_3rd May 2011_FR.doc");
}
Thanks
Hi Djordje,
Thanks for your inquiry.
djordje:
How could I dinamically know that field is invalid, how You do this? Also, it would help me if you have way to I know are run nodes
A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a “facade” object that provides properties and methods that allow to work with a field as a single object.
The Start, Separator and End properties point to the field start, separator and end nodes of the field respectively.
The FieldStart.getField() throws exception if the FieldEnd is missing. Please check the following code example for your kind reference.
Document doc = new Document(MyDir + "in.doc");
for (FieldStart fStart : (Iterable<FieldStart>)doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getChildNodes(NodeType.FIELD_START, true))
{
try
{
if (fStart.getField().getEnd() == null) // this line will throw exception if field end is missing
{
}
}
catch (Exception e)
{
System.out.println("Invalid field");
}
}
djordje:
Also, it would help me if you have way to I know are run nodes from field ( field code and field result ) or from out of field?
In this case, I suggest you please first save the document in memory stream and load it again in Aspose.Words DOM. Please check the following code example for your kind reference. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document(MyDir + "in.doc");
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.DOCX);
// In you want to read the result into a Document object again, in Java you need to get the
// data bytes and wrap into an input stream.
ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());
// Load the entire document into memory.
Document doc2 = new Document(srcStream);
// You can close the stream now, it is no longer needed because the document is in memory.
srcStream.close();
for (FieldStart fStart : (Iterable<FieldStart>)doc2.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getChildNodes(NodeType.FIELD_START, true))
{
System.out.println(fStart.getField().getFieldCode());
System.out.println(fStart.getField().getResult());
}
Hi,
thanks for answer, but this second part of answer isn’t helpful to me, if I would save document in memory I would slow my aplication especially for large documents, so I can’t do this…
Do You have way to I know are run nodes from field or from out of field, or where should be fieldEnd ( when is missing in field ), how did You know where to insert fieldEnd in this invalid field in my document?
Many thanks
Hi Djordje,
Thanks for your inquiry. Unfortunately, there is no other way to fix the missing FieldEnd issue. In your case, please use the workaround shared in my previous post.
Please let us know if you have any more queries.