Ejemplo n.º 1
0
        public Task <byte[]> ConvertToPdfA2B(MemoryStream input)
        {
            var output    = new MemoryStream();
            var iccStream = new FileStream(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _imageColorMatchingFilename), FileMode.Open, FileAccess.Read);

            try
            {
                var intent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", iccStream);

                var currentPdf = new PdfADocument(new PdfWriter(output), PdfAConformanceLevel.PDF_A_2B, intent);
                var inputPdf   = new PdfDocument(new PdfReader(input));

                currentPdf.SetTagged();
                currentPdf.GetCatalog().SetLang(new PdfString("en-US"));
                currentPdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));

                for (int i = 1; i <= inputPdf.GetNumberOfPages(); i++)
                {
                    currentPdf.AddPage(inputPdf.GetPage(i).CopyTo(currentPdf));
                }

                //inputPdf.CopyPagesTo(1, inputPdf.GetNumberOfPages(), currentPdf);

                currentPdf.Close();

                return(Task.FromResult(output.ToArray()));
            }
            finally
            {
                iccStream.Dispose();
                input.Dispose();
                output.Dispose();
            }
        }
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDFA document with output intent
            PdfADocument pdf = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent
                                                    ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open, FileAccess.Read
                                                                                                                               )));

            //Setting some required parameters
            pdf.SetTagged();
            pdf.GetCatalog().SetLang(new PdfString("en-US"));
            pdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
            PdfDocumentInfo info = pdf.GetDocumentInfo();

            info.SetTitle("iText7 PDF/A-1a example");
            //Create PdfMerger instance
            PdfMerger merger = new PdfMerger(pdf);
            //Add pages from the first document
            PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));

            merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());
            //Add pages from the second pdf document
            PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));

            merger.Merge(secondSourcePdf, 1, secondSourcePdf.GetNumberOfPages());
            //Close the documents
            firstSourcePdf.Close();
            secondSourcePdf.Close();
            pdf.Close();
        }
        public virtual void CreatePdf(String dest)
        {
            PdfADocument pdf = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_3A, new PdfOutputIntent
                                                    ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open, FileAccess.Read
                                                                                                                               )));
            Document document = new Document(pdf, PageSize.A4.Rotate());

            document.SetMargins(20, 20, 20, 20);
            //Setting some required parameters
            pdf.SetTagged();
            pdf.GetCatalog().SetLang(new PdfString("en-US"));
            pdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
            PdfDocumentInfo info = pdf.GetDocumentInfo();

            info.SetTitle("iText7 PDF/A-3 example");
            //Add attachment
            PdfDictionary parameters = new PdfDictionary();

            parameters.Put(PdfName.ModDate, new PdfDate().GetPdfObject());
            PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec(pdf, File.ReadAllBytes(System.IO.Path.Combine(DATA
                                                                                                                    )), "united_states.csv", "united_states.csv", new PdfName("text/csv"), parameters, PdfName.Data);

            fileSpec.Put(new PdfName("AFRelationship"), new PdfName("Data"));
            pdf.AddFileAttachment("united_states.csv", fileSpec);
            PdfArray array = new PdfArray();

            array.Add(fileSpec.GetPdfObject().GetIndirectReference());
            pdf.GetCatalog().Put(new PdfName("AF"), array);
            //Embed fonts
            PdfFont font = PdfFontFactory.CreateFont(FONT, true);
            PdfFont bold = PdfFontFactory.CreateFont(BOLD_FONT, true);
            // Create content
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 4, 1, 3, 4, 3, 3, 3, 3, 1 }))
                          .UseAllAvailableWidth();

            using (StreamReader sr = File.OpenText(DATA))
            {
                String line = sr.ReadLine();
                Process(table, line, bold, true);
                while ((line = sr.ReadLine()) != null)
                {
                    Process(table, line, font, false);
                }
            }

            document.Add(table);
            //Close document
            document.Close();
        }
Ejemplo n.º 4
0
        private void ManipulatePdf(String dest)
        {
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);

            Stream 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));

            pdfDoc.GetCatalog().SetLang(new PdfString("nl-nl"));

            pdfDoc.SetTagged();

            Document doc = new Document(pdfDoc);

            doc.SetMargins(MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM);

            PdfDocumentInfo info = pdfDoc.GetDocumentInfo();

            info
            .SetTitle("title")
            .SetAuthor("Author")
            .SetSubject("Subject")
            .SetCreator("Creator")
            .SetKeywords("Metadata, iText, PDF")
            .SetCreator("My program using iText")
            .AddCreationDate();

            Paragraph element = new Paragraph("Hello World").SetFont(font).SetFontSize(10);

            doc.Add(element);

            Image logoImage = new Image(ImageDataFactory.Create(LOGO));

            logoImage.GetAccessibilityProperties().SetAlternateDescription("Logo");
            doc.Add(logoImage);

            doc.Close();
        }
Ejemplo n.º 5
0
        private void ManipulatePdf(String dest)
        {
            PdfFont font = PdfFontFactory.CreateFont(FONT, 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_2A,
                                                   new PdfOutputIntent("Custom", "",
                                                                       null, "sRGB IEC61966-2.1", fileStream));

            Document document = new Document(pdfDoc);

            // Specifies that document should contain tag structure
            pdfDoc.SetTagged();
            pdfDoc.GetCatalog().SetLang(new PdfString("en-us"));

            Paragraph p = new Paragraph("Hello World!").SetFont(font).SetFontSize(10);

            document.Add(p);
            document.Close();
        }