|
|
PDF SDK |
|
|
MzPDF Toolkit Visual Basic 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 Visual Basic tutorial will explain how to generate a PDF file with 5X5 cm rectangle using “Map Mode” technique.
Steps:
1. Open Visual Basic 6.
2. Create new project (Standard EXE).
3. Add the module mzPDF.bas to your project (you can find this module on \MzPDF\Include\VB\).
4. Inside Form1 module, please add the following.
Option Explicit
' Constants
Private Const FW_BOLD = 700
Private Const MM_ANISOTROPIC = 8
Private Const DT_BOTTOM = &H8
Private Const DT_CENTER = &H1
Private Const DT_WORDBREAK = &H10
Private Const DT_EXPANDTABS = &H40
Private Const SW_SHOW = 5
' Types
Private Type Size
cx As Long
cy As Long
End Type
' Functions
Private Declare Function SaveDC
Lib "gdi32" (ByVal
hdc As Long) As Long
Private Declare Function SelectObject
Lib "gdi32" (ByVal
hdc As Long, ByVal
hObject As Long) As Long
Private Declare Function SetMapMode
Lib "gdi32" (ByVal
hdc As Long,
ByVal nMapMode As
Long) As
Long
Private Declare Function SetWindowExtEx
Lib "gdi32" (ByVal
hdc As Long,
ByVal nX As
Long, ByVal nY
As Long, lpSize
As Size) As
Long
Private Declare Function SetViewportExtEx
Lib "gdi32" (ByVal
hdc As Long,
ByVal nX As
Long, ByVal nY
As Long, lpSize
As Size) As
Long
Private Declare Function Rectangle
Lib "gdi32" (ByVal
hdc As Long,
ByVal X1 As
Long, ByVal Y1
As Long,
ByVal X2 As
Long, ByVal Y2
As Long)
As Long
Private Declare Function DrawText
Lib "user32"
Alias "DrawTextA" (ByVal
hdc As Long,
ByVal lpStr As
String, ByVal nCount
As Long, lpRect
As RECT, _ ByVal wFormat
As Long)
As Long
Private Declare Function RestoreDC
Lib "gdi32" (ByVal
hdc As Long,
ByVal nSavedDC As
Long) As
Long
Private Declare Function ShellExecute
Lib "shell32.dll"
Alias "ShellExecuteA"
(ByVal hwnd As
Long, ByVal
lpOperation As String,
ByVal lpFile As
String _ , ByVal
lpParameters As String, ByVal
lpDirectory As String,
ByVal nShowCmd As
Long) As
Long
Private Declare Function CreateFont
Lib "gdi32"
Alias "CreateFontA" (ByVal H As Long,ByVal W As Long, ByVal E As Long, _
ByVal O As Long, ByVal W As Long,ByVal I As Long, ByVal u As _
Long,ByVal S As Long, ByVal C As Long,ByVal OP As Long, ByVal CP As Long,ByVal Q As Long, _ ByVal PAF As Long,ByVal F As String) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long)
As Long
5. Now, add a button to Form1. Double click the button. Inside Command1_Click(), please add the following code:
Dim pdfFilename
As String
Dim hPdfDocument
As Long
Dim PageInfo
As MZPAGEINFO
Dim PdfOptions
As MZPDFOPTIONS
Dim rcPage
As RECT
Dim nRet As
Long
Dim hFont
As Long
Dim hOldFont
As Long
Dim nSaveDC
As Long
Dim rectText
As String
Dim lpSize
As Size
' Choose the file name
pdfFilename = "c:\generatedPDFFile.pdf"
' Now create the font you want to use
hFont = CreateFont(50, 0, 0, 0, FW_BOLD, -1, 0, 0, 0, 0, 0,
0, 0, "Times New Roman")
' Start the document
If (MZ_ERR_SUCCESS <>
mzCreateDocument(hPdfDocument))
Then
MsgBox "Could
not create the document!", vbOKOnly, "Error"
Exit Sub
End If
' Now start the page
PageInfo.uStructSize = Len(PageInfo)
If (MZ_ERR_SUCCESS =
mzStartPage(hPdfDocument, MZ_LAST_PAGE, 8.5,
11, PageInfo, 0)) Then
' Now select
font
nSaveDC = SaveDC(PageInfo.hPageDC)
hOldFont =
SelectObject(PageInfo.hPageDC, hFont)
' Now Set the
Map Mode each logical unit equal 1 CM
SetMapMode PageInfo.hPageDC,
MM_ANISOTROPIC
' Map 0.01, for
example if you need map 0.1 then change 100 to 10
SetWindowExtEx PageInfo.hPageDC, 100,
100, lpSize
SetViewportExtEx PageInfo.hPageDC,
Round(PageInfo.xDPI / 2.5), Round(PageInfo.yDPI / 2.5), lpSize
' Draw 1 CM
rectangle, start on 5 CM
rcPage.Left = 1 * 100
' Since map 0.01, start 1 CM from left
rcPage.Top = 1 * 100
' Start after 1 CM from top
rcPage.Right = rcPage.Left + 5 * 100
' 5 CM width
rcPage.Bottom = rcPage.Top + 5 * 100
' 5 CM height
Rectangle PageInfo.hPageDC,
rcPage.Left, rcPage.Top, rcPage.Right, rcPage.Bottom
' Now draw text
inside rectangle.
rectText = Chr$(13) + "This
demo displays 5 Centimeters rectangle" + Chr$(13) + "By
Mozon Soft"
DrawText PageInfo.hPageDC, rectText,
Len(rectText), rcPage, DT_BOTTOM Or DT_CENTER
Or DT_WORDBREAK Or
DT_EXPANDTABS
DeleteObject (SelectObject(PageInfo.hPageDC,
hOldFont))
' Now Restore
the PDF DC and close the page
RestoreDC PageInfo.hPageDC, nSaveDC
' Close current
page
mzEndCurrentPage hPdfDocument
End If
' Now close the document
mzCloseDocument
hPdfDocument
' Finally generate the PDF file.
PdfOptions.uStructSize = Len(PdfOptions)
PdfOptions.dwFlags = MZ_GENERATE_ALL
PdfOptions.bPDFCompress = True
nRet = mzGeneratePDF(hPdfDocument,
pdfFilename, PdfOptions, 0, 0)
If (nRet <> MZ_ERR_SUCCESS)
Then
If (nRet
<> MZ_ERR_USER_ABORT) Then MsgBox "Error
Generate PDF file", vbOKOnly, "Report Demo"
End If
' Delete document handle to free memory
mzDeleteDocument
hPdfDocument
' Now if you want, you can load the
file
ShellExecute Me.hwnd, "open",
pdfFilename, "", "", SW_SHOW
6. Before you run you need to make sure the exe will be created in the output folder
Output directory = ..\..\..\bin\api\win32
where mzpdflib.dll and mzpdfKrn.dll are exist.
7. Run the project and click on the button and then you will get the result.
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.
|