Limited time promo

ASP.NET PDF generator - SDK sample

English en

This article applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Mar 22, 2022
NovaPDF 9.x
NovaPDF 8.x
We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
Hello World (ASP.NET PDF generator) sample is a simple ASP application that generates one PDF file containing the text "novaPDF says Hello World from ASP.NET".
The PDF is created using the novaPDF printer driver and is saved in the “upload” folder. It demonstrates the basic use of the INovaPDFOptions interface. The printing job is done using the package System.Drawing.Printing.
Note
To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: novaPDF SDK.
What this sample does:
  • determines the active profile, makes a copy of it and names it "Test ASP.NET"
  • sets the new profile (Test ASP.NET) as active as well as some mandatory settings
  • generates a test PDF file and saves it in the “upload” folder
  • restores the original settings of the novaPDF printer driver.
Note
To test this sample on IIS, you should set the Application Pool to run under the “Local System” Account.
Here’s how you can do this:
  • In IIS Manager, expand Local computer, the Application Pools folder, right-click the application pool you would like to configure (the one selected for this application/ virtual directory) and click Properties.
  • Go to the Identity tab.
  • Click on Predefined and in the list box beside it click Local System.
  • Click OK.

Source code for the ASP sample output display page (Default.aspx):

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
	<title>Hello World ASP.NET</title>
</head>
<body>
<h1>Hello World ASP.NET and novaPDF SDK</h1>
	<form id="form2" runat="server">
	<div>
		<asp:Label ID="Label1" runat="server" Text="Press the button"></asp:Label>
		<br />
		<asp:LinkButton ID="Link1" Visible="false" runat="server">View folder.</asp:LinkButton>
		<br />
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
			Text="Print to novaPDF" /><br />
		<br />
		1. In IIS Manager, expand the local computer, expand the Application Pools folder,
		right-click the application pool you would like to configure (the one selected for
		this application/ virtual directory), and click Properties.
		<br />
		2. Click the Identity tab.
		<br />
		3. Click Predefined, and in the list box beside it, click Local System.
		<br />
		4. Click OK.
	</div>
	</form>
</body>
</html>

Source code for the ASP sample (Default.aspx.cs):

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Printing;

// the novapiLib package must be added as a	COM	reference
using novapiLib90;
using System.Diagnostics;

public enum SaveFolder
{
	SAVEFOLDER_APPLICATION = 1,
	SAVEFOLDER_LAST = 2,
	SAVEFOLDER_CUSTOM = 3,
	SAVEFOLDER_MYDOCUMENTS = 4
}

public enum SaveDlgType
{
	PROMPT_SAVE_STNANDARD = 0,
	PROMPT_SAVE_NONE = 1,
	PROMPT_SAVE_SIMPLE = 2
}

public enum SaveFileConflictType
{
	FILE_CONFLICT_STRATEGY_PROMPT = 0,
	FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW = 1,
	FILE_CONFLICT_STRATEGY_APPEND_DATE = 2,
	FILE_CONFLICT_STRATEGY_OVERWRITE = 3,
	FILE_CONFLICT_STRATEGY_AUTONUMBER_EXIST = 4,
	FILE_CONFLICT_STRATEGY_APPEND = 5,
	FILE_CONFLICT_STRATEGY_INSERT_BEFORE = 6,
	FILE_CONFLICT_STRATEGY_DONTSAVE = 7
}

public class NovaOptions
{
	public static int NOVAPDF_DOCINFO_SUBJECT = 68;
	public static int NOVAPDF_SAVE_FILE_NAME = 104;
	public static int NOVAPDF_SAVE_FOLDER = 103;
	public static int NOVAPDF_SAVE_PROMPT_TYPE = 101;
	public static int NOVAPDF_SAVE_FILEEXIST_ACTION = 108;
	public static int NOVAPDF_SAVE_FOLDER_TYPE = 260;
	public static int NOVAPDF_INFO_VIEWER_ENABLE = 8;
}

public class NovaErrors
{
	public static long NV_NO_ACTIVE_PROFILE = 0xD5DA0028;
}

