In the HTML document you can define different styles for different media types using @media rules. For example you can have a style for screen with background colors and images and a style for printing without background colors or images to save ink. Winnovative HTML to PDF Converter allows you to select the media type for which you want to render the HTML document using the HtmlToPdfConverterMediaType property.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Use Winnovative Namespace
using WinnovativeClient;
namespace WnvHtmlToPdfDemo.HTML_to_PDF.Media_Types
{
public partial class Select_Screen_or_Print_Media_Type : System.Web.UI.Page
{
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Get the server IP and port
String serverIP = textBoxServerIP.Text;
uint serverPort = uint.Parse(textBoxServerPort.Text);
// Create a HTML to PDF converter object
HtmlToPdfConverter htmlToPdfConverter = null;
if (radioButtonUseTcpService.Checked)
htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
else
htmlToPdfConverter = new HtmlToPdfConverter(true, textBoxWebServiceUrl.Text);
// Set optional service password
if (textBoxServicePassword.Text.Length > 0)
htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text;
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";
// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
htmlToPdfConverter.ConversionDelay = 2;
// Set the media type for which to render HTML to PDF
htmlToPdfConverter.MediaType = printMediaTypeRadioButton.Checked ? "print" : "screen";
byte[] outPdfBuffer = null;
if (convertHtmlRadioButton.Checked)
{
string htmlWithForm = htmlStringTextBox.Text;
string baseUrl = baseUrlTextBox.Text;
// Convert a HTML string to a PDF document for the selected media type
outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl);
}
else
{
string url = urlTextBox.Text;
// Convert the HTML page to a PDF document for the selected media type
outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
}
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Select_Screen_or_Print_Media_Type.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string currentPageUrl = HttpContext.Current.Request.Url.AbsoluteUri;
string rootUrl = "http://www.winnovative-software.com/demo/";
htmlStringTextBox.Text = System.IO.File.ReadAllText(Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Media_Type_Rules.html"));
baseUrlTextBox.Text = rootUrl + "DemoAppFiles/Input/HTML_Files/";
urlTextBox.Text = rootUrl + "DemoAppFiles/Input/HTML_Files/Media_Type_Rules.html";
sampleCodeLiteral.Text = System.IO.File.ReadAllText(Server.MapPath("~/DemoAppFiles/Input/Code_Samples/CSharp/AspNet/HTML_to_PDF/Media_Types/Screen_Print_Media_Type.html"));
descriptionLiteral.Text = System.IO.File.ReadAllText(Server.MapPath("~/DemoAppFiles/Input/Descriptions/AspNet/HTML_to_PDF/Media_Types/Screen_Print_Media_Type.html"));
Master.CollapseAll();
Master.ExpandNode("HTML_to_PDF");
Master.ExpandNode("Select_Media_Type");
Master.SelectNode("Select_Screen_or_Print_Media_Type");
}
}
protected void convertHtmlRadioButton_CheckedChanged(object sender, EventArgs e)
{
urlPanel.Visible = convertUrlRadioButton.Checked;
htmlStringPanel.Visible = !convertUrlRadioButton.Checked;
}
protected void convertUrlRadioButton_CheckedChanged(object sender, EventArgs e)
{
urlPanel.Visible = convertUrlRadioButton.Checked;
htmlStringPanel.Visible = !convertUrlRadioButton.Checked;
}
protected void demoMenu_MenuItemClick(object sender, MenuEventArgs e)
{
switch (e.Item.Value)
{
case "Live_Demo":
demoMultiView.SetActiveView(liveDemoView);
break;
case "Description":
demoMultiView.SetActiveView(descriptionView);
break;
case "Sample_Code":
demoMultiView.SetActiveView(sampleCodeView);
break;
default:
break;
}
}
protected void viewHtmlLinkButton_Click(object sender, EventArgs e)
{
Response.Redirect(urlTextBox.Text);
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>Media Type Rules</title>
<style type="text/css">
/* Set the body text family and font size both for screen and print*/
body
{
width: 1000px;
margin: 10px;
font-family: 'Times New Roman';
font-size: 18px;
}
/* Set the title text font size and weight both for screen and print*/
.title
{
font-size: 24px;
font-weight: bold;
}
@media screen
{
/* Set a background color only when the HTML page is displayed on screen*/
body
{
background-color: aliceblue;
}
/* Use blue to write the text on screen*/
p
{
color: darkblue;
}
}
@media print
{
/* Hide images when priting*/
img
{
display: none;
}
/* Use black to write the text on screen*/
p
{
color: black;
}
}
@media screen,print
{
/* Set the paragraph text family and font size both for screen and print*/
p
{
font-family: 'Times New Roman';
font-size: 20px;
}
}
</style>
</head>
<body>
<span class="title">Media Type Rules</span><br />
<br />
This document have different styles when it is displayed on the screen and when it is printed.
You can instruct the converter to use the style you want setting its <i>MediaType</i> property.<br />
<br />
<b>The image below is visible only when the selected media type is 'screen' and is hidden when the selected media type is 'print'</b>:<br />
<br />
<img alt="Logo Image" src="img/logo.jpg" />
<br />
<br />
<b>The text below will be dark blue when the selected media type is 'screen' and black when the selected media type is 'print':</b>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla
facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi.
</p>
</body>
</html>