Components for Developers
© 2000-2008, Two Pilots

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF Add-on History In the Lab

PDF Creator Pilot documentation

Download CHM version of this manual.
PDF Creator Pilot 4
How to convert metafile to PDF using C++

See Also Example
Collapse All

First of all import the library in your project:

#import "c:\\...\\PDFCreatorPilot.dll" // enter path here
using namespace PDFCreatorPilotLib;

Then you need to initialize a PDFDocument object:

IPDFDocument4* PDF = NULL;

CLSID clsid_PDFCreatorPilot;
CLSIDFromProgID(OLESTR("PDFCreatorPilot.PDFDocument4"), &clsid_PDFCreatorPilot);
::CoCreateInstance(clsid_PDFCreatorPilot, NULL, CLSCTX_ALL, __uuidof(IPDFDocument4), (LPVOID*)&PDF); 
if(PDF == NULL)
{
  ::CoUninitialize();
  return 1;
}

After this you should call SetLicenseData method and adjust some properties if needed.

Load EMF file to get its properties: width, height, resolution. You will need it to calculate scaleFactor. This value is needed to properly draw EMF file fited in PDF page.

HENHMETAFILE hEmf = GetEnhMetaFile(emfFile);

MetafileDetails md = {0};
if(!GetMetafileDetails(hEmf, &md))
{
  ::CoUninitialize();
  return 2;
}

// Scale factor based on the resolutions difference
double dScaleFactor = (double)PDF->PageResolution / (double)md.ResolutionY;

PDF->PageHeight = (long)(md.Height * dScaleFactor);
PDF->PageWidth = (long)(md.Width * dScaleFactor);

To convert EMF file to PDF just call PlayMetaFile method.

PDF->PlayMetaFile(emfFile, 0, 0, dScaleFactor, dScaleFactor);
PDF->SaveToFile("PlayMetafile.pdf", true);

You can find a full example bellow. You may also download a MS Visual Studio project with this example here.

Example

Converting metafile

C/C++
[copy to clipboard]
// PlayMetafile.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <math.h>
#include "MetafileDetails.h"

// set correct path to PDFCreatorPilot.dll in the line bellow
#import "path\\to\\PDFCreatorPilot.dll"
using namespace PDFCreatorPilotLib;

int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize(NULL);
    // declare PDF Creator Pilot object
    IPDFDocument4* PDF = NULL;

    CLSID clsid_PDFCreatorPilot;
    CLSIDFromProgID(OLESTR("PDFCreatorPilot.PDFDocument4"), &clsid_PDFCreatorPilot);
    ::CoCreateInstance(clsid_PDFCreatorPilot, NULL, CLSCTX_ALL, __uuidof(IPDFDocument4), (LPVOID*)&PDF);        
    if(PDF == NULL)
    {
        ::CoUninitialize();
        return 1;
    }

    PDF->SetLicenseData("demo","demo");

    _TCHAR* emfFile = "metafile.emf";
    HENHMETAFILE hEmf = GetEnhMetaFile(emfFile);

    MetafileDetails md = {0};
    if(!GetMetafileDetails(hEmf, &md))
    {
        ::CoUninitialize();
        return 2;
    }

    // Scale factor based on the resolutions difference
    double dScaleFactor = (double)PDF->PageResolution / (double)md.ResolutionX;

    PDF->PageHeight = (long)(md.Height * dScaleFactor);
    PDF->PageWidth  = (long)(md.Width * dScaleFactor);

    PDF->PlayMetaFile(emfFile, 0, 0, dScaleFactor, dScaleFactor);

    PDF->SaveToFile("PlayMetafile.pdf", true);

    ::CoUninitialize();
    return 0;
}

See Also

Reference

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF Add-on History In the Lab

 

 

PDF Library | Virtual Printer | Converters to PDF

Support | Blog | Forum | Contacts

© 2000-2008, Two Pilots