Add FootNotes to Word Document Programatically using OpenXML SDK C# -
i need create word document using open xml sdk. have document text , footnotes . using below snippets create word document. able create document text ,but not able add footnotes it. can please let know how add footnotes programmatically using open xml
public void createworddocument() { using (memorystream docxmemory = new memorystream()) { using (wordprocessingdocument worddocument = wordprocessingdocument.create(docxmemory, wordprocessingdocumenttype.document, true)) { // add main document part. maindocumentpart mainpart = worddocument.addmaindocumentpart(); // create document structure , add text. this.addsettingstomaindocumentpart(mainpart); styledefinitionspart part = mainpart.styledefinitionspart; // if styles part not exist, add it. if (part == null) { this.addstylesparttopackage(mainpart); } mainpart.document = new document(); body body = mainpart.document.appendchild(new body()); paragraph para = body.appendchild(new paragraph()); run run = para.appendchild(new run()); run.appendchild(new documentformat.openxml.wordprocessing.text("this main text of document")); var footnotespart = mainpart.addnewpart<footnotespart>("rid1"); this.generatefooterpartcontent(mainpart.footnotespart); mainpart.document.save(); worddocument.close(); memorystream result = new memorystream(); docxmemory.seek(0, seekorigin.begin); docxmemory.copyto(result); result.position = 0; ////citation processing byte[] bufferfiledata = new byte[result.length]; result.read(bufferfiledata, 0, (int)result.length); file.writeallbytes("output1.docx", bufferfiledata); } } } private void addsettingstomaindocumentpart(maindocumentpart part) { documentsettingspart settingspart = part.documentsettingspart; if (settingspart == null) settingspart = part.addnewpart<documentsettingspart>(); settingspart.settings = new settings( new compatibility( new compatibilitysetting() { name = new enumvalue<compatsettingnamevalues> (compatsettingnamevalues.compatibilitymode), val = new stringvalue("14"), uri = new stringvalue ("http://schemas.microsoft.com/office/word") } ) ); settingspart.settings.save(); } private styledefinitionspart addstylesparttopackage(maindocumentpart mainpart) { styledefinitionspart part; part = mainpart.addnewpart<styledefinitionspart>(); styles root = new styles(); root.save(part); return part; } // generates content of part. private void generatefooterpartcontent(footnotespart part) { footnotes footnotes1 = new footnotes() { mcattributes = new markupcompatibilityattributes() { ignorable = "w14 wp14" } }; footnotes1.addnamespacedeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingcanvas"); footnotes1.addnamespacedeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); footnotes1.addnamespacedeclaration("o", "urn:schemas-microsoft-com:office:office"); footnotes1.addnamespacedeclaration("r", "http://schemas.openxmlformats.org/officedocument/2006/relationships"); footnotes1.addnamespacedeclaration("m", "http://schemas.openxmlformats.org/officedocument/2006/math"); footnotes1.addnamespacedeclaration("v", "urn:schemas-microsoft-com:vml"); footnotes1.addnamespacedeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingdrawing"); footnotes1.addnamespacedeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingdrawing"); footnotes1.addnamespacedeclaration("w10", "urn:schemas-microsoft-com:office:word"); footnotes1.addnamespacedeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); footnotes1.addnamespacedeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); footnotes1.addnamespacedeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessinggroup"); footnotes1.addnamespacedeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingink"); footnotes1.addnamespacedeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); footnotes1.addnamespacedeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingshape"); footnote footnote1 = new footnote() { type = footnoteendnotevalues.separator, id = -1 }; paragraph paragraph1 = new paragraph() { rsidparagraphaddition = "003f1a60", rsidparagraphproperties = "00626544", rsidrunadditiondefault = "003f1a60" }; run run1 = new run(); separatormark separatormark1 = new separatormark(); run1.append(separatormark1); paragraph1.append(run1); footnote1.append(paragraph1); footnote footnote2 = new footnote() { type = footnoteendnotevalues.continuationseparator, id = 0 }; paragraph paragraph2 = new paragraph() { rsidparagraphaddition = "003f1a60", rsidparagraphproperties = "00626544", rsidrunadditiondefault = "003f1a60" }; run run2 = new run(); continuationseparatormark continuationseparatormark1 = new continuationseparatormark(); run2.append(continuationseparatormark1); paragraph2.append(run2); footnote2.append(paragraph2); footnote footnote3 = new footnote() { id = 1 }; paragraph paragraph3 = new paragraph() { rsidparagraphmarkrevision = "009774cc", rsidparagraphaddition = "00626544", rsidparagraphproperties = "00626544", rsidrunadditiondefault = "00626544" }; paragraphproperties paragraphproperties1 = new paragraphproperties(); paragraphmarkrunproperties paragraphmarkrunproperties1 = new paragraphmarkrunproperties(); runfonts runfonts1 = new runfonts() { asciitheme = themefontvalues.minorhighansi, highansitheme = themefontvalues.minorhighansi, complexscripttheme = themefontvalues.minorhighansi }; fontsize fontsize1 = new fontsize() { val = "24" }; fontsizecomplexscript fontsizecomplexscript1 = new fontsizecomplexscript() { val = "24" }; paragraphmarkrunproperties1.append(runfonts1); paragraphmarkrunproperties1.append(fontsize1); paragraphmarkrunproperties1.append(fontsizecomplexscript1); paragraphproperties1.append(paragraphmarkrunproperties1); run run3 = new run() { rsidrunproperties = "009774cc" }; runproperties runproperties1 = new runproperties(); runfonts runfonts2 = new runfonts() { asciitheme = themefontvalues.minorhighansi, highansitheme = themefontvalues.minorhighansi, complexscripttheme = themefontvalues.minorhighansi }; fontsize fontsize2 = new fontsize() { val = "24" }; fontsizecomplexscript fontsizecomplexscript2 = new fontsizecomplexscript() { val = "24" }; verticaltextalignment verticaltextalignment1 = new verticaltextalignment() { val = verticalpositionvalues.superscript }; runproperties1.append(runfonts2); runproperties1.append(fontsize2); runproperties1.append(fontsizecomplexscript2); runproperties1.append(verticaltextalignment1); text text1 = new text(); text1.text = "1"; run3.append(runproperties1); run3.append(text1); run run4 = new run() { rsidrunproperties = "009774cc" }; runproperties runproperties2 = new runproperties(); runfonts runfonts3 = new runfonts() { asciitheme = themefontvalues.minorhighansi, highansitheme = themefontvalues.minorhighansi, complexscripttheme = themefontvalues.minorhighansi }; runproperties2.append(runfonts3); text text2 = new text() { space = spaceprocessingmodevalues.preserve }; text2.text = " "; run4.append(runproperties2); run4.append(text2); run run5 = new run() { rsidrunproperties = "009774cc", rsidrunaddition = "009774cc" }; runproperties runproperties3 = new runproperties(); runfonts runfonts4 = new runfonts() { asciitheme = themefontvalues.minorhighansi, highansitheme = themefontvalues.minorhighansi, complexscripttheme = themefontvalues.minorhighansi }; fontsize fontsize3 = new fontsize() { val = "21" }; fontsizecomplexscript fontsizecomplexscript3 = new fontsizecomplexscript() { val = "21" }; runproperties3.append(runfonts4); runproperties3.append(fontsize3); runproperties3.append(fontsizecomplexscript3); text text3 = new text(); text3.text = "this foot note text"; run5.append(runproperties3); run5.append(text3); paragraph3.append(paragraphproperties1); paragraph3.append(run3); paragraph3.append(run4); paragraph3.append(run5); footnote3.append(paragraph3); footnotes1.append(footnote1); footnotes1.append(footnote2); footnotes1.append(footnote3); part.footnotes = footnotes1; }
i'm not expert on openxml think you're missing adding actual reference footnote in body text, tweaked code , footnote in document. hope helps or @ least gets started.
run run = para.appendchild(new run()); run.appendchild(new documentformat.openxml.wordprocessing.text("this main text of document")); var footnoteref = new footnotereference() { id = 1 }; run.append(footnoteref);
Comments
Post a Comment