MzPDF Toolkit Tutorial

Introduction

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#.

  1. Open Visual Studio 2005.
  2. Create a new project (Visual C#, Windows Application).
  3. Open Form1.cs.
  4. After using System.Windows.Forms;, add using MZDocument;.
  5. Add the following inside the Form1 class:
  6. 
                                    const double page_width_inch = 8.5;
                                    const double page_height_inch = 11;
  7. Add the following function:
  8. 
                                    public void GeneratePDF()
                                    {
                                    }
  9. Now, we will do some work inside this function. Define PdfOptions class and fill it with PDF options:
  10.                                 
                                        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;
                                    
  11. Now define MZDocument class as follows:
  12. MZDocument.Document doc = new Document();
  13. Add a progress function:
  14. doc.Progress += new EventHandler<ProgressEventArgs>(Progress2);
  15. Create the document:
  16. 
                                    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();
  17. Add a progress event function:
  18. 
                                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;
                                }
  19. Go to Refrences and add MZDocument.dll for Win32 or for x64.
  20. Add a new menu item to generate PDF, then double-click on it to create its function. Inside this function, add the following code:
  21. GeneratePDF();
  22. Run your application and click the menu item you created in step 13. Then go to C:\ drive, and you will find 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.

How We Did It?

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.