【示例教程】LEADTOOLS中如何将HTML文件转换为PDF并与PDF合并

本篇教程演示在LEADTOOLS中如何将HTML文件转换为PDF并与PDF合并。

本篇教程演示在LEADTOOLS中如何将HTML文件转换为PDF并与PDF合并。

点击查看LEADTOOLS产品


在这个示例中,我们采用不是标准8.5 x 11大小的HTML文件,先将HTML文件转换为PDF并修复大小调整,然后将其与8.5 x 11的PDF合并。

这允许对文件进行转换和大小调整,以使它们与作为PDF合并的文件的大小相匹配。尺寸说明在LEADTOOLS在线文档中的解决方案的备注部分中进行了说明。

注意:当您使用下面的项目时,您需要将Leadtools.Pdf.Utilities.dll包含在输出目录中。

可以从这里复制:

C: LEADTOOLS 20 Bin Dotnet4 x64

并在这里添加:

…. convertCombineHtmlToPDF_20 bin x64 Debug

C#代码:

using System;using System.IO;using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using Leadtools.Pdf;namespace convertCombineHtmlToPDF_20{    class Program    {        static void Main(string[] args)        {            string licenseFilePath = @"C:LEADTOOLS 20CommonLicenseLEADTOOLS.LIC";            string keyFileValue = File.ReadAllText(@"C:LEADTOOLS 20CommonLicenseLEADTOOLS.LIC.KEY");            RasterSupport.SetLicense(licenseFilePath, keyFileValue);                //Step 1: Convert HTML file to PDF            string srcFileHTML = @"...readme.html"; //point to the HTML file            string srcFilePDF = @"...Leadtools.pdf"; //point to the PDF file            string outPath = @"...readmeToPDF_Test.pdf"; //point to the output after convert location            string mergeOutPath = @"...merge_Test.pdf"; //point to the output after merge location            RasterCodecs codecs = new RasterCodecs();            //Shows the PDF Width, Height, X Resolution, and Y Resolution            RasterImage imagePDF = codecs.Load(srcFilePDF);            Console.WriteLine("PDF Image Width: {0}", imagePDF.Width);            Console.WriteLine("PDF Image Width: {0}", imagePDF.Height);                Console.WriteLine("PDF X Resolution: {0}", imagePDF.XResolution);            Console.WriteLine("PDF Y Resolution: {0}", imagePDF.YResolution);            //Shows the HTML Width, Height, X Resolution, and Y Resolution            RasterImage imageHTML = codecs.Load(srcFileHTML);            Console.WriteLine("HTML Image Width: {0}", imageHTML.Width);            Console.WriteLine("HTML Image Width: {0}", imageHTML.Height);            Console.WriteLine("HTML X Resolution: {0}", imageHTML.XResolution);            Console.WriteLine("HTML Y Resolution: {0}", imageHTML.YResolution);            //Compares the Width and Height of the HTML to the 8.5 x 11 PDF            //Numeric Values to set the HTML file to            // imageHTML.Width = 1275            // imageHTML.Height = 1650            if (imageHTML.Width != imagePDF.Width || imageHTML.Height != imagePDF.Height)            {                RasterImage destImage = new RasterImage(                    RasterMemoryFlags.Conventional,                    imagePDF.Width,                    imagePDF.Height,                    imageHTML.BitsPerPixel,                    imageHTML.Order,                    imageHTML.ViewPerspective,                    imageHTML.GetPalette(),                    IntPtr.Zero,                    0);                ResizeCommand command = new ResizeCommand();                command.DestinationImage = destImage;                command.Flags = RasterSizeFlags.Bicubic;                command.Run(imageHTML);                        codecs.Save(destImage, outPath, RasterImageFormat.RasPdf, 0);            }                //Shows the outPut HTML file to make sure the Width and Height match the PDF            RasterImage imageConvertedHTML = codecs.Load(outPath);            Console.WriteLine("HTML Image Width: {0}", imageConvertedHTML.Width);            Console.WriteLine("HTML Image Width: {0}", imageConvertedHTML.Height);                //Step 2: Merge the two PDF files            PDFFile pdfFile = new PDFFile(srcFilePDF);            pdfFile.MergeWith(new string[] {outPath}, mergeOutPath);                codecs.Dispose();            Console.ReadLine();        }    }}

文件附件:convertCombineHtmlToPDF_20


想要购买LEADTOOLS正版授权,或者获取更多LEADTOOLS产品相关信息的朋友可以点击” 咨询在线客服 “~
海量产品正在参加年终大促,价格优惠!详情请点击” 年终巅峰钜惠 “~

标签:HTML文件格式转换PDF文档图像文档处理

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年11月22日
下一篇 2018年11月22日

相关推荐

发表回复

登录后才能评论