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)
        {
            //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
                                                                                                                               )));
            Document document = new Document(pdf);

            //Setting some required parameters
            pdf.SetTagged();
            //Fonts need to be embedded
            PdfFont   font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true);
            Paragraph p    = new Paragraph();

            p.SetFont(font);
            p.Add(new Text("The quick brown "));
            iText.Layout.Element.Image foxImage = new Image(ImageDataFactory.Create(FOX));
            //Set alt text
            foxImage.GetAccessibilityProperties().SetAlternateDescription("Fox");
            p.Add(foxImage);
            p.Add(" jumps over the lazy ");
            iText.Layout.Element.Image dogImage = new iText.Layout.Element.Image(ImageDataFactory.Create(DOG));
            //Set alt text
            dogImage.GetAccessibilityProperties().SetAlternateDescription("Dog");
            p.Add(dogImage);
            document.Add(p);
            document.Close();
        }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
0
        private void ConvertToPdfa(Job job)
        {
            if (job.Profile.OutputFormat.IsPdfA())
            {
                var sourcePdf = job.IntermediatePdfFile;

                if (string.IsNullOrEmpty(sourcePdf))
                {
                    return;
                }

                var targetPdf = AddTailToFile(sourcePdf, "_pdfa");

                var version          = DeterminePdfVersionAsEnum(job.Profile);
                var writerProperties = new WriterProperties();
                writerProperties.SetPdfVersion(version);

                byte[] resource;
                //Set ICC Profile according to the color model
                switch (job.Profile.PdfSettings.ColorModel)
                {
                case ColorModel.Cmyk:
                    resource = GhostscriptResources.WebCoatedFOGRA28;
                    break;

                case ColorModel.Gray:
                    resource = GhostscriptResources.ISOcoated_v2_grey1c_bas;
                    break;

                default:
                    resource = GhostscriptResources.eciRGB_v2;
                    break;
                }

                try
                {
                    using (var icc = new MemoryStream(resource))
                    {
                        var document = new PdfADocument(new PdfWriter(targetPdf, writerProperties), GetConformLevel(job.Profile.OutputFormat), new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc));

                        document.SetTagged();
                        var toCopyDoc = new PdfDocument(new PdfReader(sourcePdf));
                        toCopyDoc.CopyPagesTo(1, toCopyDoc.GetNumberOfPages(), document);
                        var metaData = XMPMetaFactory.ParseFromBuffer(toCopyDoc.GetXmpMetadata());
                        document.SetXmpMetadata(metaData);
                        toCopyDoc.Close();
                        document.Close();

                        File.Delete(sourcePdf);
                        job.IntermediatePdfFile = targetPdf;
                    }
                }
                catch (PdfAConformanceException ex)
                {
                    throw new ProcessingException("One of the used pdfs does not conform to the PDF-A specification", ErrorCode.Processing_ConformanceMismatch, ex);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="html">the HTML file as a byte array</param>
        /// <param name="baseUri">the base URI</param>
        /// <param name="dest">the path to the resulting PDF</param>
        /// <param name="intent">the path to the output intent</param>
        public void CreatePdf(byte[] html, String baseUri, String dest, String intent)
        {
            PdfWriter    writer = new PdfWriter(dest);
            PdfADocument pdf    = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_2A,
                                                   new PdfOutputIntent("Custom", "", "http://www.color.org",
                                                                       "sRGB IEC61966-2.1", new FileStream(intent, FileMode.Open, FileAccess.Read)));

            pdf.SetTagged();
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            HtmlConverter.ConvertToPdf(new MemoryStream(html), pdf, properties);
        }
Ejemplo n.º 7
0
        public virtual void LinkTest10ToPdfa()
        {
            Stream       @is          = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
            PdfADocument pdfADocument = new PdfADocument(new PdfWriter(destinationFolder + "linkTest10.pdf"), PdfAConformanceLevel
                                                         .PDF_A_2A, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", @is));

            pdfADocument.SetTagged();
            using (FileStream fileInputStream = new FileStream(sourceFolder + "linkTest10.html", FileMode.Open, FileAccess.Read
                                                               )) {
                HtmlConverter.ConvertToPdf(fileInputStream, pdfADocument);
            }
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "linkTest10.pdf", sourceFolder
                                                                             + "cmp_linkTest10.pdf", destinationFolder, "diff10_"));
        }
        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.º 9
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="xml">a byte array with XML data</param>
        /// <param name="html">the HTML file as a byte array</param>
        /// <param name="baseUri">the base URI</param>
        /// <param name="dest">the path to the resulting PDF</param>
        /// <param name="intent">a path to the output intent</param>
        public void CreatePdf(byte[] xml, byte[] html, String baseUri, String dest, String intent)
        {
            PdfWriter    writer = new PdfWriter(dest);
            PdfADocument pdf    = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3A,
                                                   new PdfOutputIntent("Custom", "", "http://www.color.org",
                                                                       "sRGB IEC61966-2.1", new FileStream(intent, FileMode.Open, FileAccess.Read)));

            pdf.SetTagged();
            pdf.AddFileAttachment("Movie info",
                                  PdfFileSpec.CreateEmbeddedFileSpec(pdf, xml, "Movie info", "movies.xml",
                                                                     PdfName.ApplicationXml, new PdfDictionary(), PdfName.Data));
            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(baseUri);
            HtmlConverter.ConvertToPdf(new MemoryStream(html), pdf, properties);
        }
Ejemplo n.º 10
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.º 11
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();
        }