Beispiel #1
0
 public static Document ConvertToDocument(Stream htmlStream, PdfWriter pdfWriter, ConverterProperties converterProperties
                                          )
 {
     return(ConvertToDocument(htmlStream, new PdfDocument(pdfWriter), converterProperties));
 }
Beispiel #2
0
        /// <summary>
        /// Converts HTML obtained from an
        /// <see cref="System.IO.Stream"/>
        /// to objects that
        /// will be added to a
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// , using specific
        /// <see cref="ConverterProperties"/>
        /// ,
        /// returning a
        /// <see cref="iText.Layout.Document"/>
        /// instance.
        /// </summary>
        /// <param name="htmlStream">
        /// the
        /// <see cref="System.IO.Stream"/>
        /// with the source HTML
        /// </param>
        /// <param name="pdfDocument">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// instance
        /// </param>
        /// <param name="converterProperties">
        /// a
        /// <see cref="ConverterProperties"/>
        /// instance
        /// </param>
        /// <returns>
        /// a
        /// <see cref="iText.Layout.Document"/>
        /// instance
        /// </returns>
        /// <exception cref="System.IO.IOException">Signals that an I/O exception has occurred.</exception>
        public static Document ConvertToDocument(Stream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties
                                                 )
        {
            try
            {
                String licenseKeyClassName        = "iText.License.LicenseKey, itext.licensekey";
                String licenseKeyProductClassName = "iText.License.LicenseKeyProduct, itext.licensekey";
                String licenseKeyFeatureClassName = "iText.License.LicenseKeyProductFeature, itext.licensekey";
                String checkLicenseKeyMethodName  = "ScheduledCheck";
                Type   licenseKeyClass            = GetClass(licenseKeyClassName);
                if (licenseKeyClass != null)
                {
                    Type     licenseKeyProductClass        = GetClass(licenseKeyProductClassName);
                    Type     licenseKeyProductFeatureClass = GetClass(licenseKeyFeatureClassName);
                    Array    array   = Array.CreateInstance(licenseKeyProductFeatureClass, 0);
                    object[] objects = new object[]
                    {
                        Html2PdfProductInfo.PRODUCT_NAME,
                        Html2PdfProductInfo.MAJOR_VERSION,
                        Html2PdfProductInfo.MINOR_VERSION,
                        array
                    };
                    Object     productObject = System.Activator.CreateInstance(licenseKeyProductClass, objects);
                    MethodInfo m             = licenseKeyClass.GetMethod(checkLicenseKeyMethodName);
                    m.Invoke(System.Activator.CreateInstance(licenseKeyClass), new object[] { productObject });
                }
            }
            catch (Exception e)
            {
                if (!Kernel.Version.IsAGPLVersion())
                {
                    throw;
                }
            }
            if (pdfDocument.GetReader() != null)
            {
                throw new Html2PdfException(Html2PdfException.PdfDocumentShouldBeInWritingMode);
            }
            IXmlParser    parser = new JsoupHtmlParser();
            IDocumentNode doc    = parser.Parse(htmlStream, converterProperties != null ? converterProperties.GetCharset(
                                                    ) : null);

            return(Attacher.Attach(doc, pdfDocument, converterProperties));
        }
Beispiel #3
0
 public static void ConvertToPdf(Stream htmlStream, PdfWriter pdfWriter, ConverterProperties converterProperties
                                 )
 {
     ConvertToPdf(htmlStream, new PdfDocument(pdfWriter, new DocumentProperties().SetEventCountingMetaInfo(new HtmlMetaInfo())), converterProperties);
 }
Beispiel #4
0
 /// <summary>
 /// Converts HTML obtained from an
 /// <see cref="System.IO.Stream"/>
 /// to a PDF written to
 /// an
 /// <see cref="System.IO.Stream"/>
 /// .
 /// </summary>
 /// <param name="htmlStream">
 /// the
 /// <see cref="System.IO.Stream"/>
 /// with the source HTML
 /// </param>
 /// <param name="pdfStream">
 /// the
 /// <see cref="System.IO.Stream"/>
 /// for the resulting PDF
 /// </param>
 /// <param name="converterProperties">
 /// a
 /// <see cref="ConverterProperties"/>
 /// instance
 /// </param>
 /// <exception cref="System.IO.IOException">Signals that an I/O exception has occurred.</exception>
 public static void ConvertToPdf(Stream htmlStream, Stream pdfStream, ConverterProperties converterProperties
                                 )
 {
     ConvertToPdf(htmlStream, new PdfWriter(pdfStream), converterProperties);
 }
Beispiel #5
0
        private ConverterProperties GetConverterProperties(String fontsFolder)
        {
            ConverterProperties properties = new ConverterProperties().SetBaseUri(fontsFolder);

            return(properties);
        }
