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();
        }
Exemplo n.º 2
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);
        }