原創|使用教程|編輯:黃竹雯|2018-12-25 11:55:33.000|閱讀 462 次
概述:本篇教程演示在LEADTOOLS中如何將HTML文件轉換為PDF并與PDF合并。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本篇教程演示在LEADTOOLS中如何將HTML文件轉換為PDF并與PDF合并。
在這個示例中,我們采用不是標準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 20\Common\License\LEADTOOLS.LIC";
            string keyFileValue = File.ReadAllText(@"C:\LEADTOOLS 20\Common\License\LEADTOOLS.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();
        }
    }
}
文件附件:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn