Exemplo n.º 1
0
        public void  ChangeLastNameForSecondRow(string pFileName, string pTextValue)
        {
            var fileName = Path.Combine(DocumentFolder, pFileName);

            if (!File.Exists(fileName))
            {
                CreateDocumentWithTableFromArray(fileName);
            }

            // Open our document, second parameter indicates edit more
            using (var document = WordprocessingDocument.Open(fileName, true))
            {
                // Get the sole table in the document
                Table table = document.MainDocumentPart.Document.Body.Elements <Table>().First();

                // Find the second row in the table.
                TableRow row = table.Elements <TableRow>().ElementAt(1);

                // Find the second cell in the row.
                TableCell cell = row.Elements <TableCell>().ElementAt(1);

                // Find the first paragraph in the table cell.
                Paragraph p = cell.Elements <Paragraph>().First();

                // Find the first run in the paragraph.
                Run run = p.Elements <Run>().First();

                // Set the text for the run.
                Text text = run.Elements <Text>().First();
                text.Text = pTextValue;

                document.Save();
            }
        }
Exemplo n.º 2
0
        private string GetText(Run run, DqStyleTable dqStyleTable, DqFontScheme fontScheme, DqNumberingTable dqNumberingTable)
        {
            var buffer = new StringBuilder();

            foreach (var element in run.Elements())
            {
                switch (element)
                {
                case DocumentFormat.OpenXml.Wordprocessing.Break @break when @break.Type?.Value == BreakValues.Page:
                    buffer.Append("{PageBreak}");
                    break;

                case AlternateContent alternateContent:
                    if (alternateContent.Descendants <GraphicFrameLocks>().Any())
                    {
                        buffer.Append("{IMG}");
                        break;
                    }
                    var textBoxes  = alternateContent.Elements <AlternateContentFallback>().SelectMany(f => f.Descendants <TextBoxContent>());
                    var paragraphs = textBoxes.SelectMany(textBox => textBox.Elements <Paragraph>()).ToList();
                    foreach (var paragraph in paragraphs.Select(p => Convert(p, dqStyleTable, fontScheme, dqNumberingTable)))
                    {
                        buffer.AppendLine(paragraph.Text);
                    }
                    break;

                case Text text:
                    buffer.Append(text.Text);
                    break;

                case TabChar tab:
                    buffer.Append('\t');
                    break;

                case Drawing drawing:
                    if (drawing.Descendants <DocumentFormat.OpenXml.Drawing.Pictures.Picture>().Any())
                    {
                        buffer.Append("{IMG}");
                    }
                    break;
                }
            }

            return(buffer.ToString());
        }
Exemplo n.º 3
0
        //:::::WORD DOCUMENT PROCESSING PART:::::
        public static void AddToTable(String fileName, String tableName, int elemNo, String txt, int elemRow = 0)
        {
            List <WordP.Table> tables = null;

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true))
            {
                tables = new List <WordP.Table>(wordDoc.MainDocumentPart.Document.Body.Elements <WordP.Table>());
                tables.ForEach(table =>
                {
                    WordP.TableProperties tblProperties = table.GetFirstChild <WordP.TableProperties>();
                    if (tblProperties != null)
                    {
                        WordP.TableCaption caption = tblProperties.GetFirstChild <WordP.TableCaption>();
                        if (caption != null)
                        {
                            if (caption.Val.HasValue && caption.Val.Value.Equals(tableName))
                            {
                                WordP.TableRow row = table.Elements <WordP.TableRow>().ElementAt(elemNo);

                                // Find the third cell in the row.
                                WordP.TableCell cell = row.Elements <WordP.TableCell>().ElementAt(elemRow);

                                // Find the first paragraph in the table cell.
                                WordP.Paragraph p = cell.Elements <WordP.Paragraph>().First();

                                // Find the first run in the paragraph.
                                WordP.Run r = p.Elements <WordP.Run>().First();

                                // Set the text for the run.
                                WordP.Text t = r.Elements <WordP.Text>().First();
                                t.Text       = txt;

                                // you have got your table. process the rest.
                            }
                        }
                    }
                });
            }
        }