PDF SDK by Mozon Soft that will make your application capable of generating PDF files in 15 minutes

PDF Toolkit/PDF SDK

Generate PDF Reports from your application in 15 minutes

Mozon Soft Home

Monzon Soft provides a PDF SDK that will make your application capable of saving PDF files in 15 minutes

Mozon PDF SDK is very easy to use

PDF SDK

MzPDF Toolkit C# Tutorial:


Click here for API Tutorial

Click here for Visual Basic Tutorial

Click here for Delphi Tutorial

Click here for VB.NET Tutorial


Introduction:
  This tutorial will show you how to integrate Mozon MzPDF toolkit in your own application in less than 15 minutes. After that you will have your application PDF-enabled.

  This tutorial will explain how to create a PDF with 3 pages, each page contains 1x1 inch rectangle using C#.

Steps:

 

1. Open visual studio 2005
 

2. Create new project (visual C# , Windows application)
 

3. Open Form1.cs.

4. After using System.Windows.Forms; add
using MZDocument;


5. Add the following inside Form1 class

const double page_width_inch = 8.5;

const double page_height_inch = 11;


6. Then add the following function

public void GeneratePDF()
{
}

7. Now, we will do some work inside this function. Define 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;


8. Now define MZDocument class as the following:

MZDocument.Document doc = new Document();

9. Add progress function:

doc.Progress += new EventHandler<ProgressEventArgs>(Progress2);

10. Create the document

doc.CreateDocument();

// create 3 pages so we need 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();

    // Repeated 3 times
}

// Close the document.
doc.CloseDocument();

// Generate PDF
doc.GeneratePDF("c:\\RetanglePDF.pdf", pdfOptions);

// After finish everything delete this document
doc.DeleteDocument();


11. Add progress event function

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.

       // we are setting this to false for now.

    if ( false )
        e.Cancel = true;
    else
        e.Cancel = false;
}


12. Go to references and add MZDocument.dll for win32 or MZDocumentx.dll for x64


13. Add new menu item to generate PDF then double click on it to create its function, inside this function add the following code:

GeneratePDF();

14. Run your application and click the menu item you created in step 13. Then go to C:\ drive you will find RetanglePDF.pdf document was created with the options that you've already chosen above.


Note: For x64 project, you need first to create new x64 project config from configuration manager



Full code is here

using System.Windows.Forms;

using MZDocument;

 

namespace WindowsApplication1

{

 

public partial class Form1 : Form

{

     const double page_width_inch = 8.5;

     const double page_height_inch = 11;

     public Form1()

     {

          InitializeComponent();

     }

     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();

          if (false)

               e.Cancel = true;

          else

               e.Cancel = false;

     }

 

     public void GeneratePDF()

     {

          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;

          try

          {

              MZDocument.Document doc = new Document();

              doc.Progress += new EventHandler<ProgressEventArgs>(Progress2);

              doc.CreateDocument();

              //create three pages

              for(int i = 1; i<= 3; i++)

              {

                  MZDocument.PageInfo pageInfo = new PageInfo();

                  // Start the page.

                  doc.StartPage(Document.LastPage, page_width_inch, page_height_inch, pageInfo, 0);

                  Rectangle pageRectangle = new Rectangle(0, 0, (int)(page_width_inch * pageInfo.xDPI + 0.5), (int)(page_height_inch * pageInfo.yDPI + 0.5));

                  //OR (Same result)

                  //Rectangle = pageInfo.RectPage;

             

                  //use page info DPI to calculate the drawing rectangle.

                  // Draw 1 inch rectangle.

                  Rectangle rectangle = new Rectangle((int)(1*pageInfo.xDPI),(int)(1*pageInfo.yDPI),(int)(1*pageInfo.xDPI),(int)(1*pageInfo.yDPI));

                  Pen pen = new Pen(Color.Black);

                  pageInfo.PageGraphics.DrawRectangle(pen,rectangle);

                  // close current page

                  doc.EndCurrentPage();

              }

              doc.CloseDocument();

              try

              {

                  doc.GeneratePDF("c:\\RetanglePDF.pdf", pdfOptions);

              }

              catch (DocumentException documentException)

              {

                  MessageBox.Show(documentException.Descreption, "Error");

              }

              // delete document handle to clear

              doc.DeleteDocument();

          }

          catch(DocumentException documentException)

          {

              MessageBox.Show(documentException.Descreption, "Error");

          }

    }

    private void GeneratePDFMenuItem_Click(object sender, EventArgs e)

    {

        GeneratePDF();

    }

}

}



 

Download MzPDF Toolkit:

  The MzPDF toolkit (Evaluation version) can be downloaded from the downloads page.

Order MzPDF Toolkit:

  To order MzPDF toolkit, go to the order page.

Note: MzPDF Toolkit is royalty free.

Contact Us:

  If you have any notes or questions please drop us a line and we will be glad to help.

PDF SDK to generate PDF files quickly and easily