Add a Caption to a Table or Figure in OpenXml

左心房为你撑大大i 提交于 2019-12-02 16:18:23

问题


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


回答1:


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.




回答2:


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;
        }


来源:https://stackoverflow.com/questions/15548298/add-a-caption-to-a-table-or-figure-in-openxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!