Пример #1
0
        private void MakeDoc1(C1PrintDocument doc)
        {
            // create the document title
            RenderParagraph docTitle = new RenderParagraph();

            docTitle.Content.AddText("This document demonstates the use of ");
            docTitle.Content.AddText("RenderToc", Color.Blue);
            docTitle.Content.AddText(" object to create the table of content.");
            doc.Body.Children.Add(docTitle);

            // in this cycle we generate the document chapters, each chapter
            // has some parts, each chapter and part has an entry in the RenderToc object
            const int chapterCount = 10; // chapter count
            const int partCount    = 5;  // parts count

            // generate sample text that will serve as introduction to chapter
            StringBuilder chapterIntroduction = new StringBuilder("Introduction");

            for (int i = 0; i < 40; i++)
            {
                chapterIntroduction.Append(" introduction");
            }
            chapterIntroduction.Append(".\r\n");

            // generate sample text that will be displayed as content of part of chapter
            StringBuilder partContent = new StringBuilder("Content of part");

            for (int i = 0; i < 80; i++)
            {
                partContent.Append(" content of part");
            }
            partContent.Append(".\r\n");

            // create an instance of RenderToc object
            RenderToc toc = new RenderToc();

            toc.BreakAfter = BreakEnum.Page;

            // Create styles for chaprters and parts
            Style chapterTitleStyle = doc.Style.Children.Add();

            chapterTitleStyle.Font           = new Font("Verdana", 15, FontStyle.Bold);
            chapterTitleStyle.BackColor      = Color.LightSteelBlue;
            chapterTitleStyle.Spacing.Bottom = "5mm";
            Style partTitleStyle = doc.Style.Children.Add();

            partTitleStyle.Font           = new Font("Tahoma", 13);
            partTitleStyle.Spacing.Top    = "3mm";
            partTitleStyle.Spacing.Bottom = "1mm";

            // loop over chapters
            for (int c = 1; c < chapterCount; c++)
            {
                // each chapter will be represented as a RenderArea object
                RenderArea chapter = new RenderArea();
                if (c < chapterCount - 1)
                {
                    chapter.BreakAfter = BreakEnum.Page;
                }

                RenderText chapterTitle = new RenderText(string.Format("Chapter {0}", c), chapterTitleStyle);
                chapter.Children.Add(chapterTitle);
                chapter.Children.Add(new RenderText(chapterIntroduction.ToString(), AlignHorzEnum.Justify));

                // add item for the chapter to the RenderToc
                toc.AddItem(chapterTitle.Text, chapterTitle, 1);

                // loop over the current chapter's parts
                for (int p = 1; p < partCount; p++)
                {
                    RenderText partTitle = new RenderText(string.Format("Chapter {0} part {1}", c, p), partTitleStyle);
                    chapter.Children.Add(partTitle);
                    chapter.Children.Add(new RenderText(partContent.ToString(), AlignHorzEnum.Justify));

                    // add item for the chapter part to the RenderToc
                    toc.AddItem(string.Format("Part {0}", p), partTitle, 2);
                }

                // add the chapter to the document
                doc.Body.Children.Add(chapter);
            }

            // insert the RenderToc into the document immediatedly after the title
            doc.Body.Children.Insert(1, toc);
        }
