public StoryCardHelper(string storyCardFile, int maxColumns) { using (var document = WordprocessingDocument.Open(storyCardFile, false)) { var doc = document.MainDocumentPart.Document; Table oldTable = doc.Descendants <Table>().First(); originalRows = oldTable.Descendants <TableRow>().ToArray(); originalCells = new List <TableCell[]>(); foreach (var row in originalRows) { originalCells.Add(row.Descendants <TableCell>().ToArray()); } TableProperties = oldTable.Descendants <TableProperties>().First(); } table = new Table(); table.AppendChild(TableProperties.CloneNode(true)); fromFilename = storyCardFile; this.maxColumns = maxColumns; }
private void button_Click(object sender, RoutedEventArgs e) { DirectoryInfo di = new DirectoryInfo("."); foreach (var file in di.GetFiles("*.docx")) { file.Delete(); } DirectoryInfo di2 = new DirectoryInfo("../../../"); foreach (var file in di2.GetFiles("*.docx")) { file.CopyTo(di.FullName + "/" + file.Name); } using (WordprocessingDocument docWord = WordprocessingDocument.Open(filepath, true)) { PageSize pgSz = docWord.MainDocumentPart.Document.Descendants <SectionProperties>().FirstOrDefault().Descendants <PageSize>().FirstOrDefault(); PageMargin pgMr = docWord.MainDocumentPart.Document.Descendants <SectionProperties>().FirstOrDefault().Descendants <PageMargin>().FirstOrDefault(); TableProperties tblPr = new TableProperties(new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }, new TableStyle() { Val = "A3" }, new TableLook() { Val = "04A0", NoVerticalBand = true, NoHorizontalBand = false, LastColumn = false, FirstColumn = true, LastRow = false, FirstRow = true }, new TableBorders() { BottomBorder = new BottomBorder() { Val = BorderValues.Single }, TopBorder = new TopBorder() { Val = BorderValues.Single }, LeftBorder = new LeftBorder() { Val = BorderValues.Single }, RightBorder = new RightBorder() { Val = BorderValues.Single }, InsideHorizontalBorder = new InsideHorizontalBorder() { Val = BorderValues.Single }, InsideVerticalBorder = new InsideVerticalBorder() { Val = BorderValues.Single } }); foreach (var table in docWord.MainDocumentPart.Document.Body.Elements <Table>()) { int maxWidth = Int32.Parse(pgSz.Width) - (Int32.Parse(pgMr.Left) + Int32.Parse(pgMr.Right)); TableWidth tw = table.Elements <TableProperties>().FirstOrDefault().Descendants <TableWidth>().FirstOrDefault(); if (maxWidth < Int32.Parse(tw.Width)) { if (table.Elements <TableProperties>().Count() > 0) { table.RemoveChild(table.Elements <TableProperties>().First()); } table.PrependChild(tblPr.CloneNode(true)); } List <List <string> > numCells = new List <List <string> >(); int i = 0; int prev_shapka = 0; double check; foreach (var item in table.Elements <TableRow>()) { if (prev_shapka == 0 || prev_shapka != item.Elements <TableCell>().Count() && i < 2) { shapkaCorrector(item); i++; } else { int j = 0; List <string> list = new List <string>(); foreach (TableCell c in item.Elements <TableCell>()) { TableCellProperties tbcPr = (TableCellProperties)c.Elements <TableCellProperties>().FirstOrDefault().CloneNode(true); try { if (Double.TryParse(c.Elements <Paragraph>().First().Elements <Run>().First().Elements <Text>().First().Text, out check)) { if (tbcPr.Elements <Justification>().Count() > 0) { tbcPr.Elements <Justification>().FirstOrDefault().Val = JustificationValues.Center; } list.Add(c.Elements <Paragraph>().First().Elements <Run>().First().Elements <Text>().First().Text); c.PrependChild(tbcPr); } else { list.Add(null); if (tbcPr.Elements <Justification>().Count() > 0) { tbcPr.Elements <Justification>().FirstOrDefault().Val = JustificationValues.Both; } c.PrependChild(tbcPr); } } catch { list.Add(null); if (tbcPr.Elements <Justification>().Count() > 0) { tbcPr.Elements <Justification>().FirstOrDefault().Val = JustificationValues.Both; } c.PrependChild(tbcPr); } if (c.Elements <TableCellProperties>().Count() > 1) { c.RemoveChild(c.Elements <TableCellProperties>().Last()); } foreach (Paragraph para in c.Elements <Paragraph>()) { try { if (Double.TryParse(para.Elements <Run>().First().Elements <Text>().First().Text, out check)) { ParagraphProperties pPr = new ParagraphProperties(new Justification() { Val = JustificationValues.Center }); para.PrependChild(pPr); } else { ParagraphProperties pPr = new ParagraphProperties(new Justification() { Val = JustificationValues.Both }); para.PrependChild(pPr); } } catch { ParagraphProperties pPr = new ParagraphProperties(new Justification() { Val = JustificationValues.Both }); para.PrependChild(pPr); } if (para.Elements <ParagraphProperties>().Count() > 1) { para.RemoveChild(para.Elements <ParagraphProperties>().Last()); } foreach (Run run in para.Elements <Run>()) { RunProperties rPr = new RunProperties(new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman" }, new FontSize() { Val = "24" }, new Color() { Val = "000000" }); run.PrependChild(rPr); if (run.Elements <RunProperties>().Count() > 1) { run.RemoveChild(run.Elements <RunProperties>().Last()); } } } j++; } numCells.Add(list); } prev_shapka = item.Elements <TableCell>().Count(); } // numCorrector(numCells, table); i++; } } }