コード例 #1
0
        private void ManipulatePdf(String dest)
        {
            font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            bold = PdfFontFactory.CreateFont(BOLD, PdfEncodings.IDENTITY_H);

            FileStream fileStream =
                new FileStream("../../../resources/data/sRGB_CS_profile.icm", FileMode.Open, FileAccess.Read);

            PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1A,
                                                   new PdfOutputIntent("Custom", "",
                                                                       null, "sRGB IEC61966-2.1", fileStream));

            Document document = new Document(pdfDoc, PageSize.A4.Rotate());

            pdfDoc
            .SetTagged()
            .GetCatalog()
            .SetLang(new PdfString("en-us"));

            template = new PdfFormXObject(new Rectangle(795, 575, 30, 30));
            PdfCanvas canvas = new PdfCanvas(template, pdfDoc);

            total = new Image(template);
            total.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT);

            // Creates a header for every page in the document
            pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new HeaderHandler(this));

            PdfDictionary parameters = new PdfDictionary();

            parameters.Put(PdfName.ModDate, new PdfDate().GetPdfObject());

            Table table = new Table(UnitValue.CreatePercentArray(
                                        new float[] { 4, 1, 3, 4, 3, 3, 3, 3, 1 })).UseAllAvailableWidth();

            StreamReader br = new StreamReader(DATA);

            // Reads content of csv file
            String line = br.ReadLine();

            Process(table, line, bold, 10, true);

            while ((line = br.ReadLine()) != null)
            {
                Process(table, line, font, 10, false);
            }

            br.Close();

            document.Add(table);

            canvas.BeginText();
            canvas.SetFontAndSize(bold, 12);
            canvas.MoveText(795, 575);
            canvas.ShowText(pdfDoc.GetNumberOfPages().ToString());
            canvas.EndText();
            canvas.Stroke();

            document.Close();
        }
コード例 #2
0
        public Task <byte[]> GenerateTransactionHistory(TransactionHistoryPdf transactionHistoryPdf)
        {
            var output    = new MemoryStream();
            var iccStream = new FileStream(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _imageColorMatchingFilename), FileMode.Open, FileAccess.Read);
            var pdfFont   = PdfFontFactory.CreateFont(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _trueTypeFontFilename), PdfEncodings.IDENTITY_H, true);

            try
            {
                var      intent   = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", iccStream);
                var      pdf      = new PdfADocument(new PdfWriter(output), PdfAConformanceLevel.PDF_A_2B, intent);
                Document document = new Document(pdf);

                if (transactionHistoryPdf.Columns.Any())
                {
                    var table = new Table(6, false);
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Pid).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.TypeOfObject).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.TypeOfChanges).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Descriptions).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Author).SetFont(pdfFont));
                    table.AddCell(CreateRootCell(transactionHistoryPdf.Rows.Date).SetFont(pdfFont));

                    foreach (var column in transactionHistoryPdf.Columns)
                    {
                        table.AddCell(CreateCell(column.Pid).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.TypeOfObject).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.TypeOfChanges).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Descriptions, TextAlignment.LEFT).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Author, TextAlignment.LEFT).SetFont(pdfFont));
                        table.AddCell(CreateCell(column.Date, TextAlignment.LEFT).SetFont(pdfFont));
                    }

                    document.Add(table);
                    document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
                }

                document.Add(CreateNewLine().SetFont(pdfFont));
                document.Add(CreateLineSeparator().SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Name).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Originator).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.Address).SetFont(pdfFont));
                document.Add(CreateTextAlignment(transactionHistoryPdf.SerialNumber).SetFont(pdfFont));
                document.Add(CreateTextAlignment($"{transactionHistoryPdf.NumberOfPages}{pdf.GetNumberOfPages()}").SetFont(pdfFont));

                var numberOfPages = pdf.GetNumberOfPages();

                pdf.MovePage(numberOfPages, 1);

                for (int pageNum = 1; pageNum <= numberOfPages; pageNum++)
                {
                    Rectangle pageSize = pdf.GetPage(pageNum).GetPageSize();

                    if (pageNum != 1)
                    {
                        document.ShowTextAligned(CreateTextAlignment(transactionHistoryPdf.Header, 7).SetFont(pdfFont), pageSize.GetLeft() + 35, pageSize.GetTop() - 13, pageNum, TextAlignment.LEFT, VerticalAlignment.TOP, 0);
                    }

                    document.ShowTextAligned(CreateText(pageNum + " / " + numberOfPages, 7).SetFont(pdfFont), pageSize.GetWidth() / 2, pageSize.GetBottom() + 15, pageNum, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
                }

                document.Close();

                return(Task.FromResult(output.ToArray()));
            }
            finally
            {
                iccStream.Dispose();
                output.Dispose();
            }
        }