Пример #2
0
        /// <summary>
        /// Fills a C1MultiDocument or a C1PrintDocument passed to it with listings of
        /// all files in the specified directory and its subdirectories matching the specified mask.
        /// </summary>
        /// <param name="theDoc">A C1MultiDocument or a C1PrintDocument.</param>
        /// <param name="dir">Directory containing the files to list.</param>
        /// <param name="mask">The files mask (e.g. "*.cs").</param>
        /// <param name="pf">Progress form.</param>
        public void MakeMultiDocument(object theDoc, string dir, string mask, ProgressForm pf)
        {
            // Find out what kind of document we're creating:
            _mdoc = theDoc as C1MultiDocument;
            _sdoc = theDoc as C1PrintDocument;
            if (_mdoc == null && _sdoc == null)
            {
                throw new Exception("Unsupported document type.");
            }

            _dir  = Path.GetFullPath(dir);
            _mask = mask;

            // Set up the document:
            if (_mdoc != null)
            {
                _mdoc.Clear();
                _mdoc.DoEvents = true;
            }
            else
            {
                _sdoc.Clear();
                _sdoc.DoEvents = true;
                SetupDoc(_sdoc);
            }

            // Show initial progress:
            pf.SetProgress(string.Format("Reading {0}...", _dir), 0);

            // For progress indicator only - get the list of all subdirectories:
            // long allDirsCount = Directory.GetDirectories(_dir, "*", SearchOption.AllDirectories).LongLength;
            // long allDirsIdx = 0;

            // Create TOC render object that will list directories and files:
            RenderToc toc = new RenderToc();

            // Add a TOC directory entry in "directory added" event:
            DirAdded += (doc, dirRo, dirName, level) =>
            {
                C1Anchor aDir = new C1Anchor(string.Format("d{0}", dirName.GetHashCode()));
                dirRo.Anchors.Add(aDir);

                // Add a TOC item for the directory (full name for root dir, own name for subdirs):
                string        tocName = dirName == _dir ? dirName : Path.GetFileName(dirName);
                RenderTocItem rti     = toc.AddItem(tocName, new C1Hyperlink(new C1LinkTargetAnchor(aDir.Name)), level);
                // Bold directory TOC entries:
                rti.Style.FontBold = true;
                // duplicate TOC entry in OUTLINE:
                OutlineNode outlineNode = new OutlineNode(dirName, dirRo);
                doc.Outlines.Add(outlineNode); // in this mode, all directory nodes are top-level

                // update overall progress:
                pf.SetProgress(string.Format("Reading {0}...", dirName));
                if (pf.Cancelled)
                {
                    throw new Exception("Directory scan aborted.");
                }

                return(outlineNode);
            };

            // Add a TOC file entry in "file added" event:
            FileAdded += (doc, dirOutlineNode, fileRo, fileName, level) =>
            {
                C1Anchor aFile = new C1Anchor(string.Format("f{0}", fileName.GetHashCode()));
                fileRo.Anchors.Add(aFile);
                string tocItemName = Path.GetFileName(fileName);
                toc.AddItem(tocItemName, new C1Hyperlink(new C1LinkTargetAnchor(aFile.Name)), level);
                // duplicate TOC entry in OUTLINE:
                if (dirOutlineNode == null)
                {
                    doc.Outlines.Add(tocItemName, fileRo); // top-level entry
                }
                else
                {
                    dirOutlineNode.Children.Add(tocItemName, fileRo); // nested entry
                }
            };

            // Create the common index:
            RenderIndex index = new RenderIndex();
            // Init keywords:
            Dictionary <string, IndexEntry> indexEntries = new Dictionary <string, IndexEntry>();

            // For this sample, we will build an index of all KnownColor names:
            string[] colorNames = Enum.GetNames(typeof(KnownColor));
            foreach (string keyword in colorNames)
            {
                indexEntries.Add(keyword, new IndexEntry(keyword));
            }

            // Add an index entry for each key word in line:
            LineAdded += (lineRo, fileName, line, lineNo) =>
            {
                var      words = line.Split(s_delims, StringSplitOptions.RemoveEmptyEntries);
                C1Anchor a     = null;
                foreach (string word in words)
                {
                    if (indexEntries.ContainsKey(word))
                    {
                        if (a == null)
                        {
                            a = new C1Anchor(string.Format("k{0}{1}", fileName.GetHashCode(), lineNo));
                            lineRo.Anchors.Add(a);
                        }
                        indexEntries[word].Occurrences.Add(new IndexEntryOccurrence(new C1LinkTargetAnchor(a.Name)));
                    }
                }
            };


            // Add listings of files to the document:
            ListDir(_dir, 1);

            // insert TOC at the top:
            RenderText tocHeader = new RenderText("Table of Contents");

            tocHeader.Style.Spacing.Bottom = "3mm";
            tocHeader.Style.TextAlignHorz  = AlignHorzEnum.Center;
            tocHeader.Style.FontSize       = 12;
            tocHeader.Style.FontBold       = true;
            if (_mdoc != null)
            {
                C1PrintDocument docToc = new C1PrintDocument();
                docToc.Body.Children.Add(tocHeader);
                docToc.Body.Children.Add(toc);
                _mdoc.Items.Insert(0, docToc);
                docToc = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            else
            {
                toc.BreakAfter = BreakEnum.Page;
                _sdoc.Body.Children.Insert(0, toc);
                _sdoc.Body.Children.Insert(0, tocHeader);
            }

            // insert word index at the bottom:
            RenderText indexHeader = new RenderText("Index of Known Colors");

            indexHeader.Style.Spacing.Bottom = "3mm";
            indexHeader.Style.TextAlignHorz  = AlignHorzEnum.Center;
            indexHeader.Style.FontSize       = 12;
            indexHeader.Style.FontBold       = true;
            index.HeadingStyle.FontSize     += 1;
            index.HeadingStyle.FontBold      = true;
            index.HeadingStyle.Padding.All   = "2mm";
            // add index entries that have some occurrences:
            foreach (IndexEntry ie in indexEntries.Values)
            {
                if (ie.HasOccurrences)
                {
                    index.Entries.Add(ie);
                }
            }
            if (_mdoc != null)
            {
                C1PrintDocument docIndex = new C1PrintDocument();
                docIndex.Body.Children.Add(indexHeader);
                docIndex.Body.Children.Add(index);
                _mdoc.Items.Add(docIndex);
                docIndex = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            else
            {
                indexHeader.BreakBefore = BreakEnum.Page;
                _sdoc.Body.Children.Add(indexHeader);
                _sdoc.Body.Children.Add(index);
            }

            // we're done!
            pf.SetProgress(string.Format("Reading {0}...", _dir), 1);
        }
Пример #3
0
        private void MakeDoc1(C1PrintDocument doc)
        {
            // title of document
            RenderText rt = new RenderText("Stacking and positioning of objects");

            rt.Style.Borders.Bottom = new LineDef("1mm", Color.Black);
            doc.Body.Children.Add(rt);

            // document will contains the TOC
            RenderToc toc = new RenderToc();

            // 1. Demonstrate StackingRulesEnum.BlockTopToBottom
            RenderText title = new RenderText("Demonstrates StackingRulesEnum.BlockTopToBottom stacking");

            title.BreakBefore = BreakEnum.Page;
            doc.Body.Children.Add(title);
            RenderArea area = new RenderArea();

            area.Stacking = StackingRulesEnum.BlockTopToBottom;
            for (int i = 1; i <= 10; i++)
            {
                area.Children.Add(CreateObj(string.Format("OBJECT {0}", i)));
            }
            doc.Body.Children.Add(area);

            toc.AddItem("Stacking", title, 1);
            toc.AddItem("StackingRulesEnum.BlockTopToBottom", title, 2);

            // 2. Demonstrate the StackingRulesEnum.BlockLeftToRight
            title             = new RenderText("Demonstrates StackingRulesEnum.BlockLeftToRight stacking");
            title.BreakBefore = BreakEnum.Page;
            doc.Body.Children.Add(title);
            area = new RenderArea();
            // by default the RenderArea has a width = "parent.width" so we set it to
            // auto to show all child objects
            area.Width = Unit.Auto;
            // by default the RenderArea does not split by horizontally...
            area.SplitHorzBehavior = SplitBehaviorEnum.SplitIfNeeded;
            area.Stacking          = StackingRulesEnum.BlockLeftToRight;
            for (int i = 1; i <= 4; i++)
            {
                area.Children.Add(CreateObj(string.Format("OBJECT {0}", i)));
            }
            doc.Body.Children.Add(area);

            toc.AddItem("StackingRulesEnum.BlockLeftToRight", title, 2);

            // 3. Demonstrate the StackingRulesEnum.InlineLeftToRight
            title             = new RenderText("Demonstrates StackingRulesEnum.InlineLeftToRight stacking");
            title.BreakBefore = BreakEnum.Page;
            doc.Body.Children.Add(title);
            area          = new RenderArea();
            area.Stacking = StackingRulesEnum.InlineLeftToRight;
            for (int i = 1; i <= 10; i++)
            {
                area.Children.Add(CreateObj(string.Format("OBJECT {0}", i)));
            }
            doc.Body.Children.Add(area);

            toc.AddItem("StackingRulesEnum.InlineLeftToRight", title, 2);

            // 4. The positioning of objects
            title             = new RenderText("Each object starting with the second one is positioned relative to the previous object.");
            title.BreakBefore = BreakEnum.Page;
            doc.Body.Children.Add(title);

            doc.Body.Children.Add(CreateObj("FIRST OBJECT"));
            for (int i = 2; i <= 5; i++)
            {
                RenderObject ro = CreateObj(string.Format("OBJECT {0}", i));
                ro.X = "prev.Left + 1cm";
                ro.Y = "prev.Top + 1cm";
                doc.Body.Children.Add(ro);
            }

            toc.AddItem("Positioning", title, 1);

            doc.Body.Children.Insert(1, toc);
        }