<%@ WebHandler Language="C#" Class="Print" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using bioPDF.PdfWriter;
public class Print : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string printerName = "IIS PDF Printer";
string downloadName = "aspnet test.pdf";
string jobId = Guid.NewGuid().ToString();
string jobName = string.Format("ASP.NET-{0}", jobId);
string tempPath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"),
"Temp");
string jobTempPath = Path.Combine(tempPath, jobId);
string pdfName = string.Format("ASP.NET-{0}.pdf", jobId);
string pdfPath = Path.Combine(jobTempPath, pdfName);
string statusFile = Path.Combine(jobTempPath, "status.ini");
// Create folders
Directory.CreateDirectory(jobTempPath);
// Set parameters for print job
PdfSettings pdfSettings = new PdfSettings();
pdfSettings.PrinterName = printerName;
// These settings will only have effect if they are not overwritten by values in
// the global.ini
pdfSettings.SetValue("Output", pdfPath);
pdfSettings.SetValue("StatusFile", statusFile);
pdfSettings.SetValue("ShowSettings", "never");
pdfSettings.SetValue("ShowSaveAS", "never");
pdfSettings.SetValue("ShowProgress", "no");
pdfSettings.SetValue("ShowProgressFinished", "no");
pdfSettings.SetValue("ShowPDF", "no");
pdfSettings.SetValue("ConfirmOverwrite", "no");
pdfSettings.SetValue("RememberLastFolderName", "no");
pdfSettings.SetValue("RememberLastFileName", "no");
pdfSettings.SetValue("WatermarkLayer", "bottom");
pdfSettings.SetValue("WatermarkText", "Test from ASP.NET");
// Create a runonce.ini that is specific to this job
// http://www.biopdf.com/guide/configuration_files.php
string defaultRunoncePath =
pdfSettings.GetSettingsFilePath(PdfSettingsFileType.RunOnce);
string specificRunoncePath = Path.Combine(
Path.GetDirectoryName(defaultRunoncePath),
string.Format("runonce_{0}.ini", Uri.EscapeDataString(jobName)));
pdfSettings.WriteSettings(specificRunoncePath);
// Create print job
PrintDocument pd = new PrintDocument();
pd.DocumentName = jobName;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.PrinterSettings.PrinterName = printerName;
pd.DefaultPageSettings.Landscape = false;
pd.DefaultPageSettings.PaperSize = new PaperSize("Letter", 850, 1100);
pd.Print();
// Wait for PDF creation to finish
if (PdfUtil.WaitForFile(statusFile, 20000))
{
// Read error information from status file
string errorValue = PdfUtil.ReadIniString(statusFile, "Status", "Errors",
"");
if (errorValue == "0")
{
// Stream PDF to browser
context.Response.ClearContent();
context.Response.ContentType = "Application/pdf";
context.Response.AddHeader("Content-Disposition",
"inline; filename=" + downloadName);
context.Response.WriteFile(pdfPath);
context.Response.Flush();
// Remove files after the PDF is streamed to the client browser
File.Delete(pdfPath);
File.Delete(statusFile);
Directory.Delete(jobTempPath);
context.Response.End();
return;
}
else
{
string errorMessage = PdfUtil.ReadIniString(statusFile, "Status",
"MessageText", "");
WriteErrorMessage(string.Format("An error was reported: {0}; {1}",
errorValue, errorMessage));
}
}
WriteErrorMessage("No PDF was created.");
}
private void WriteErrorMessage(string message)
{
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write(message);
}
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Output something in the print
SolidBrush myBrush = new SolidBrush(Color.Blue);
Font font = new Font("Arial", 12);
e.Graphics.DrawString("Print test: " + DateTime.Now.ToString(),
font, myBrush, e.MarginBounds.Left, e.MarginBounds.Top);
myBrush.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
}