To use the HTML to PDF Converter in Windows Forms and Console applications, all
you have to do is to add a reference the wnvhtmlconvert.dll assembly
and call the render methods from your code after you have set the render properties.
Below is a code example of setting the converter library properties and then calling
the render methods to generate either an image or a PDF document. The code is taken
from the windows forms application sample provided with the HTML to PDF Converter.
In the event handler of a button, the application creates either a PdfConverter
or an ImgConverter object and calls
private PdfConverter GetPDFConverter()
{
int pageWidth = 0;
int pageHeight = 0;
// set common properties
if (!radioButtonAutodetect.Checked)
{
pageWidth = int.Parse(textBoxWebPageWidth.Text.Trim());
pageHeight = int.Parse(textBoxWebPageHeight.Text.Trim());
}
// create the PDF converter
PdfConverter pdfConverter = new PdfConverter(pageWidth, pageHeight);
// set PDF options
pdfConverter.PdfDocumentOptions.PdfPageSize =
(PdfPageSize)Enum.Parse(typeof(PdfPageSize),
ddlPageFormat.SelectedItem.ToString());
pdfConverter.PdfDocumentOptions.PdfCompressionLevel = (PdfCompressionLevel)
Enum.Parse(typeof(PdfCompressionLevel),
ddlCompression.SelectedItem.ToString());
pdfConverter.PdfDocumentOptions.ShowHeader = cbShowHeader.Checked;
pdfConverter.PdfDocumentOptions.ShowFooter = cbShowFooter.Checked;
pdfConverter.PdfDocumentOptions.LeftMargin = int.Parse(
textBoxLeftMargin.Text.Trim());
pdfConverter.PdfDocumentOptions.RightMargin = int.Parse(
textBoxRightMargin.Text.Trim());
pdfConverter.PdfDocumentOptions.TopMargin = int.Parse(
textBoxTopMargin.Text.Trim());
pdfConverter.PdfDocumentOptions.BottomMargin = int.Parse(
textBoxBottomMargin.Text.Trim());
pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = cbUseMetafileFormat.Checked;
pdfConverter.PdfHeaderOptions.HeaderText = textBoxHeaderTitle.Text;
pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.FromKnownColor(
(KnownColor)Enum.Parse(typeof(KnownColor),
ddlHeaderTextColor.SelectedItem.ToString()));
pdfConverter.PdfHeaderOptions.HeaderSubtitleText = textBoxHeadeSubtitle.Text;
pdfConverter.PdfHeaderOptions.DrawHeaderLine = cbDrawHeaderLine.Checked;
pdfConverter.PdfFooterOptions.FooterText = textBoxFooterText.Text;
pdfConverter.PdfFooterOptions.FooterTextColor = Color.FromKnownColor(
(KnownColor)Enum.Parse(typeof(KnownColor),
ddlFooterColor.SelectedItem.ToString()));
pdfConverter.PdfFooterOptions.DrawFooterLine = cbDrawFooterLine.Checked;
pdfConverter.PdfFooterOptions.PageNumberText = textBoxPageNumberText.Text;
pdfConverter.PdfFooterOptions.ShowPageNumber = cbShowPageNumber.Checked;
return pdfConverter;
}
private ImgConverter GetImgConverter()
{
int pageWidth = 0;
int pageHeight = 0;
// set common properties
if (!radioButtonAutodetect.Checked)
{
pageWidth = int.Parse(textBoxWebPageWidth.Text.Trim());
pageHeight = int.Parse(textBoxWebPageHeight.Text.Trim());
}
// create the PDF converter
ImgConverter imgConverter = new ImgConverter(pageWidth, pageHeight);
return imgConverter;
}
private void btnConvert_Click(object sender, EventArgs e)
{
if (!PageIsValid())
return;
string outFile = null;
if (radioConvertToPDF.Checked)
{
PdfConverter pdfConverter = GetPDFConverter();
outFile = Path.Combine(Application.StartupPath, "RenderedPage.pdf");
try
{
pdfConverter.SavePdfFromUrlToFile(textBoxWebPageURL.Text, outFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
else
{
ImgConverter imgConverter = GetImgConverter();
outFile = Path.Combine(Application.StartupPath, String.Format(
"RenderedPage.{0}", ddlImageFormat.SelectedItem.ToString()));
try
{
RenderImageFormat renderImageFormat = (RenderImageFormat)Enum.Parse
(typeof(RenderImageFormat),
ddlImageFormat.SelectedItem.ToString());
imgConverter.SaveImageFromUrlToFile(
textBoxWebPageURL.Text,
RenderImageFormatConverter.GetImageFormat(renderImageFormat),
outFile
);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
DialogResult dr = MessageBox.Show("Open the rendered file in an external
viewer?", "Open Rendered File", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
try
{
System.Diagnostics.Process.Start(outFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
|