/// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String src, String dest)
        {
            ConverterProperties properties = new ConverterProperties();

            properties.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileInfo(src), new FileInfo(dest), properties);
        }
예제 #2
0
        public void ManipulatePdf(string htmlSource, string pdfDest, string resourceLoc, PageSize pageSize, float screenWidth)
        {
            PdfWriter   writer = new PdfWriter(pdfDest);
            PdfDocument pdfDoc = new PdfDocument(writer);

            // Set the result to be tagged
            pdfDoc.SetTagged();
            pdfDoc.SetDefaultPageSize(pageSize);

            ConverterProperties converterProperties = new ConverterProperties();

            // Set media device description details
            MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);

            mediaDescription.SetWidth(screenWidth);
            converterProperties.SetMediaDeviceDescription(mediaDescription);

            FontProvider fp = new DefaultFontProvider();

            // Register external font directory
            fp.AddDirectory(resourceLoc);

            converterProperties.SetFontProvider(fp);
            // Base URI is required to resolve the path to source files
            converterProperties.SetBaseUri(resourceLoc);

            // Create acroforms from text and button input fields
            converterProperties.SetCreateAcroForm(true);

            HtmlConverter.ConvertToPdf(new FileStream(htmlSource, FileMode.Open, FileAccess.Read, FileShare.Read),
                                       pdfDoc, converterProperties);

            pdfDoc.Close();
        }
예제 #3
0
        public virtual void VisiblePropertyInFormDropdownListTest()
        {
            //TODO update cmp-file after DEVSIX-2090 and DEVSIX-1901 done
            String htmlFile   = sourceFolder + "visiblePropertyInFormDropdownListTest.html";
            String outAcroPdf = destinationFolder + "visiblePropertyInFormDropdownListTest.pdf";
            ConverterProperties properties = new ConverterProperties();

            properties.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileInfo(htmlFile), new FileInfo(outAcroPdf), properties);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outAcroPdf, sourceFolder + "cmp_visiblePropertyInFormDropdownListTest.pdf"
                                                                             , destinationFolder, "diff_dropdown"));
        }
예제 #4
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        private void RunTest(String name)
        {
            String htmlPath                = sourceFolder + name + ".html";
            String outTaggedPdfPath        = destinationFolder + name + ".pdf";
            String outTaggedPdfPathAcro    = destinationFolder + name + "_acro.pdf";
            String outTaggedPdfPathFlatted = 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(outTaggedPdfPath);
            PdfDocument pdfTagged    = new PdfDocument(taggedWriter);

            pdfTagged.SetTagged();
            HtmlConverter.ConvertToPdf(new FileStream(htmlPath, FileMode.Open, FileAccess.Read), pdfTagged);
            //convert tagged PDF with acroform
            PdfWriter   taggedWriterAcro = new PdfWriter(outTaggedPdfPathAcro);
            PdfDocument pdfTaggedAcro    = new PdfDocument(taggedWriterAcro);

            pdfTaggedAcro.SetTagged();
            ConverterProperties converterPropertiesAcro = new ConverterProperties();

            converterPropertiesAcro.SetCreateAcroForm(true);
            HtmlConverter.ConvertToPdf(new FileStream(htmlPath, FileMode.Open, FileAccess.Read), pdfTaggedAcro, converterPropertiesAcro
                                       );
            //flatted created tagged PDF with acroform
            PdfDocument document = new PdfDocument(new PdfReader(outTaggedPdfPathAcro), new PdfWriter(outTaggedPdfPathFlatted
                                                                                                      ));
            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);

            acroForm.FlattenFields();
            document.Close();
            //compare with cmp
            String compResult1 = new CompareTool().CompareByContent(outTaggedPdfPath, cmpPdfPath, destinationFolder, diff1
                                                                    );
            String compResult2 = new CompareTool().CompareByContent(outTaggedPdfPathAcro, cmpPdfPathAcro, destinationFolder
                                                                    , diff2);
            String compResult3 = new CompareTool().CompareByContent(outTaggedPdfPathFlatted, cmpPdfPathAcroFlatten, destinationFolder
                                                                    , diff3);

            NUnit.Framework.Assert.IsNull(compResult1);
            NUnit.Framework.Assert.IsNull(compResult2);
            NUnit.Framework.Assert.IsNull(compResult3);
        }
