Exemplo n.º 1
0
// ---------------------------------------------------------------------------

        /**
         * Creates a PDF document.
         */
        public byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream()) {
                using (var c = AdoDB.Provider.CreateConnection()) {
                    c.ConnectionString = AdoDB.CS;
                    c.Open();
                    // step 1
                    using (Document document = new Document(PageSize.A5)) {
                        // step 2
                        PdfWriter writer = PdfWriter.GetInstance(document, ms);
                        // step 3
                        document.Open();
                        // step 4
                        int[] start = new int[3];
                        for (int i = 0; i < 3; i++)
                        {
                            start[i] = writer.PageNumber;
                            AddParagraphs(document, c, SQL[i], FIELD[i]);
                            document.NewPage();
                        }
                        PdfPageLabels labels = new PdfPageLabels();
                        labels.AddPageLabel(start[0], PdfPageLabels.UPPERCASE_LETTERS);
                        labels.AddPageLabel(start[1], PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
                        labels.AddPageLabel(
                            start[2], PdfPageLabels.DECIMAL_ARABIC_NUMERALS,
                            "Movies-", start[2] - start[1] + 1
                            );
                        writer.PageLabels = labels;
                    }
                    return(ms.ToArray());
                }
            }
        }
Exemplo n.º 2
0
        public void AddSheet(string pdfFile, string addSheetFile, int numberInSheetFile, int numberInPdfFile, string destFile)
        {
            var sheet  = new PdfReader(addSheetFile);
            var pdf    = new PdfReader(pdfFile);
            var doc    = new Document();
            var copier = new PdfCopy(doc, new FileStream(destFile, FileMode.Create));

            doc.Open();
            var pdfLabels = PdfPageLabels.GetPageLabels(pdf);

            if (pdfLabels?.Length != pdf.NumberOfPages)
            {
                pdfLabels = null;
            }
            var sheetLabel = PdfPageLabels.GetPageLabels(sheet);

            if (sheetLabel?.Length != sheet.NumberOfPages)
            {
                sheetLabel = null;
            }
            var labels = new PdfPageLabels();

            for (var i = 1; i < numberInPdfFile; i++)
            {
                var page = copier.GetImportedPage(pdf, i);
                copier.AddPage(page);
                if (pdfLabels != null)
                {
                    var label = pdfLabels[i - 1];
                    labels.AddPageLabel(i, PdfPageLabels.EMPTY, label);
                }
            }

            copier.AddPage(copier.GetImportedPage(sheet, numberInSheetFile));
            if (sheetLabel != null)
            {
                labels.AddPageLabel(numberInPdfFile, PdfPageLabels.EMPTY, sheetLabel[numberInSheetFile - 1]);
            }
            else
            {
                labels.AddPageLabel(numberInPdfFile, PdfPageLabels.EMPTY, "бланк");
            }
            for (var i = numberInPdfFile; i < pdf.NumberOfPages + 1; i++)
            {
                copier.AddPage(copier.GetImportedPage(pdf, i));
                if (pdfLabels != null)
                {
                    var label = pdfLabels[i - 1];
                    labels.AddPageLabel(i + 1, PdfPageLabels.EMPTY, label);
                }
            }

            copier.PageLabels = labels;
            doc.Close();
            copier.Close();
            pdf.Close();
            sheet.Close();
        }
