1

I'm trying to create this structure in OpenXml:

<P>
 <Table />
 <Caption>Table 1 - Some Text 1 </Caption>
 <Picture />
 <Caption>Figure 1 - Some Text 2 </Caption>
</P>

In terms of code I have:

var currentLine = new Paragraph();
currentLine.AppendChild(new Run(elem));  -> Where the elem is Table
...
currentLine.AppendChild(new Run(elem2)); -> Where the elem2 is Drawing

So I only miss the way to add captions, the same captions that I can do in MS Word References-> Insert Caption.

Some information how to accomplish this would be very appreciated.

Rui

rsantos
  • 230
  • 1
  • 15

2 Answers2

5

Here is the method I used to create Captions. Basically, create a SimpleField and append it to the paragraph.

public Paragraph CreateCaption( string caption, string name )
        {
            Run run = new Run( new Text() { Text= name + " ", Space = SpaceProcessingModeValues.Preserve } );
            SimpleField simpleField = new SimpleField( new Run( new RunProperties( new NoProof() ), new Text() { Text= " ", Space = SpaceProcessingModeValues.Preserve } ) );
            simpleField.Instruction = @"SEQ " + name;
            Run runLabel = new Run( new Text() { Text= " " + caption, Space = SpaceProcessingModeValues.Preserve } );

            ParagraphProperties captionPr = new ParagraphProperties( new ParagraphStyleId() { Val = "Caption" } );
            Paragraph paragraph = new Paragraph();
            paragraph.ParagraphProperties = captionPr;
            paragraph.Append( run );
            paragraph.Append( simpleField );
            paragraph.Append( runLabel );
            return paragraph;
        }
Tarveen
  • 161
  • 2
  • 6
0

Use this: http://www.microsoft.com/en-us/download/details.aspx?id=5124

(Productivity tool) - to inspect word document and see what they are made up of.

Put here is the code:

 SimpleField sf = new SimpleField(new Run(new Text("Figure 1")));

To actually link anything, you need to set sf's instruction property.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • Do you know anyway to update the Caption numbers ? Like the Chapter and Item number ? In my case I'm creating something like "Table 3.1 Some text" the problem is after I add all the Captions I want to update all the Caption in the document, so the Table 3.1 could be updated to 3.3 if there is already 2 Tables before.. thanks – rsantos Mar 21 '13 at 15:08
  • You need to use Interop to update your table of contents. http://stackoverflow.com/questions/3982624/is-it-possible-to-update-the-toc-tableofcontents-of-a-word-document-generated Interop means you need Word.exe installed on the server. – jn1kk Mar 21 '13 at 17:12
  • Actually there is a way to do it without using Interop: simpleField2.Dirty = OnOffOnlyValues.FromBoolean(true); -> when the Word Document is open will ask the user to update all the references, which was what I wanted ;) – rsantos Mar 22 '13 at 09:14
  • Link is now 404. :-( – james.garriss Jun 18 '21 at 12:32
  • @James.garriss it seems it got deprecated, the main Microsoft GitHub repo now links to this too: https://github.com/rmboggs/DocxToSource – jn1kk Jun 21 '21 at 13:08