private void button_Click(object sender, EventArgs e) { SaveFileDialog objSaveFileDialog = new SaveFileDialog(); objSaveFileDialog.Filter = "Word Document (*.docx)|*.docx"; objSaveFileDialog.ShowDialog(); string strSavePath = objSaveFileDialog.FileName; if (strSavePath.Length > 0) { using (WordprocessingDocument objWordprocessingDocument = WordprocessingDocument.Create(strSavePath, WordprocessingDocumentType.Document)) { MainDocumentPart objMainDocumentPart = objWordprocessingDocument.AddMainDocumentPart(); objMainDocumentPart.Document = new Document(); Body objBody = new Body(); SectionProperties objSectionProperties = new SectionProperties(); FooterPart objFootPart = objMainDocumentPart.AddNewPart<FooterPart>(); Footer objFooter = new Footer(); objFootPart.Footer = objFooter; SdtBlock objSdtBlock_1 = new SdtBlock(); SdtContentBlock objSdtContentBlock_1 = new SdtContentBlock(); SdtBlock objSdtBlock_2 = new SdtBlock(); SdtContentBlock objSdtContentBlock_2 = new SdtContentBlock(); Paragraph objParagraph_1 = new Paragraph(); ParagraphProperties objParagraphProperties = new ParagraphProperties(); ParagraphStyleId objParagraphStyleId = new ParagraphStyleId() { Val = "Footer" }; objParagraphProperties.Append(objParagraphStyleId); Justification objJustification = new Justification() { Val = JustificationValues.Right }; objParagraphProperties.Append(objJustification); objParagraph_1.Append(objParagraphProperties); Run objRun_2 = new Run(); FieldChar objFieldChar_1 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; objRun_2.Append(objFieldChar_1); objParagraph_1.Append(objRun_2); Run objRun_3 = new Run(); FieldCode objFieldCode_1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; objFieldCode_1.Text = "PAGE "; objRun_3.Append(objFieldCode_1); objParagraph_1.Append(objRun_3); Run objRun_4 = new Run(); FieldChar objFieldChar_2 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; objRun_4.Append(objFieldChar_2); objParagraph_1.Append(objRun_4); Run objRun_5 = new Run(); Text objText_2 = new Text(); objText_2.Text = "2"; objRun_5.Append(objText_2); objParagraph_1.Append(objRun_5); Run objRun_6 = new Run(); FieldChar objFieldChar_3 = new FieldChar() { FieldCharType = FieldCharValues.End }; objRun_6.Append(objFieldChar_3); objParagraph_1.Append(objRun_6); Run objRun_7 = new Run(); Text objText_3 = new Text("/ "); objRun_7.Append(objText_3); objParagraph_1.Append(objRun_7); Run objRun_8 = new Run(); FieldChar objFieldChar_4 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; objRun_8.Append(objFieldChar_4); objParagraph_1.Append(objRun_8); Run objRun_9 = new Run(); FieldCode objFieldCode_2 =new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; objFieldCode_2.Text = "NUMPAGES "; objRun_9.Append(objFieldCode_2); objParagraph_1.Append(objRun_9); Run objRun_10 = new Run(); FieldChar objFieldChar_5 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; objRun_10.Append(objFieldChar_5); objParagraph_1.Append(objRun_10); Run objRun_11 = new Run(); Text objText_4 = new Text(); objText_4.Text = "2"; objRun_11.Append(objText_4); objParagraph_1.Append(objRun_11); Run objRun_12 = new Run(); FieldChar objFieldChar_6 =new FieldChar() { FieldCharType = FieldCharValues.End }; objRun_12.Append(objFieldChar_6); objParagraph_1.Append(objRun_12); objSdtContentBlock_2.Append(objParagraph_1); objSdtBlock_2.Append(objSdtContentBlock_2); objSdtContentBlock_1.Append(objSdtBlock_2); objSdtBlock_1.Append(objSdtContentBlock_1); objFooter.Append(objSdtBlock_1); string strFootrID = objMainDocumentPart.GetIdOfPart(objFootPart); FooterReference objFooterReference = new FooterReference() { Type = HeaderFooterValues.Default, Id = strFootrID }; objSectionProperties.Append(objFooterReference); objBody.Append(objSectionProperties); objMainDocumentPart.Document.Append(objBody); DocumentSettingsPart objDocumentSettingPart = objMainDocumentPart.AddNewPart<DocumentSettingsPart>(); objDocumentSettingPart.Settings = new Settings(); Compatibility objCompatibility = new Compatibility(); CompatibilitySetting objCompatibilitySetting = new CompatibilitySetting() { Name = CompatSettingNameValues.CompatibilityMode, Uri = "http://schemas.microsoft.com/office/word", Val = "14" }; objCompatibility.Append(objCompatibilitySetting); objDocumentSettingPart.Settings.Append(objCompatibility); } } }
你好,我现在的需求有些特殊,页脚内容是 从HTML 中抓取的代码如下,该如何处理呢?
1 Footer footer = new Footer(); 2 string altChunkId = "AltChunkHeaderId1"; 3 AlternativeFormatImportPart chunk = headerPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Mht, altChunkId); 4 FileStream fileStream = File.Open(footerMhtFilePath, FileMode.Open); 5 chunk.FeedData(fileStream); 6 AltChunk altChunk = new AltChunk(); 7 altChunk.Id = altChunkId; 8 9 footer.AppendChild(altChunk); 10 11 fileStream.Close(); 12 fileStream.Dispose(); 13 return footer;
谢谢,经过测试你提供 的方式是可行的!
1 // Creates an Footer instance and adds its children. 2 private static Footer GeneratePageFooterPart(string footerHtmlFilePath, string footerMhtFilePath, FooterPart footerPart, out string errInfo) 3 { 4 errInfo = ""; 5 Footer footer = new Footer(); 6 try 7 { 8 AltChunk altChunk = GenerateAltChunk(3, (object)footerPart, footerHtmlFilePath, footerMhtFilePath, out errInfo); 9 if (altChunk == null) 10 { 11 return null; 12 } 13 14 footer.AppendChild(altChunk); 15 16 #region 创建页码 标签 用于自动生成页码 17 18 SdtBlock sdtBlock = new SdtBlock(); 19 SdtContentBlock sdtContentBlock = new SdtContentBlock(); 20 21 Paragraph p = new Paragraph(); 22 ParagraphProperties pproperties = new ParagraphProperties(); 23 Justification justification = new Justification() { Val = JustificationValues.Right }; 24 pproperties.Append(justification); 25 p.Append(pproperties); 26 27 Run objRun_1 = new Run(); 28 Text objText_1 = new Text(); 29 objText_1.Text = "PAGE "; 30 objText_1.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve); 31 objRun_1.Append(objText_1); 32 p.Append(objRun_1); 33 34 Run objRun_2 = new Run(); 35 FieldChar objFieldChar_1 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; 36 objRun_2.Append(objFieldChar_1); 37 p.Append(objRun_2); 38 Run objRun_3 = new Run(); 39 FieldCode objFieldCode_1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; 40 objFieldCode_1.Text = "PAGE"; 41 objRun_3.Append(objFieldCode_1); 42 p.Append(objRun_3); 43 Run objRun_4 = new Run(); 44 FieldChar objFieldChar_2 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; 45 objRun_4.Append(objFieldChar_2); 46 p.Append(objRun_4); 47 48 Run objRun_6 = new Run(); 49 FieldChar objFieldChar_3 = new FieldChar() { FieldCharType = FieldCharValues.End }; 50 objRun_6.Append(objFieldChar_3); 51 p.Append(objRun_6); 52 53 Run objRun_7 = new Run(); 54 Text objText_3 = new Text(); 55 objText_3.Text = " OF "; 56 objText_3.Space = new EnumValue<SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve); 57 objRun_7.Append(objText_3); 58 p.Append(objRun_7); 59 60 Run objRun_8 = new Run(); 61 FieldChar objFieldChar_4 = new FieldChar() { FieldCharType = FieldCharValues.Begin }; 62 objRun_8.Append(objFieldChar_4); 63 p.Append(objRun_8); 64 65 Run objRun_9 = new Run(); 66 FieldCode objFieldCode_2 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve }; 67 objFieldCode_2.Text = "NUMPAGES"; 68 objRun_9.Append(objFieldCode_2); 69 p.Append(objRun_9); 70 Run objRun_10 = new Run(); 71 FieldChar objFieldChar_5 = new FieldChar() { FieldCharType = FieldCharValues.Separate }; 72 objRun_10.Append(objFieldChar_5); 73 p.Append(objRun_10); 74 75 Run objRun_12 = new Run(); 76 FieldChar objFieldChar_6 = new FieldChar() { FieldCharType = FieldCharValues.End }; 77 objRun_12.Append(objFieldChar_6); 78 p.Append(objRun_12); 79 80 sdtContentBlock.Append(p); 81 sdtBlock.Append(sdtContentBlock); 82 footer.Append(sdtBlock); 83 84 #endregion 85 86 } 87 catch (Exception ex) 88 { 89 errInfo = "GeneratePageFooterPart 发生异常:" + ex.ToString(); 90 new Log(logFileName).Write("GeneratePageFooterPart 发生异常:ErrorInfo:" + ex.ToString()); 91 new Log(logFileName).Write("GeneratePageFooterPart 发生异常:footerHtmlFilePath:" + footerHtmlFilePath.ToString()); 92 new Log(logFileName).Write("GeneratePageFooterPart 发生异常:footerMhtFilePath:" + footerMhtFilePath); 93 return null; 94 } 95 96 return footer; 97 98 }
sdk有