public void Default_Numbered_Test()
        {
            Document doc        = new Document();
            int      totalcount = 10;

            //Add ten pages with default numbering
            for (int i = 0; i < totalcount; i++)
            {
                Page pg = new Page();
                doc.Pages.Add(pg);
                PageNumberLabel lbl = new PageNumberLabel();
                pg.Contents.Add(lbl);
            }

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                doc.LayoutComplete += Doc_LayoutCompleted;
                doc.SaveAsPDF(ms);
            }

            for (int i = 0; i < totalcount; i++)
            {
                int           pgNum = i + 1;
                PDFLayoutPage lpg   = layout.AllPages[i];

                PDFPageNumberData data = lpg.GetPageNumber();

                //Check the number
                Assert.AreEqual(pgNum, data.PageNumber);
                Assert.AreEqual(totalcount, data.LastPageNumber);
                Assert.AreEqual(pgNum, data.GroupNumber);
                Assert.AreEqual(totalcount, data.GroupLastNumber);
                Assert.AreEqual(pgNum.ToString(), data.ToString());
            }
        }
        public void DefaultWithSection_Numbered_Test()
        {
            Document doc        = new Document();
            int      totalcount = 10;

            //Add ten pages with default numbering
            for (int i = 0; i < totalcount; i++)
            {
                if (i == 5)
                {
                    Section section = new Section();
                    section.PageNumberStyle = PageNumberStyle.UppercaseLetters;

                    doc.Pages.Add(section);

                    for (int j = 0; j < 4; j++) //4 page breaks = 5 pages
                    {
                        PageBreak br = new PageBreak();
                        section.Contents.Add(br);
                    }
                }
                else
                {
                    Page pg = new Page();
                    doc.Pages.Add(pg);
                    PageNumberLabel lbl = new PageNumberLabel();
                    pg.Contents.Add(lbl);
                }
            }


            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                doc.LayoutComplete += Doc_LayoutCompleted;
                doc.SaveAsPDF(ms);
            }
            string[] allpages = new string[] { "1", "2", "3", "4", "5", "A", "B", "C", "D", "E", "6", "7", "8", "9" };
            string[] grptotal = new string[] { "5", "5", "5", "5", "5", "E", "E", "E", "E", "E", "9", "9", "9", "9" };

            for (int i = 0; i < allpages.Length; i++)
            {
                string expected = string.Format("Page {0} of {1} ({2} of {3})", allpages[i], grptotal[i], i + 1, allpages.Length);

                PDFLayoutPage     lpg  = layout.AllPages[i];
                PDFPageNumberData data = lpg.GetPageNumber();

                string actual = data.ToString("Page {0} of {1} ({2} of {3})");
                Assert.AreEqual(expected, actual);
                TestContext.WriteLine("Expected '{0}', Actual '{1}'", expected, actual);
            }
        }
        public void DocumentPrefixUpperAlpha_Numbered_Test()
        {
            Document doc = new Document();

            //Add a catch all number style definition for upper letter with Prefix
            StyleDefn pgNumStyle = new StyleDefn();

            pgNumStyle.AppliedType = typeof(Document);

            pgNumStyle.PageStyle.NumberStyle = PageNumberStyle.UppercaseLetters;
            doc.Styles.Add(pgNumStyle);


            int totalcount = 10;

            //Add ten pages with default numbering
            for (int i = 0; i < totalcount; i++)
            {
                Page pg = new Page();
                doc.Pages.Add(pg);
                PageNumberLabel lbl = new PageNumberLabel();
                pg.Contents.Add(lbl);
            }


            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                doc.LayoutComplete += Doc_LayoutCompleted;
                doc.SaveAsPDF(ms);
            }

            //Check the numbers
            for (int i = 0; i < totalcount; i++)
            {
                int           pgNum = i + 1;
                PDFLayoutPage lpg   = layout.AllPages[i];

                PDFPageNumberData data = lpg.GetPageNumber();

                //Check the number
                Assert.AreEqual(pgNum, data.PageNumber, "Page Number failed");
                Assert.AreEqual(totalcount, data.LastPageNumber, "Last Page number failed");
                Assert.AreEqual(pgNum, data.GroupNumber, "Group number failed");
                Assert.AreEqual(totalcount, data.GroupLastNumber, "Last Group number failed");

                //Should be upper letter with hash prefix
                string output = ((char)((int)'A' + i)).ToString();
                Assert.AreEqual(output, data.ToString(), "String result failed");
            }
        }
        public void Numbering_SimpleDocument()
        {
            Document doc = new Document();



            for (int i = 0; i < 20; i++)
            {
                Page pg = new Page();
                doc.Pages.Add(pg);
            }


            doc.ViewPreferences.PageDisplay = PageDisplayMode.Thumbnails;
            doc.LayoutComplete += Doc_LayoutComplete;

            using (var ms = new System.IO.MemoryStream())
                doc.SaveAsPDF(ms);
            var ldoc = this._layout;

            for (int i = 0; i < 20; i++)
            {
                PDFPageNumberData num = ldoc.GetNumbering(i);
                Assert.AreEqual(i + 1, num.GroupNumber);
                Assert.AreEqual(i + 1, num.PageNumber);
                Assert.AreEqual(20, num.LastPageNumber);
                Assert.AreEqual(20, num.GroupLastNumber);
                Assert.AreEqual((i + 1).ToString(), num.Label);
                Assert.AreEqual("20", num.LastLabel);
            }

            int last = -1;

            foreach (Scryber.Layout.PDFLayoutPage pg in ldoc.AllPages)
            {
                int index = pg.PageIndex;
                Assert.AreEqual(last + 1, index);
                last = index;
                PDFPageNumberData num = pg.GetPageNumber();

                Assert.AreEqual(index + 1, num.GroupNumber);
                Assert.AreEqual(index + 1, num.PageNumber);
                Assert.AreEqual(20, num.LastPageNumber);
                Assert.AreEqual(20, num.GroupLastNumber);
                Assert.AreEqual((index + 1).ToString(), num.Label);
                Assert.AreEqual("20", num.LastLabel);
            }
        }
        /// <summary>
        /// Gets the full text for the page label
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. If there is no PageFormat, then the current page index in the label style for this page is used
        /// 2. If there is a format then the format is used with the following fields...
        ///                     {0} = page label, {1} = total label for this pages numbering group,
        ///                     {2} global page index, {3} global page count,
        ///                     {4} index in group, {5} group count
        /// </remarks>
        protected virtual string GetDisplayText(int pageindex, Style style, bool rendering)
        {
            if (null == this._doc)
            {
                throw new ArgumentNullException("The PageNumberLabel does not have a layout document associated with it, so cannot get the page number in the document");
            }

            PDFPageNumberData num = this._doc.GetNumbering(pageindex);

            if (null == num)
            {
                throw new NullReferenceException("No numbering data was returned for the specified page index '" + pageindex + "'");
            }

            string format = GetPageFormat(style);

            //If we just want the page label - we can skip the rest and return
            if (string.IsNullOrEmpty(format))
            {
                return(num.Label);
            }



            //If we are not rendering then we don't actually know the group or total page count
            //So we use the hints from the style or our default values
            if (!rendering)
            {
                int grp = style.GetValue(StyleKeys.PageNumberGroupHintKey, DefaultGroupPageCountHint);
                num.GroupLastNumber = grp;
                num.LastPageNumber  = style.GetValue(StyleKeys.PageNumberTotalHintKey, DefaultTotalPageCountHint);
                num.LastLabel       = num.GroupOptions.GetPageLabel(grp);
            }

            return(num.ToString(format));
        }
        public void PageNumbering_SimpleDocument()
        {
            Document doc = new Document();


            for (int i = 0; i < 20; i++)
            {
                Page pg = new Page();
                doc.Pages.Add(pg);
            }

            doc.ViewPreferences.PageDisplay = PageDisplayMode.Thumbnails;
            doc.LayoutComplete += Doc_LayoutComplete;

            using (var ms = DocStreams.GetOutputStream("Numbering_Simple.pdf"))
                doc.SaveAsPDF(ms);
            var ldoc = this._layout;
            int last = -1;

            foreach (Scryber.Layout.PDFLayoutPage pg in ldoc.AllPages)
            {
                int index = pg.PageIndex;
                Assert.AreEqual(last + 1, index);
                last = index;

                //Get the page number details for the page
                PDFPageNumberData num = pg.GetPageNumber();

                Assert.AreEqual(index + 1, num.GroupNumber, "Group number of page was not correct");
                Assert.AreEqual(index + 1, num.PageNumber, "Page number was not correct");
                Assert.AreEqual(20, num.LastPageNumber, "Last page number was not correct");
                Assert.AreEqual(20, num.GroupLastNumber, "Group last number was not correct");
                Assert.AreEqual((index + 1).ToString(), num.Label, "Page label was not correct");
                Assert.AreEqual("20", num.LastLabel, "Last page label was not correct");
            }
        }
