Extract FieldCodes
Home › Forums › WordprocessingML › Extract FieldCodes
Tagged: FieldCodes
This topic contains 10 replies, has 4 voices, and was last updated by Myname 5 years, 6 months ago.
-
AuthorPosts
-
August 31, 2016 at 10:26 am #3711
Hello,
How do I extract field codes from the run? I don’t see any method returning FieldCode in fieldRetriever.cs.
THanks,
ManuAugust 31, 2016 at 1:11 pm #3714Hi Manu,
With regards to FieldRetriever, I believe that you call this on the entire document, and it returns the entire set of field codes for the document. However, it has been many years since I wrote and used this. Have you gone through the examples in Open-Xml-PowerTools? There is a FieldRetriever01 example in there.
Cheers, Eric
September 1, 2016 at 1:08 pm #3726Hi Eric,
I don’t see the fieldcode for hyperlink in document.xml within the below TOC field paragraph. here is the snippet of the document.xml but i see that word interop or Aspose.words are recognizing this hyperlink field code : HYPERLINK \l “_Toc433904186″. could you please help me how interop/Aspose are identifying this fieldcode?
<w:p w:rsidRDefault=”00497C28″ w:rsidR=”00630630″>
<w:pPr>
<w:pStyle w:val=”TOC1″/>
-<w:rPr>
<w:rFonts w:cstheme=”minorBidi” w:hAnsiTheme=”minorHAnsi” w:eastAsiaTheme=”minorEastAsia” w:asciiTheme=”minorHAnsi”/>
<w:noProof/>
<w:kern w:val=”0″/>
<w:sz w:val=”22″/>
<w:szCs w:val=”22″/>
<w:lang w:eastAsia=”en-GB”/>
</w:rPr>
</w:pPr>
<w:r>
<w:fldChar w:fldCharType=”begin”/>
</w:r>
<w:r w:rsidR=”002223C4″>
<w:instrText xml:space=”preserve”> TOC \b “ContentA” \o “1-1″ \h \z \u </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType=”separate”/>
</w:r>
<w:hyperlink w:history=”1″ w:anchor=”_Toc433904186″>
<w:r w:rsidRPr=”00CF2217″ w:rsidR=”00630630″>
<w:rPr>
<w:rStyle w:val=”Hyperlink”/>
<w:noProof/>
</w:rPr>
<w:t>1.</w:t>
</w:r>
<w:r w:rsidR=”00630630″>
<w:rPr>
<w:rFonts w:cstheme=”minorBidi” w:hAnsiTheme=”minorHAnsi” w:eastAsiaTheme=”minorEastAsia” w:asciiTheme=”minorHAnsi”/>
<w:noProof/>
<w:kern w:val=”0″/>
<w:sz w:val=”22″/>
<w:szCs w:val=”22″/>
<w:lang w:eastAsia=”en-GB”/>
</w:rPr>
<w:tab/>
</w:r>
<w:r w:rsidRPr=”00CF2217″ w:rsidR=”00630630″>
<w:rPr>
<w:rStyle w:val=”Hyperlink”/>
<w:noProof/>
</w:rPr>
<w:t>Interpretation</w:t>
</w:r>
<w:r w:rsidR=”00630630″>
<w:rPr>
<w:noProof/>
<w:webHidden/>
</w:rPr>
<w:tab/>
</w:r>
</w:hyperlink>
</w:p>THanks,
ManuSeptember 2, 2016 at 11:07 am #3736Could anyone please help me with this, it is very urgent?
Thanks in advance!
ManuSeptember 5, 2016 at 6:04 am #3739Manu,
Not sure if this is what you are looking for:
Retrieving Fields in Open XML WordprocessingML DocumentsIt explains a console app called QueryFields that writes out the field code and value of every field in the (hard-coded) specified document. The source is linked from the blog post.
Hope this helps.
John
September 5, 2016 at 5:23 pm #3742Thanks John that was very helpful. I need to ask one more thing do hyperlinks not have the fldchar begin, separate and attributes??
Thanks
ManuSeptember 6, 2016 at 1:07 am #3744Manu,
I’m still learning this stuff too (I’m more interested in merge fields and content controls at present), so it’s a bit of the blind leading the blind, but see if this helps:
Have a look at Wordprocessing Hyperlinks in officeopenxml.com, which explains the difference between external and internal links.
I assume you have already looked at How to Replace or Modify Hyperlinks and Replace Fields with Static Text on the Aspose.Words documentation site, together with the example source at https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples on GitHub?
I note, however, that you were asking about the hyperlinks in a TOC. As I understand it, they are hyperlink fields in a TOC, but they link to PAGEREF fields. And in all the examples I’ve looked and and created, they do have “begin”, “separate” and “end” tags.
See Eric’s Screen-Cast: Exploring Tables-of-Contents in Open XML WordprocessingML Documents, the first of five screencasts on TOCs.
It might help if you explained exactly what you are trying to do with the TOC. I assume you are using Aspose.Words?
Hope this is of assistance.
Cheers.
JohnSeptember 6, 2016 at 5:41 am #3745No John I am using OPenxml only, I just to get the output similar to Apose.words using openxml.
Hyperlink that I showed in the above example doesnot have any “begin”, “separate” and “end” tags but still aspose/word interop generates a fieldcode (HYPERLINK \l “_Toc433904186″), Could you please explain me how?
THanks,
ManuSeptember 6, 2016 at 6:26 am #3746Also, I tried below code to query fields in the docuement
static void Main(string[] args)
{
using (WordprocessingDocument doc =
WordprocessingDocument.Open(“Test.docx”, false))
{
foreach (var f in doc.MainDocumentPart.Fields())
Console.WriteLine(“Id: {0} InstrText: {1}”, f.Id, f.InstrText);
}
}but I am getting build error as –
DocumentFormat.OpenXml.Packaging.MainDocumentPart’ does not contain a definition for ‘Fields’ and no extension method ‘Fields’ accepting a first argument of type ‘DocumentFormat.OpenXml.Packaging.MainDocumentPart’ could be found (are you missing a using directive or an assembly reference?)please help!
Thanks,
ManuSeptember 12, 2016 at 6:16 am #3771Hi,
There are three forms of field codes:
- There is the form that has fldChar with begin/separate/end
- There is a simple form of fields, which use the fldSimple element
- There is a variation on fldSimple, which is the w:hyperlink element
You can find hyperlinks in both forms – fldChart/begin/separate/end form, and w:hyperlink form. Your code needs to be prepared to handle either. To make matters more fun, when you have a w:hyperlink element, the URI is stored as an external relationship. When using the fldChar/begin/separate/end form, the hyperlink is stored in the text of the field code.
It is a bit of a mess, IMO.
If I recall correctly, in the WmlToHtmlConverter, I converted all fields that are in the fldChar/begin/separate/end to the w:hyperlink form, so that the transform can handle all hyperlinks in the same way. That approach might be useful to you.
Cheers, Eric
May 3, 2019 at 10:04 am #7891I think you need have a look here for more. See ya later!
-
AuthorPosts
You must be logged in to reply to this topic.