Print PDF - C#
PDF Renderer SDK sample in C# demonstrating ‘Print PDF’
Form1.Designer.cs
��namespace PrintPDF
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.buttonPageSetup = new System.Windows.Forms.Button();
this.buttonPrintPreview = new System.Windows.Forms.Button();
this.buttonPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon) (resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.UseAntiAlias = true;
this.printPreviewDialog1.Visible = false;
//
// pageSetupDialog1
//
this.pageSetupDialog1.Document = this.printDocument1;
//
// printDialog1
//
this.printDialog1.Document = this.printDocument1;
this.printDialog1.UseEXDialog = true;
//
// buttonPageSetup
//
this.buttonPageSetup.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.buttonPageSetup.Location = new System.Drawing.Point(12, 12);
this.buttonPageSetup.Name = "buttonPageSetup";
this.buttonPageSetup.Size = new System.Drawing.Size(253, 41);
this.buttonPageSetup.TabIndex = 0;
this.buttonPageSetup.Text = "Page Setup";
this.buttonPageSetup.UseVisualStyleBackColor = true;
this.buttonPageSetup.Click += new System.EventHandler(this.buttonPageSetup_Click);
//
// buttonPrintPreview
//
this.buttonPrintPreview.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.buttonPrintPreview.Location = new System.Drawing.Point(12, 59);
this.buttonPrintPreview.Name = "buttonPrintPreview";
this.buttonPrintPreview.Size = new System.Drawing.Size(253, 41);
this.buttonPrintPreview.TabIndex = 1;
this.buttonPrintPreview.Text = "Print Preview";
this.buttonPrintPreview.UseVisualStyleBackColor = true;
this.buttonPrintPreview.Click += new System.EventHandler(this.buttonPrintPreview_Click);
//
// buttonPrint
//
this.buttonPrint.Anchor = ((System.Windows.Forms.AnchorStyles) (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.buttonPrint.Location = new System.Drawing.Point(12, 106);
this.buttonPrint.Name = "buttonPrint";
this.buttonPrint.Size = new System.Drawing.Size(253, 41);
this.buttonPrint.TabIndex = 2;
this.buttonPrint.Text = "Print";
this.buttonPrint.UseVisualStyleBackColor = true;
this.buttonPrint.Click += new System.EventHandler(this.buttonPrint_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(277, 161);
this.Controls.Add(this.buttonPrint);
this.Controls.Add(this.buttonPrintPreview);
this.Controls.Add(this.buttonPageSetup);
this.Name = "Form1";
this.Text = "Print PDF";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.Button buttonPageSetup;
private System.Windows.Forms.Button buttonPrintPreview;
private System.Windows.Forms.Button buttonPrint;
}
}
+ Show More
Explore SDK documentations here.
Form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Bytescout.PDFRenderer;
namespace PrintPDF
{
public partial class Form1 : Form
{
private string _document = @"multipage.pdf";
readonly RasterRenderer _rasterRenderer = null;
private int _page = 0;
public Form1()
{
InitializeComponent();
// Create an instance of Bytescout.PDFRenderer.RasterRenderer object and register it.
_rasterRenderer = new RasterRenderer();
_rasterRenderer.RegistrationName = "demo";
_rasterRenderer.RegistrationKey = "demo";
}
private void Form1_Load(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
// Load PDF document
_rasterRenderer.LoadDocumentFromFile(_document);
}
catch (Exception exception)
{
MessageBox.Show("Could not open PDF document.\n\n" + exception.Message);
}
finally
{
Cursor = Cursors.Default;
}
}
private void buttonPageSetup_Click(object sender, EventArgs e)
{
// Set landscape orientation if needed
RectangleF pageRectangle = _rasterRenderer.GetPageRectangle(0);
if (pageRectangle.Width > pageRectangle.Height)
pageSetupDialog1.PageSettings.Landscape = true;
pageSetupDialog1.ShowDialog();
}
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
_page = 0;
printPreviewDialog1.Width = 800;
printPreviewDialog1.Height = 600;
printPreviewDialog1.ShowDialog();
}
private void buttonPrint_Click(object sender, EventArgs e)
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Cursor = Cursors.WaitCursor;
try
{
// For the best quality set the rendering resolution equal to the printer resolution
float renderingResolution = e.PageSettings.PrinterResolution.X;
// Render page to image
using (Image image = _rasterRenderer.GetImage(_page, renderingResolution))
{
// Fit image into the print rectangle keeping the aspect ratio
Rectangle printRect = e.MarginBounds;
float ratio = printRect.Width / (float) image.Width;
int width = printRect.Width;
int height = (int) (image.Height * ratio);
if (height > printRect.Height)
{
ratio = printRect.Height / (float) image.Height;
width = (int) (image.Width * ratio);
height = printRect.Height;
}
// Draw image on device
e.Graphics.DrawImage(image, new Rectangle(printRect.X, printRect.Y, width, height),
new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
}
if (_page < _rasterRenderer.GetPageCount() - 1)
{
_page++;
e.HasMorePages = true;
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
finally
{
Cursor = Cursors.Default;
}
}
}
}
+ Show More
Explore SDK documentations here.
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PrintPDF
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
+ Show More
Explore SDK documentations here.
Resources.Designer.cs
��//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace PrintPDF.Properties
{
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("PrintPDF.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}
+ Show More
Explore SDK documentations here.
Settings.Designer.cs
��//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace PrintPDF.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "8.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings) (global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}
+ Show More
Explore SDK documentations here.