Beispiel #6
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        /// <exception cref="Javax.Xml.Parsers.ParserConfigurationException"/>
        /// <exception cref="Org.Xml.Sax.SAXException"/>
        public virtual void ConvertToPdfAcroformFlattenAndCompare(String name, String sourceFolder, String destinationFolder
                                                                  , bool tagged)
        {
            String sourceHtml = sourceFolder + name + ".html";

            if (tagged)
            {
                name = name + "Tagged";
            }
            String outPdfPath            = destinationFolder + name + ".pdf";
            String outPdfPathAcro        = destinationFolder + name + "_acro.pdf";
            String outPdfPathFlatted     = destinationFolder + name + "_acro_flatten.pdf";
            String cmpPdfPath            = sourceFolder + "cmp_" + name + ".pdf";
            String cmpPdfPathAcro        = sourceFolder + "cmp_" + name + "_acro.pdf";
            String cmpPdfPathAcroFlatten = sourceFolder + "cmp_" + name + "_acro_flatten.pdf";
            String diff1 = "diff1_" + name;
            String diff2 = "diff2_" + name;
            String diff3 = "diff3_" + name;
            //convert tagged PDF without acroform (from html with form elements)
            PdfWriter   taggedWriter = new PdfWriter(outPdfPath);
            PdfDocument pdfTagged    = new PdfDocument(taggedWriter);

            if (tagged)
            {
                pdfTagged.SetTagged();
            }
            HtmlConverter.ConvertToPdf(new FileStream(sourceHtml, FileMode.Open, FileAccess.Read), pdfTagged, new ConverterProperties
                                           ().SetBaseUri(sourceFolder));
            //convert PDF with acroform
            PdfWriter   writerAcro    = new PdfWriter(outPdfPathAcro);
            PdfDocument pdfTaggedAcro = new PdfDocument(writerAcro);

            if (tagged)
            {
                pdfTaggedAcro.SetTagged();
            }
            ConverterProperties converterPropertiesAcro = new ConverterProperties();

            converterPropertiesAcro.SetBaseUri(sourceFolder);
            converterPropertiesAcro.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileStream(sourceHtml, FileMode.Open, FileAccess.Read), pdfTaggedAcro, converterPropertiesAcro
                                       );
            System.Console.Out.WriteLine("html: file:///" + UrlUtil.ToNormalizedURI(sourceHtml).AbsolutePath + "\n");
            //flatted created tagged PDF with acroform
            PdfDocument document = new PdfDocument(new PdfReader(outPdfPathAcro), new PdfWriter(outPdfPathFlatted));
            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);

            acroForm.FlattenFields();
            document.Close();
            //compare with cmp
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPath, cmpPdfPath, destinationFolder
                                                                             , diff1));
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPathAcro, cmpPdfPathAcro, destinationFolder
                                                                             , diff2));
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdfPathFlatted, cmpPdfPathAcroFlatten,
                                                                             destinationFolder, diff3));
            //compare tags structure if tagged
            if (tagged)
            {
                CompareTagStructure(outPdfPath, cmpPdfPath);
                CompareTagStructure(outPdfPathAcro, cmpPdfPathAcro);
                CompareTagStructure(outPdfPathFlatted, cmpPdfPathAcroFlatten);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Converts HTML obtained from an
        /// <see cref="System.IO.Stream"/>
        /// to objects that
        /// will be added to a
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// , using specific
        /// <see cref="ConverterProperties"/>
        /// ,
        /// returning a
        /// <see cref="iText.Layout.Document"/>
        /// instance.
        /// </summary>
        /// <param name="htmlStream">
        /// the
        /// <see cref="System.IO.Stream"/>
        /// with the source HTML
        /// </param>
        /// <param name="pdfDocument">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// instance
        /// </param>
        /// <param name="converterProperties">
        /// a
        /// <see cref="ConverterProperties"/>
        /// instance
        /// </param>
        /// <returns>
        /// a
        /// <see cref="iText.Layout.Document"/>
        /// instance
        /// </returns>
        public static Document ConvertToDocument(Stream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties
                                                 )
        {
            ReflectionUtils.ScheduledLicenseCheck();
            if (pdfDocument.GetReader() != null)
            {
                throw new Html2PdfException(Html2PdfException.PdfDocumentShouldBeInWritingMode);
            }
            IXmlParser    parser = new JsoupHtmlParser();
            IDocumentNode doc    = parser.Parse(htmlStream, converterProperties != null ? converterProperties.GetCharset(
                                                    ) : null);

            return(Attacher.Attach(doc, pdfDocument, converterProperties));
        }