public partial class _Default : System.Web.UI.Page
{
	public static string PRINTER_NAME = "novaPDF SDK 9";
	public static string NOVAPDF_INFO_SUBJECT = "Document Subject";
	public static string PROFILE_NAME = "Test ASP.NET";
	public static int PROFILE_IS_PUBLIC = 0;

	protected void Page_Load(object sender, EventArgs e)
	{

	}

	protected void Button1_Click(object sender, EventArgs e)
	{
		try
		{
			// create the NovaPdfOptions object
			NovaPdfOptions90 pNova = new NovaPdfOptions90();
			// initialize the NovaPdfOptions object
			pNova.InitializeSilent(PRINTER_NAME, "");
			// get the active profile ...
			string activeProfile = null;
			string newProfileId = null;
			try
			{
				pNova.GetActiveProfile(out activeProfile);
			}
			catch (System.Runtime.InteropServices.COMException come)
			{
				//if there is no active profile, ignore exception
				if (NovaErrors.NV_NO_ACTIVE_PROFILE == (uint)come.ErrorCode)
				{
				}
				else
				{
					throw (come);
				}
			}
			//create new profile
			pNova.AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, out newProfileId);
			pNova.LoadProfile(newProfileId);

			// and set some	options
			pNova.SetOptionString2(NovaOptions.NOVAPDF_DOCINFO_SUBJECT, "ASP.NET Hello document");
			pNova.SetOptionString(NovaOptions.NOVAPDF_SAVE_FILE_NAME, "novaPDFDocument");
			pNova.SetOptionLong(NovaOptions.NOVAPDF_SAVE_FOLDER_TYPE, (int)SaveFolder.SAVEFOLDER_CUSTOM);
			pNova.SetOptionString(NovaOptions.NOVAPDF_SAVE_FOLDER, Server.MapPath("upload/"));
			pNova.SetOptionLong(NovaOptions.NOVAPDF_SAVE_FILEEXIST_ACTION, (int)SaveFileConflictType.FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW);
			pNova.SetOptionBool(NovaOptions.NOVAPDF_INFO_VIEWER_ENABLE, 1);
			pNova.SetOptionLong(NovaOptions.NOVAPDF_SAVE_PROMPT_TYPE, (int)SaveDlgType.PROMPT_SAVE_SIMPLE);

			//save the new added profile
			pNova.SaveProfile();
			//set the new profile as the active profile
			pNova.SetActiveProfile(newProfileId);

			// print a test	page, using	the	previously set active profile settings
			using (PrintDocument pd = new PrintDocument())
			{
				pd.PrintController = new StandardPrintController();
				pd.PrinterSettings.PrinterName = PRINTER_NAME;
				pd.PrintPage += new PrintPageEventHandler(PrintPageFunction);
				pd.Print();
			}
			if ((activeProfile != null) && (activeProfile.Length > 0) && (activeProfile.CompareTo(PROFILE_NAME) != 0))
			{
				pNova.SetActiveProfile(activeProfile);
			}
			pNova.DeleteProfile(newProfileId);
			// mark finish changing options so they are saved for the printer

			Label1.Text = "Success.<br />You will find the new printed file in \"/upload\" folder.";
			Button1.Visible = false;

			Link1.Visible = true;
			Link1.Attributes.Add("href", "upload/");
			Link1.Attributes.Add("target", "_blank");
		}
		catch (System.Runtime.InteropServices.COMException come)
		{
			Label1.Style.Add("color", "red");
			Label1.Text = "COM Exception:" + come.Message;

		}
		catch (Exception ee)
		{
			Label1.Style.Add("color", "red");
			Label1.Text = "Exception: " + ee.Message;
			return;
		}
	}
	// and finally the function that actually prints the page
	private static void PrintPageFunction(object sender, PrintPageEventArgs ev)
	{
		string str = "novaPDF says Hello World from	ASP.NET\r\n"
				   + "on " + DateTime.Now.ToString("dd/MM/yyyy - H:mm:ss ");

		Font font = new Font("Arial", 16);
		Brush brush = new SolidBrush(Color.Black);
		ev.Graphics.DrawString(str, font, brush, 20.0f, 20.0f);
		ev.HasMorePages = false;
	}
}