Exemplo n.º 1
0
        private void BuildDocumentIndexStyle(List <DirectoryInfo> list)
        {
            var templatetbl = docx.Tables[0];
            var c           = 0;

            var       templaterow    = templatetbl.Rows[0];
            var       ncols          = templaterow.ColumnCount;
            Row       row            = null;
            Table     tbl            = null;
            Paragraph alphaParagraph = null;

            foreach (var pg in docx.Paragraphs)
            {
                if (pg.Text.Contains("{lastnamestartswith}"))
                {
                    alphaParagraph = pg;
                    break;
                }
            }

            var alphaindex = "@";

            foreach (var m in list)
            {
                if (m.Person.LastName[0] != alphaindex[0])
                {
                    tbl?.RemoveRow(0);
                    var pg = docx.InsertParagraph(alphaParagraph);
                    pg.KeepWithNextParagraph();
                    alphaindex = m.Person.LastName.Substring(0, 1);
                    pg.ReplaceText("{lastnamestartswith}", alphaindex);
                    tbl = docx.InsertTable(templatetbl);
                    c   = 0;
                }
                Debug.Assert(tbl != null, "tbl != null");
                if (c == 0) // start a new row
                {
                    row = tbl.InsertRow(templaterow);
                    row.BreakAcrossPages = false;
                    tbl.Rows.Add(row);
                }
                Debug.Assert(row != null, "row != null");
                var cell = row.Cells[c];
                DoCellReplacements(cell, m);
                if (++c == ncols)
                {
                    c = 0; // start a new row
                }
            }
            while (c > 0 && c < ncols && row != null)
            {
                DoEmptyCell(row.Cells[c++]);
            }

            tbl?.RemoveRow(0);                              // remove first row from last table worked on
            docx.RemoveParagraph(alphaParagraph);           // remove the alphaParagraph template
            docx.Tables[0].Remove();                        // remove the template table
            docx.Paragraphs[0].Remove(trackChanges: false); // remove the empty paragraph after the template table
        }
Exemplo n.º 2
0
        public static void RemoveEndMark(this DocX doc, string endMarkText)
        {
            var toRemoveParagraphs = doc.Paragraphs.Where(x => x.Text == endMarkText);

            foreach (var p in toRemoveParagraphs)
            {
                doc.RemoveParagraph(p);
            }
        }
Exemplo n.º 3
0
        private static void ReplaceEmail(string tag, string email, DocX doc)
        {
            var link = doc.AddHyperlink(email, new Uri($"mailto:{email}"));
            var p    = doc.InsertParagraph("");

            p.AppendHyperlink(link).Color(Color.Blue).UnderlineStyle(UnderlineStyle.singleLine);
            doc.ReplaceTextWithObject(tag, p.Hyperlinks.FirstOrDefault());
            doc.RemoveParagraph(p);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Добавляем титульный лист
        /// </summary>
        /// <param name="document"></param>
        private void AddTitle(DocX document)
        {
            document.Xml.Elements().First().FirstNode.Remove();

            var title = document.InsertParagraph("Титульный лист");

            title.FontSize(18).Bold().Alignment = Alignment.center;

            // Выводим в xml на верхний уровень для корректной работы InsertPageBreakAfterSelf
            document.RemoveParagraph(title);
            document.Xml.AddFirst(title.Xml);

            title.InsertPageBreakAfterSelf();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            DocX   sourceDoc = DocX.Load(@"E:/temp.docx");
            string text      = sourceDoc.Text;

            Console.WriteLine("Document size is {0}", text.Length);

            string encryptedText = "Hello world";

            byte[] encryptedBytes = Encoding.ASCII.GetBytes(encryptedText);
            bool[] encryptedBits  = Utils.ConvertToBits(encryptedBytes);
            if (encryptedBits.Length > text.Length)
            {
                throw new Exception("Encrypted text is larger than container");
            }

            ContainerElement[] container = new ContainerElement[text.Length];
            var shuffle = Utils.GetShuffled(container.Length);

            for (var i = 0; i < encryptedBits.Length; i++)
            {
                container[shuffle[i]] = encryptedBits[i] ? ContainerElement.Pos : ContainerElement.Neg;
            }
            Console.WriteLine("Start encrypting");
            //DocX resultDoc = DocX.Create(@"E:/tempRes.docx");
            DocX resultDoc = sourceDoc.Copy( );

            foreach (var a in  resultDoc.Paragraphs)
            {
                resultDoc.RemoveParagraph(a);
            }
            var lastContainerIndex = 0;

            foreach (var sourceParagraph in sourceDoc.Paragraphs)
            {
                Paragraph newParagraph = resultDoc.InsertParagraph( );
                foreach (var paragraphPart in sourceParagraph.MagicText)
                {
                    Paragraph append(string textPart)
                    {
                        if (paragraphPart.formatting == null)
                        {
                            return(newParagraph.Append(textPart));
                        }
                        return(newParagraph.Append(textPart, paragraphPart.formatting));
                    }

                    ContainerElement lastUsedElement = ContainerElement.Any;
                    int firstCharIndex = 0;
                    for (var j = 0; j < paragraphPart.text.Length; j++)
                    {
                        var containerIndex = lastContainerIndex + j;
                        if (container[containerIndex] != ContainerElement.Any)
                        {
                            if (lastUsedElement != ContainerElement.Any)
                            {
                                if (lastUsedElement != container[containerIndex])
                                {
                                    append(paragraphPart.text.Substring(firstCharIndex, j - firstCharIndex - 1)).Spacing(GenerateSpacing(paragraphPart, container[containerIndex] == ContainerElement.Neg));
                                    firstCharIndex = j;
                                }
                            }
                            lastUsedElement = container[containerIndex];
                        }
                    }
                    if (firstCharIndex < paragraphPart.text.Length)
                    {
                        append(paragraphPart.text.Substring(firstCharIndex, paragraphPart.text.Length - firstCharIndex)).Spacing(GenerateSpacing(paragraphPart, lastUsedElement == ContainerElement.Neg));
                    }
                    lastContainerIndex += paragraphPart.text.Length;
                }
                newParagraph.StyleName = sourceParagraph.StyleName;
            }
            Console.WriteLine("Saving");
            resultDoc.SaveAs(@"E:/tempRes.docx");
            Console.WriteLine("Press any key to exit...");
#if DEBUG
            Console.ReadKey( );
#endif
        }