Exemple #7
0
        public void PageCollection_Nested()
        {
            // structure                  |  None      | Start                 | Reset Numbers                                                | Back To Start sequence        | Back to None
            // output                     | 0   1   2  | 3    4      5     6   | 7      8        9       10     11       12      13      14   | 15       16      17      18   | 19  20  21
            string[] expectedLabels     = { "", "", "", "i", "ii", "iii", "iv", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "v", "vi", "vii", "viii", "", "", "" };
            string[] expectedLastLabels = { "", "", "", "iv", "iv", "iv", "iv", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "viii", "", "", "" };

            PDFPageNumbers col = new PDFPageNumbers();

            col.StartNumbering(null);

            Style none = new Style();

            none.PageStyle.NumberStyle = PageNumberStyle.None;

            PDFPageNumberGroup noneGrp = col.PushPageNumber(none.CreatePageNumberOptions());
            int noneReg = 0;

            col.Register(noneReg);
            //keep none at the top - no unregister

            Style intro = new Style();

            intro.PageStyle.NumberStyle = PageNumberStyle.LowercaseRoman;
            PDFPageNumberGroup introGrp = col.PushPageNumber(intro.CreatePageNumberOptions());

            int introReg = 3;

            col.Register(introReg);
            //col.UnRegister(introUnreg);
            //col.PopNumberStyle(grp);

            //no style, just start index
            Style resetStartIndex = new Style();

            resetStartIndex.PageStyle.NumberStartIndex = 1;
            PDFPageNumberGroup resetNumGrp = col.PushPageNumber(resetStartIndex.CreatePageNumberOptions());

            int pgReg   = 7;
            int pgUnreg = 14;

            col.Register(pgReg);
            col.UnRegister(pgUnreg);
            col.PopNumberStyle(resetNumGrp);

            int introUnreg = 18;

            col.UnRegister(introUnreg);
            col.PopNumberStyle(introGrp);

            int noneUnReg = 21;

            col.UnRegister(noneUnReg);
            col.PopNumberStyle(noneGrp);

            col.EndNumbering();

            for (int i = 0; i < 21; i++)
            {
                PDFPageNumberData number = col.GetPageData(i);
                Assert.AreEqual(expectedLabels[i], number.Label, "Page labels did not match for index " + i);
                Assert.AreEqual(expectedLastLabels[i], number.LastLabel, "Page Last labels did not match for index " + i);
            }
        }
        public void PageNumbering_FullStyledMultiSectionDocument()
        {
            Document doc = new Document();


            for (int i = 0; i < 4; i++)
            {
                Section group = new Section();
                if (i == 0)
                {
                    group.Style.PageStyle.NumberStyle = PageNumberStyle.LowercaseLetters;
                }
                if (i == 1)
                {
                    group.Style.PageStyle.NumberStyle      = PageNumberStyle.LowercaseRoman;
                    group.Style.PageStyle.NumberStartIndex = 1;
                }
                else if (i == 2)
                {
                    group.Style.PageStyle.NumberStyle      = PageNumberStyle.UppercaseLetters;
                    group.Style.PageStyle.NumberStartIndex = 2;
                }
                doc.Pages.Add(group);

                for (int j = 0; j < 4; j++)
                {
                    PageNumberLabel pgNum = new PageNumberLabel();
                    group.Contents.Add(pgNum);

                    PageBreak br = new PageBreak();
                    group.Contents.Add(br);
                }
            }


            doc.ViewPreferences.PageDisplay = PageDisplayMode.Thumbnails;
            doc.LayoutComplete += Doc_LayoutComplete;

            using (var ms = DocStreams.GetOutputStream("Numbering_StyledMultiSection.pdf"))
                doc.SaveAsPDF(ms);
            var ldoc = this._layout;

            // page indices                          0,    1,   2,   3,   4,   5,   6,    7,    8,    9,   10,  11 , 12,  13,  14,  15,  16,  17,  18,   19
            string[] expectedlabels = new string[] { "a", "b", "c", "d", "e", "i", "ii", "iii", "iv", "v", "B", "C", "D", "E", "F", "1", "2", "3", "4", "5" };

            int index = -1;

            List <string> actuallabels = new List <string>();

            Assert.AreEqual(20, ldoc.AllPages.Count);
            int last = -1;

            foreach (Scryber.Layout.PDFLayoutPage pg in ldoc.AllPages)
            {
                index = pg.PageIndex;
                Assert.AreEqual(last + 1, index, "Page numbers are not in synch");
                last = index;

                //Get the page number details for the page
                PDFPageNumberData num = pg.GetPageNumber();
                actuallabels.Add(num.Label);

                if (index < 5)
                {
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                    Assert.AreEqual(expectedlabels[index], num.Label);
                    Assert.AreEqual("e", num.LastLabel);
                }
                else if (index < 10)
                {
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);

                    index -= 5;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                    Assert.AreEqual(Scryber.Utilities.NumberHelper.GetRomanLower(index + 1), num.Label);
                    Assert.AreEqual("v", num.LastLabel);
                }
                else if (index < 15)
                {
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);
                    index -= 10;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                    Assert.AreEqual(Scryber.Utilities.NumberHelper.GetLetterUpper(index + 2), num.Label);
                    Assert.AreEqual("F", num.LastLabel);
                }
                else
                {
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);

                    //labels are based from the continuation of the sequence
                    Assert.AreEqual(expectedlabels[index], num.Label);
                    Assert.AreEqual("5", num.LastLabel);

                    //group is one based from the start of that sequence
                    index -= 15;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                }
            }
            string fullexpected = string.Join(", ", expectedlabels);
            string fullactual   = string.Join(", ", actuallabels);

            Assert.AreEqual(fullexpected, fullactual);
        }
        public void PageNumbering_MultiSectionDocument()
        {
            Document doc = new Document();


            // numbering                           | Default                 | Lower roman                | Upper letter with prefix        | Back to default
            // page indices                          0,    1,   2,   3,   4,   5,   6,     7,    8,    9,   10,    11 ,   12,    13,    14,   15,  16,  17,  18,   19
            string[] expectedlabels = new string[] { "1", "2", "3", "4", "5", "i", "ii", "iii", "iv", "v", "B", "C", "D", "E", "F", "6", "7", "8", "9", "10" };

            for (int i = 0; i < 4; i++)
            {
                Section group = new Section();
                if (i == 1)
                {
                    group.Style.PageStyle.NumberStyle      = PageNumberStyle.LowercaseRoman;
                    group.Style.PageStyle.NumberStartIndex = 1;
                }
                else if (i == 2)
                {
                    group.Style.PageStyle.NumberStyle      = PageNumberStyle.UppercaseLetters;
                    group.Style.PageStyle.NumberStartIndex = 2;
                }
                doc.Pages.Add(group);

                for (int j = 0; j < 4; j++)
                {
                    PageBreak br = new PageBreak();
                    group.Contents.Add(br);
                }
            }


            doc.ViewPreferences.PageDisplay = PageDisplayMode.Thumbnails;
            doc.LayoutComplete += Doc_LayoutComplete;

            using (var ms = DocStreams.GetOutputStream("Numbering_MultiSection.pdf"))
                doc.SaveAsPDF(ms);
            var ldoc  = this._layout;
            int index = -1;

            List <string> actuallabels = new List <string>();

            Assert.AreEqual(20, ldoc.AllPages.Count);


            int last = -1;

            foreach (Scryber.Layout.PDFLayoutPage pg in ldoc.AllPages)
            {
                index = pg.PageIndex;
                Assert.AreEqual(last + 1, index, "Page numbers are not in synch");
                last = index;

                //Get the page number details for the page
                PDFPageNumberData num = pg.GetPageNumber();
                actuallabels.Add(num.Label);

                if (index < 5)
                {
                    Assert.AreEqual(index + 1, num.GroupNumber, "First block page group number was wrong");
                    Assert.AreEqual(index + 1, num.PageNumber, "First block Global page number was wrong");
                    Assert.AreEqual(20, num.LastPageNumber, "First block last page number was wrong");
                    Assert.AreEqual(5, num.GroupLastNumber, "First block last group number was wrong");
                    Assert.AreEqual(expectedlabels[index], num.Label, "First block page label was wrong");
                    Assert.AreEqual("5", num.LastLabel, "First block last label was wrong");
                }
                else if (index < 10)
                {
                    Assert.AreEqual(index + 1, num.PageNumber, "First block page group number was wrong");
                    Assert.AreEqual(20, num.LastPageNumber, "First block Global page number was wrong");

                    index -= 5;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                    Assert.AreEqual(Scryber.Utilities.NumberHelper.GetRomanLower(index + 1), num.Label);
                    Assert.AreEqual("v", num.LastLabel);
                }
                else if (index < 15)
                {
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);
                    index -= 10;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);
                    Assert.AreEqual(Scryber.Utilities.NumberHelper.GetLetterUpper(index + 2), num.Label);
                    Assert.AreEqual("F", num.LastLabel);
                }
                else
                {
                    Assert.AreEqual(index + 1, num.PageNumber);
                    Assert.AreEqual(20, num.LastPageNumber);

                    //group is one based from the start of that sequence
                    index -= 15;
                    Assert.AreEqual(index + 1, num.GroupNumber);
                    Assert.AreEqual(5, num.GroupLastNumber);

                    //labels are based from the start of that entire sequence
                    index += 5;
                    Assert.AreEqual((index + 1).ToString(), num.Label);
                    Assert.AreEqual("10", num.LastLabel);
                }
            }
            string fullexpected = string.Join(", ", expectedlabels);
            string fullactual   = string.Join(", ", actuallabels);

            Assert.AreEqual(fullexpected, fullactual);

            //Check the page registrations

            // numbering                           | Default                 | Lower roman                | Upper letter with prefix        | Back to default
            // page indices                          0,    1,   2,   3,   4,   5,   6,     7,    8,    9,   10,    11 ,   12,    13,    14,   15,  16,  17,  18,   19

            PDFPageNumbers nums = ldoc.Numbers;

            Assert.AreEqual(4, nums.Registrations.Count);

            //Default 1-4
            PDFPageNumberRegistration reg = nums.Registrations[0];

            Assert.AreEqual(0, reg.FirstPageIndex);
            Assert.AreEqual(4, reg.LastPageIndex);
            Assert.AreEqual(0, reg.PreviousLinkedRegistrationPageCount);
            Assert.AreEqual(true, reg.IsClosed);

            //Lower Roman 5-9
            reg = nums.Registrations[1];
            Assert.AreEqual(5, reg.FirstPageIndex);
            Assert.AreEqual(9, reg.LastPageIndex);
            Assert.AreEqual(0, reg.PreviousLinkedRegistrationPageCount);
            Assert.AreEqual(true, reg.IsClosed);

            //Lower Upper Letter with prefix 10-14
            reg = nums.Registrations[2];
            Assert.AreEqual(10, reg.FirstPageIndex);
            Assert.AreEqual(14, reg.LastPageIndex);
            Assert.AreEqual(0, reg.PreviousLinkedRegistrationPageCount);
            Assert.AreEqual(true, reg.IsClosed);

            //Lower Default 15-19
            reg = nums.Registrations[3];
            Assert.AreEqual(15, reg.FirstPageIndex);
            Assert.AreEqual(19, reg.LastPageIndex);
            Assert.AreEqual(5, reg.PreviousLinkedRegistrationPageCount); //restarting from the last default onto this page
            Assert.AreEqual(true, reg.IsClosed);
        }