| dsPdf.dll |
dsPdf is dll that can be used to create pdf
files. dsPrinter and dsReport uses dsPdf as well. It's a very simple dll that knows how to
create pdf files from metafiles. That means you have to pass metafile to
dsPdf.dll. Either pass a file on disk or pass memory where metafile resides.
Here are examples of using dsPdf.dll.
In the zip file you can also find example of how to use dsPdf.dd in other
PrintPreview components.
dsPdf is postcardware. That means if you want to get dll without demo message you must send me a postcard of the town or city you live in via snail mail. You can find my address at the Support section.
Delphi example:
function MMToPix(Value: Integer): Integer;
begin
Result := Round((Value / 254) * Screen.PixelsPerInch);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
m: TMetaFile;
ms: TMetafileCanvas;
mems: TMemoryStream;
H: THandle;
r: Integer;
BeginDocF: function(FileName: PChar): Integer; stdcall;
EndDocF: function: Integer; stdcall;
NewPageF: function: Integer; stdcall;
PrintPageM: function(Data: Pointer; Size: Integer): Integer; stdcall;
PrintPageF: function(FileName: PChar): Integer; stdcall;
SetPageF: function(ps, orientation, w, h: Integer): Integer; stdcall;
begin
m := TMetaFile.Create;
ms := TMetafileCanvas.Create(m, Canvas.Handle);
ms.Rectangle(MMToPix(100), MMToPix(100), MMToPix(2000), MMToPix(500));
ms.TextOut(MMToPix(900), MMToPix(200), 'hahaha');
ms.Free;
mems := TMemoryStream.Create;
m.SaveToStream(mems);
mems.Position := 0;
H := LoadLibrary('dspdf.dll');
if H > 0 then
begin
@BeginDocF := GetProcAddress(H, 'BeginDoc');
@EndDocF := GetProcAddress(H, 'EndDoc');
@NewPageF := GetProcAddress(H, 'NewPage');
@PrintPageM := GetProcAddress(H, 'PrintPageM');
@PrintPageF := GetProcAddress(H, 'PrintPageF');
@SetPageF := GetProcAddress(H, 'SetPage');
BeginDocF('c:\1.pdf');
SetPageF(2, 0, 0, 0);
PrintPageM(mems.Memory, mems.Size);
{
m.SaveToFile('c:\tmp.emf');
PrintPageF('c:\tmp.emf');
DeleteFile('c:\tmp.emf');
}
EndDocF;
FreeLibrary(H);
end;
mems.Free;
m.Free;
end;
C++ example:
test.h
#if !defined (_LOBODA_H)
#define _LOBODA_H
typedef int (FAR PASCAL *fnBeginDocF) (char *FileName) ;
typedef int (FAR PASCAL *fnEndDocF) () ;
typedef int (FAR PASCAL *fnNewPageF) () ;
typedef int (FAR PASCAL *fnPrintPageM) (unsigned char *Data, int Size) ;
typedef int (FAR PASCAL *fnPrintPageF) (char *FileName) ;
typedef int (FAR PASCAL *fnSetPageF) (int ps, int orientation, int w, int h) ;
#endif // _LOBODA_H
test.cpp
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <windows.h>
#include "test.h"
int main (int argc, char *argv[])
{
if (argc != 4)
{
printf ("Ussage:\r\n\t%s <dll name> <metafile path> <destination pdf file>\r\n\r\n", argv[0]) ;
return -1 ;
}
char szDll [MAX_PATH] ;
char szMetafile [MAX_PATH] ;
char szDestPDF [MAX_PATH] ;
strncpy (szDll, argv[1], MAX_PATH) ;
strncpy (szMetafile, argv[2], MAX_PATH) ;
strncpy (szDestPDF, argv[3], MAX_PATH) ;
HINSTANCE hDLL = LoadLibrary (szDll) ;
if (!hDLL)
{
printf ("Error while loading library %s\r\n", szDll) ;
return -1 ;
}
fnBeginDocF fBeginDocF = (fnBeginDocF) GetProcAddress (hDLL, "BeginDoc") ; ;
fnEndDocF fEndDocF = (fnEndDocF) GetProcAddress (hDLL, "EndDoc") ;
fnNewPageF fNewPageF = (fnNewPageF) GetProcAddress (hDLL, "NewPage") ;
fnPrintPageM fPrintPageM = (fnPrintPageM) GetProcAddress (hDLL, "PrintPageM") ;
fnPrintPageF fPrintPageF = (fnPrintPageF) GetProcAddress (hDLL, "PrintPageF") ;
fnSetPageF fSetPageF = (fnSetPageF) GetProcAddress (hDLL, "SetPage") ;
fBeginDocF (szDestPDF) ;
fSetPageF (2, 0, 0, 0) ;
fPrintPageF (szMetafile) ;
fEndDocF () ;
FreeLibrary (hDLL) ;
}
C# example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
[DllImport("dspdf.dll")]
public static extern unsafe int BeginDoc(byte[] FileName);
[DllImport("dspdf.dll")]
public static extern unsafe int EndDoc();
[DllImport("dspdf.dll")]
public static extern unsafe int NewPage();
//public static extern unsafe int PrintPageM(char* FileName );
[DllImport("dspdf.dll")]
public static extern unsafe int PrintPageF(byte[] FileName);
[DllImport("dspdf.dll")]
public static extern unsafe int SetPaperParameters(int ps, int orientation, int w, int h);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] buff = new byte[255];
for(int i = 0; i < "3.pdf".Length; i++)
buff[i] = (byte)"3.pdf"[i];
BeginDoc(buff);
SetPage(2, 0, 0, 0);
byte[] buff1 = new byte[255];
for(int i = 0; i < "c:\\1.emf".Length; i++)
buff1[i] = (byte)"c:\\1.emf"[i];
PrintPageF(buff1);
EndDoc();
}
}
}