LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Convert an HTML file to PDF and merge with a PDF
#1
Posted
:
Tuesday, June 19, 2018 11:12:06 AM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 89
Was thanked: 4 time(s) in 4 post(s)
In this demo,
- we take an HTML file that is not the standard 8.5 x 11 size
- convert the HTML file to a PDF and fix the sizing
- then merge it with a PDF that is 8.5 x 11
This allows files to be converted and sized so that they fit with the sizing of the files they are being merged with as a PDF.
Sizing explained in
Remarks section of the Resolution Property in the LEADTOOLS Online Documentation
*Please Note* that when you using the project below you will need to include the Leadtools.Pdf.Utilities.dll to the output directory.
Can be copied from here:
C:\LEADTOOLS 20\Bin\Dotnet4\x64
And added here:
....\convertCombineHtmlToPDF_20\bin\x64\Debug
C#Code:
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();
}
}
}
Edited by moderator Tuesday, September 11, 2018 11:18:32 AM(UTC)
| Reason: added a link to docs
Chris Thompson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Convert an HTML file to PDF and merge with a PDF
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.