|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.DocIORenderer; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace Caption_without_numbers |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + //Creates a new Word document. |
| 13 | + using (WordDocument document = new WordDocument()) |
| 14 | + { |
| 15 | + //Add a new section to the document. |
| 16 | + IWSection section = document.AddSection(); |
| 17 | + //Create a new paragraph and append a Table of Contents (TOC). |
| 18 | + IWParagraph paragraph = section.AddParagraph(); |
| 19 | + TableOfContent tableOfContent = paragraph.AppendTOC(1, 3); |
| 20 | + //Disable a flag to exclude heading style paragraphs in TOC entries. |
| 21 | + tableOfContent.UseHeadingStyles = false; |
| 22 | + //Set the name of SEQ field identifier for table of figures. |
| 23 | + tableOfContent.TableOfFiguresLabel = "Figure"; |
| 24 | + |
| 25 | + //Add a new paragraph for the first image. |
| 26 | + paragraph = section.AddParagraph(); |
| 27 | + //Add the first image to the paragraph. |
| 28 | + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/MountainCycle.jpg"), FileMode.Open, FileAccess.ReadWrite); |
| 29 | + IWPicture picture = paragraph.AppendPicture(imageStream); |
| 30 | + //Add an image caption. |
| 31 | + paragraph = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage); |
| 32 | + //Add text to the caption paragraph. |
| 33 | + paragraph.AppendText(" " + "Mountain-Cycle"); |
| 34 | + //Apply formatting to the caption. |
| 35 | + paragraph.ParagraphFormat.BeforeSpacing = 8; |
| 36 | + paragraph.ParagraphFormat.AfterSpacing = 8; |
| 37 | + //Hide the caption numbering. |
| 38 | + WSeqField field = paragraph.ChildEntities[1] as WSeqField; |
| 39 | + field.HideResult = true; |
| 40 | + |
| 41 | + //Add a new paragraph for the second image. |
| 42 | + paragraph = section.AddParagraph(); |
| 43 | + //Add the second image to the paragraph. |
| 44 | + imageStream = new FileStream(Path.GetFullPath(@"Data/RoadCycle.jpg"), FileMode.Open, FileAccess.ReadWrite); |
| 45 | + picture = paragraph.AppendPicture(imageStream); |
| 46 | + //Add an image caption. |
| 47 | + paragraph = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage); |
| 48 | + //Add text to the caption paragraph. |
| 49 | + paragraph.AppendText(" " + "Road-Cycle"); |
| 50 | + //Apply formatting to the caption. |
| 51 | + paragraph.ParagraphFormat.BeforeSpacing = 8; |
| 52 | + paragraph.ParagraphFormat.AfterSpacing = 8; |
| 53 | + //Hide the caption numbering. |
| 54 | + field = paragraph.ChildEntities[1] as WSeqField; |
| 55 | + field.HideResult = true; |
| 56 | + |
| 57 | + //Update the fields in the Word document. |
| 58 | + document.UpdateDocumentFields(); |
| 59 | + //Update the Table of Contents (TOC). |
| 60 | + document.UpdateTableOfContents(); |
| 61 | + //Create file stream. |
| 62 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 63 | + { |
| 64 | + //Save the Word document to file stream. |
| 65 | + document.Save(outputFileStream, FormatType.Docx); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments