示例#1
0
        public static Boolean FillTable(string fileName, Dictionary <string, string> varDict, Dictionary <string, BildMitKommentar> dictBilder)
        {
            Boolean bRet = false;

            szError = "";
            try
            {
                File.Copy(Config.current.szWordTemplate, fileName, true);
                using (WordprocessingDocument wordDocument
                           = WordprocessingDocument.Open(fileName, true))
                {
                    OpenXmlPowerTools.SimplifyMarkupSettings settings = new OpenXmlPowerTools.SimplifyMarkupSettings
                    {
                        RemoveComments              = true,
                        RemoveContentControls       = true,
                        RemoveEndAndFootNotes       = true,
                        RemoveFieldCodes            = false,
                        RemoveLastRenderedPageBreak = true,
                        RemovePermissions           = true,
                        RemoveProof       = true,
                        RemoveRsidInfo    = true,
                        RemoveSmartTags   = true,
                        RemoveSoftHyphens = true,
                        // ReplaceTabsWithSpaces = true,
                        RemoveGoBackBookmark = true,
                        RemoveBookmarks      = true,
                        RemoveMarkupForDocumentComparison = true,
                        RemoveWebHidden = true
                    };
                    OpenXmlPowerTools.MarkupSimplifier.SimplifyMarkup(wordDocument, settings);


                    MainDocumentPart mainPart = wordDocument.MainDocumentPart;

                    var paraList = mainPart.Document.Descendants <Paragraph>();
                    foreach (var para in paraList)
                    {
                        ReplaceVar(para, varDict);
                    }
                    var headerPart = mainPart.HeaderParts.FirstOrDefault();
                    paraList = headerPart.Header.Descendants <Paragraph>();
                    foreach (var para in paraList)
                    {
                        ReplaceVar(para, varDict);
                    }
                    var footerPart = mainPart.FooterParts.FirstOrDefault();
                    paraList = footerPart.Footer.Descendants <Paragraph>();
                    foreach (var para in paraList)
                    {
                        ReplaceVar(para, varDict);
                    }

                    var tableList = mainPart.Document.Descendants <Table>();
                    foreach (var table in tableList)
                    {
                        var rowList = table.Descendants <TableRow>();
                        foreach (var tr in rowList)
                        {
                            var tc = tr.Descendants <TableCell>().FirstOrDefault();
                            // get the first para
                            var p = tc.Descendants <Paragraph>().FirstOrDefault();
                            if (p.InnerText.Trim().Equals("${PictureTable}", StringComparison.InvariantCultureIgnoreCase))
                            {
                                AddInTable(mainPart, table, tr, dictBilder, varDict);
                            }
                        }
                    }
                    wordDocument.Save();
                    bRet = true;
                }
            }
            catch (Exception ex)
            {
                szError = "FillTable: " + ex.ToString();
                Logging.AddError(szError);
            }
            return(bRet);
        }