This tutorial will show you how to integrate the MzPDF toolkit into your application in less than 15 minutes. By the end, your application will be PDF-enabled.
This tutorial will explain how to create a PDF with 3 pages, each containing a 1x1 inch rectangle using C#.
Form1.cs
.using System.Windows.Forms;
, add using MZDocument;
.Form1
class:
const double page_width_inch = 8.5;
const double page_height_inch = 11;
public void GeneratePDF()
{
}
PdfOptions
class and fill it with PDF options:
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.Title = "Rectangle docment";
pdfOptions.Subject = "Inch rectangle";
pdfOptions.Author = "Auther - me";
pdfOptions.Keywords = "Keywords ...";
pdfOptions.Pdfcompress = true;
pdfOptions.Jpgcompress = true;
pdfOptions.PdfGenerateImage = true;
pdfOptions.PdfGenerateText = true;
pdfOptions.PdfGenerateGraphics = true;
pdfOptions.PdfShowToolbar = true;
pdfOptions.PdfShowMenuBar = true;
pdfOptions.PdfShowWindowUI = true;
pdfOptions.PageNo = 2; // open PDF in page 2
pdfOptions.Version = PdfVersion.OneDotThree; //version 1.3
pdfOptions.Magnification = PdfMagnification.Custom;
pdfOptions.CustomMag = 50;
pdfOptions.PageMode = PdfPageMode.Thumbs;
pdfOptions.PageLayout = PdfPageLayout.TwoLColumn;
MZDocument
class as follows:MZDocument.Document doc = new Document();
doc.Progress += new EventHandler<ProgressEventArgs>(Progress2);
doc.CreateDocument();
// create 3 pages so we need a for loop
for(int i = 1; i <= 3; i++)
{
// define PDF Info
MZDocument.PageInfo pageInfo = new PageInfo();
// Start the page. with 8.5 by 11 inch.
doc.StartPage(Document.LastPage, page_width_inch, page_height_inch, pageInfo, 0);
// Calculate 1 inch rectangle.
Rectangle rectangle = new Rectangle((int)(1 * pageInfo.xDPI), (int)(1 * pageInfo.yDPI), (int)(1 * pageInfo.xDPI), (int)(1 * pageInfo.yDPI));
// Create black pen
Pen pen = new Pen(Color.Black);
// Draw the rectangle on the PDF DC
pageInfo.PageGraphics.DrawRectangle(pen, rectangle);
// Close this page
doc.EndCurrentPage();
}
// Close the document.
doc.CloseDocument();
// Generate PDF
doc.GeneratePDF("c:\\RectanglePDF.pdf", pdfOptions);
// After finishing everything delete this document
doc.DeleteDocument();
public void Progress2(object sender, ProgressEventArgs e)
{
int currentPage = e.CurrentPageProgress;
int total = e.CurrentPage * 100 / e.TotalPage;
String str = String.Format("Page : {0} of {1} ", e.CurrentPage, e.TotalPage);
Application.DoEvents();
// use the following condition if you want to interrupt the operation.
if (false)
e.Cancel = true;
else
e.Cancel = false;
}
MZDocument.dll
for Win32 or for x64.GeneratePDF();
RectanglePDF.pdf
document was created with the options that you've already chosen above.Note: For x64 project, you need to first create a new x64 project configuration from the configuration manager.
The MzPDF toolkit was designed to provide you with a graphics object, which can be used to draw in the same way you draw on your application's Graphics. It's as simple as that! Just replace your application's Graphics with our PdfGraphics, and you're done.