예제 #5
0
        public virtual void EndPageEventWithFieldTest()
        {
            String name          = "endPageEventWithFieldTest";
            String pdfOutput     = destinationFolder + name + ".pdf";
            String pdfComparison = sourceFolder + "cmp_" + name + ".pdf";
            ConverterProperties converterProperties = new ConverterProperties();

            converterProperties.SetCreateAcroForm(true);
            PdfDocument pdfDocument = AddEventHandlersToPdfDocument(pdfOutput, converterProperties);
            String      htmlString  = "<html><head><title>pdfHtml header and footer example</title></head><body><span>test</span>"
                                      + "<div style='page-break-before:always;'></div><span>test</span></body></html>";

            HtmlConverter.ConvertToPdf(htmlString, pdfDocument, new ConverterProperties());
            pdfDocument.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(pdfOutput, pdfComparison, destinationFolder
                                                                             ));
        }
예제 #6
0
        public byte[] Convert(string htmlContent)
        {
            byte[] pdf = null;
            using (var memoryStream = new MemoryStream())
            {
                var writterProperties = new WriterProperties()
                                        .SetFullCompressionMode(true);
                using (PdfWriter writer = new PdfWriter(memoryStream, writterProperties))
                {
                    PdfDocument pdfDoc = new PdfDocument(writer);
                    pdfDoc.SetTagged();

                    PageSize pageSize = new PageSize(900, 1000);
                    pdfDoc.SetDefaultPageSize(pageSize);

                    ConverterProperties converterProperties = new ConverterProperties();
                    converterProperties.SetCreateAcroForm(true);

                    var fp = new DefaultFontProvider(true, false, false);
                    converterProperties.SetFontProvider(fp);

                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);

                    var                elements        = HtmlConverter.ConvertToElements(htmlContent, converterProperties);
                    Document           document        = new Document(pdfDoc);
                    CJKSplitCharacters splitCharacters = new CJKSplitCharacters();
                    document.SetSplitCharacters(splitCharacters);
                    document.SetProperty(Property.SPLIT_CHARACTERS, splitCharacters);
                    foreach (IElement element in elements)
                    {
                        document.Add((IBlockElement)element);
                    }
                    document.Close();

                    pdf = memoryStream.ToArray();

                    memoryStream.Close();
                    pdfDoc.Close();
                }
            }

            return(pdf);
        }
예제 #7
0
        public virtual void LangAttrInButtonForTaggedPdfWithActoformTest()
        {
            String      html        = sourceFolder + "langAttrInButtonForTaggedPdfTest.html";
            String      outFile     = destinationFolder + "langAttrInButtonForTaggedPdfWithActoformTest.pdf";
            String      cmp         = sourceFolder + "cmp_langAttrInButtonForTaggedPdfWithActoformTest.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));

            pdfDocument.SetTagged();
            ConverterProperties converterProperties = new ConverterProperties();

            converterProperties.SetCreateAcroForm(true);
            converterProperties.SetBaseUri(sourceFolder);
            HtmlConverter.ConvertToPdf(new FileStream(html, FileMode.Open, FileAccess.Read), pdfDocument, converterProperties
                                       );
            PrintOutputPdfNameAndDir(outFile);
            PdfDocument document = new PdfDocument(new PdfReader(outFile));

            //compareByContent is used here to check the complete logical structure tree to notice all the differences.
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmp, destinationFolder, "diff_forms"
                                                                             ));
            NUnit.Framework.Assert.AreEqual("da", document.GetCatalog().GetLang().ToUnicodeString());
            document.Close();
        }
예제 #8
0
        public byte[] GenerateHTML(string html)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                PdfWriter   writer = new PdfWriter(ms);//@"D:\Docs\demp.pdf"
                PdfDocument pdfDoc = new PdfDocument(writer);

                // Set the result to be tagged
                pdfDoc.SetTagged();
                pdfDoc.SetDefaultPageSize(PageSize.A4);

                ConverterProperties converterProperties = new ConverterProperties();

                // Set media device description details
                MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                //mediaDescription.SetWidth(screenWidth);
                converterProperties.SetMediaDeviceDescription(mediaDescription);

                FontProvider fp = new DefaultFontProvider();

                // Register external font directory
                ////fp.AddDirectory(resourceLoc);

                ////converterProperties.SetFontProvider(fp);
                // Base URI is required to resolve the path to source files
                ////converterProperties.SetBaseUri(resourceLoc);

                // Create acroforms from text and button input fields
                converterProperties.SetCreateAcroForm(true);

                // HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);
                HtmlConverter.ConvertToPdf(html, pdfDoc, converterProperties);

                pdfDoc.Close();
                return(ms.ToArray());
            }
        }