Exemplo n.º 3
0
        public Chap1111()
        {
            Console.WriteLine("Chapter 11 example 11: page labels");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            try {
                // step 2:
                // we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1111.pdf", FileMode.Create));
                // step 3: we open the document
                document.Open();
                // step 4:
                // we add some content
                for (int k = 1; k <= 10; ++k)
                {
                    document.Add(new Paragraph("This document has the logical page numbers: i,ii,iii,iv,1,2,3,A-8,A-9,A-10\nReal page " + k));
                    document.NewPage();
                }
                PdfPageLabels pageLabels = new PdfPageLabels();
                pageLabels.AddPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
                pageLabels.AddPageLabel(5, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
                pageLabels.AddPageLabel(8, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "A-", 8);
                writer.PageLabels = pageLabels;
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }

            // step 5: we close the document
            document.Close();
        }
Exemplo n.º 4
0
        public void WriteTocEntries()
        {
            if (!Config.Current.ShowTOC)
            {
                return;
            }
            if (pdfWriter == null || pdfDocument == null)
            {
                Log.Error("Uninitialized property pdfWriter or pdfDocument. Pass valid values and try again");
                return;
            }
            Log.Information("Writing Table of Contents ...");
            int beforeToc = pdfWriter.CurrentPageNumber;

            pdfWriter.PageEvent = new TocFooterHandler()
            {
                StartPageIndex = beforeToc, TOCString = TABLEOFCONTENTS
            };
            pdfDocument.NewPage();

            PdfAction  action   = PdfAction.GotoLocalPage(pdfWriter.CurrentPageNumber, new PdfDestination(PdfDestination.FITH, 806), pdfWriter);
            PdfOutline gotoPage = new PdfOutline(pdfWriter.RootOutline, action, TABLEOFCONTENTS);
            //TODO: fix this pdfWriter.DirectContent.AddOutline(gotoPage, gotoPage.Title);

            Paragraph ToCTitle = new Paragraph(TABLEOFCONTENTS, new Font(BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, true), 20));

            ToCTitle.SpacingAfter = 20;
            pdfDocument.Add(ToCTitle);

            Font tocFont = new Font(BaseFont.CreateFont(Path.Combine(Environment.SystemDirectory, "../fonts/arial.ttf"), BaseFont.IDENTITY_H, true), 12);

            foreach (TOCEntry tocEntry in TocEntries)
            {
                // Auto advance to next page
                float currentY = pdfWriter.GetVerticalPosition(false);
                if (currentY < 90)
                {
                    pdfDocument.NewPage();
                }

                // write toc entry title
                Paragraph a = new Paragraph();
                Chunk     c;
                if (tocEntry.Level <= Config.Current.TOCLevel)
                {
                    c = new Chunk(string.Format("{0} {1}", tocEntry.Numbering, tocEntry.Title), tocFont);
                }
                else
                {
                    c = new Chunk(tocEntry.Title, tocFont);
                }

                int       destinationPage = tocEntry.PageNumber;
                PdfAction gotoDestination = PdfAction.GotoLocalPage(destinationPage, new PdfDestination(PdfDestination.FITH, 806), pdfWriter);
                c.SetAction(gotoDestination);
                a.Add(c);
                a.IndentationLeft  = (tocEntry.Level - 1) * 20 + 60;
                a.FirstLineIndent  = -40;
                a.IndentationRight = 30;

                pdfDocument.Add(a);

                // write toc entry page number
                currentY = pdfWriter.GetVerticalPosition(false);
                PdfContentByte cb = pdfWriter.DirectContent;
                cb.SaveState();
                cb.BeginText();
                BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                cb.SetFontAndSize(bf, 12);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, destinationPage.ToString(), pdfDocument.Right, currentY, 0);
                cb.EndText();
                cb.RestoreState();
            }
            Log.Information("Reordering pages ...");
            int totalPages = pdfWriter.CurrentPageNumber;

            // now reorder the pages
            int[] reorder = new int[totalPages];
            for (int i = 0; i < totalPages; i++)
            {
                reorder[i] = i + beforeToc + 1;
                if (reorder[i] > totalPages)
                {
                    reorder[i] -= totalPages;
                }
            }
            pdfDocument.NewPage();
            pdfWriter.PageEvent = null;

            pdfWriter.ReorderPages(reorder);

            Log.Information(string.Format("Number of pages for TOC is {0}", totalPages - beforeToc));
            Log.Verbose("Set page labels");
            PdfPageLabels pageLabels = new PdfPageLabels();

            pageLabels.AddPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
            pageLabels.AddPageLabel(totalPages - beforeToc + 1, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
            pdfWriter.PageLabels = pageLabels;
        }