Пример #1
0
        private static PdfPTable GenerateTableOfContents(PdfEvents tocEvent, PdfStamper stamper, int tocSize)
        {
            var tableOfContents = new PdfPTable(2);

            tableOfContents.SetWidths(new[] { 10, 1 });
            tableOfContents.WidthPercentage    = 100;
            tableOfContents.DefaultCell.Border = Rectangle.NO_BORDER;

            foreach (var item in tocEvent.Toc)
            {
                tableOfContents.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
                tableOfContents.DefaultCell.PaddingLeft         = 10 * (item.Level - 1);

                Font font;
                if (item.Level == 0)
                {
                    tableOfContents.DefaultCell.PaddingBottom = 10;
                    tableOfContents.DefaultCell.PaddingTop    = 10;
                    font = PdfFonts.Bold;
                }
                else
                {
                    tableOfContents.DefaultCell.PaddingBottom = 2;
                    tableOfContents.DefaultCell.PaddingTop    = 2;
                    font = PdfFonts.Normal;
                }

                var chunk  = new Chunk(item.Title, font);
                var action = PdfAction.GotoLocalPage(item.Page, new PdfDestination(PdfDestination.FIT), stamper.Writer);
                chunk.SetAction(action);
                tableOfContents.AddCell(new Phrase(chunk));

                tableOfContents.DefaultCell.PaddingLeft         = 0;
                tableOfContents.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;

                chunk = new Chunk(string.Format("{0}", item.Page + tocSize), font);
                tableOfContents.AddCell(new Phrase(chunk));
            }
            return(tableOfContents);
        }
Пример #2
0
        public Stream GetPdf()
        {
            var document    = CreateDefaultDocument();
            var tocDocument = CreateDefaultDocument();

            var stream = new MemoryStream();

            _pdfWriter = PdfWriter.GetInstance(document, stream);
            _pdfWriter.SetLinearPageMode();

            var tocStream = new MemoryStream();
            var tocWriter = PdfWriter.GetInstance(tocDocument, tocStream);

            tocWriter.SetLinearPageMode();

            var tocEvent = new PdfEvents(tocDocument, RootDirectory);

            _pdfWriter.PageEvent           = tocEvent;
            _pdfWriter.StrictImageSequence = true;

            document.Open();
            tocDocument.Open();

            AddMetaData(document);
            AddChapters(document);
            document.Close();

            var fontFooter = PdfFonts.Small;
            var footer     = new Phrase(string.Format("{0}: {1}", Documentation.Project.ProjectNumber, Documentation.Name), fontFooter);
            var pdfReader  = new PdfReader(stream.ToArray());

            stream = new MemoryStream();
            var stamper = new PdfStamper(pdfReader, stream);

            var tableOfContents = GenerateTableOfContents(tocEvent, stamper, 0);

            tocDocument.Add(tableOfContents);
            tocDocument.Close();

            var tocReader = new PdfReader(tocStream.ToArray());
            var tocSize   = tocReader.NumberOfPages;

            var toc       = new ColumnText(null);
            var frontPage = new ColumnText(null);

            var hasFrontPage = Documentation.DocumentationChapters.Any(dc => dc.Chapter is GeneratedChapter && dc.Chapter.Name == "Forside");

            tableOfContents = GenerateTableOfContents(tocEvent, stamper, tocSize + (hasFrontPage ? 2 : 0));
            toc.AddElement(tableOfContents);

            var page        = stamper.GetImportedPage(pdfReader, 1);
            int currentPage = 0;

            while (true)
            {
                stamper.InsertPage(++currentPage, pdfReader.GetPageSize(1));
                stamper.GetUnderContent(currentPage).AddTemplate(page, 0, 0);
                toc.Canvas = stamper.GetOverContent(currentPage);
                toc.SetSimpleColumn(60, 72, PageSize.A4.Width - 60, PageSize.A4.Height - 72);
                if (!ColumnText.HasMoreText(toc.Go()))
                {
                    break;
                }
            }

            if (hasFrontPage)
            {
                frontPage.AddElement(new FrontPageBuilder(Documentation, RootDirectory).GetContent());

                page        = stamper.GetImportedPage(pdfReader, 1);
                currentPage = 0;
                while (true)
                {
                    stamper.InsertPage(++currentPage, pdfReader.GetPageSize(1));
                    stamper.GetUnderContent(currentPage).AddTemplate(page, 0, 0);
                    frontPage.Canvas = stamper.GetOverContent(currentPage);
                    frontPage.SetSimpleColumn(60, 72, PageSize.A4.Width - 60, PageSize.A4.Height - 72);
                    if (!ColumnText.HasMoreText(frontPage.Go()))
                    {
                        break;
                    }
                }

                stamper.InsertPage(++currentPage, pdfReader.GetPageSize(1));
                stamper.GetUnderContent(currentPage).AddTemplate(page, 0, 0);
            }

            int totalPages = pdfReader.NumberOfPages;

            for (int i = 3; i <= totalPages; ++i)
            {
                var canvas = stamper.GetOverContent(i);
                var table  = new PdfPTable(2);

                var rotation = stamper.Reader.GetPageRotation(i);

                table.SetWidths(new[] { 10, 1 });
                table.TotalWidth                      = (rotation == 0 ? PageSize.A4.Width : PageSize.A4.Height) - 130;
                table.DefaultCell.Border              = Rectangle.NO_BORDER;
                table.DefaultCell.FixedHeight         = 20;
                table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
                table.AddCell(footer);

                table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(new Phrase(i.ToString(CultureInfo.InvariantCulture), fontFooter));

                table.WriteSelectedRows(0, -1, 0, -1, 60, 40, canvas);
            }

            stamper.Close();

            var outStream = new MemoryStream(stream.ToArray());

            return(outStream);
        }