Enhanced EMF/WMF Metafile Support

Overview

HotPDF now provides significantly enhanced support for Enhanced Metafile (EMF) and Windows Metafile (WMF) formats The new implementation includes automatic format detection, multiple positioning options, advanced scaling control, and improved rendering quality for vector graphics in PDF documents.

Key Enhancements

  • Automatic EMF/WMF format detection and appropriate processing
  • Multiple positioning options (center, top-left, custom coordinates)
  • Enhanced scaling and aspect ratio control
  • Per-call rendering options for compression, scaling, reference DC, pen-style handling, hatch-brush preservation, and null object diagnostics
  • Direct EMF rendering for vector graphics preservation
  • Bitmap conversion option for compatibility
  • Improved memory management for large metafiles
  • Better handling of complex vector graphics and text

Position Control Options

The enhanced metafile support includes flexible positioning options:

  • mpCenter: Metafile centered on the page (default behavior)
  • mpLeftTop: Metafile displayed at the top-left corner (X=0, Y=0)
  • mpCustom: Metafile displayed at specified position (requires X, Y parameters)

Usage Examples

Basic Usage (Default Centered Display)


// Delphi example - Default centered display
procedure ShowMetafileDefault;
var
  PDF: THotPDF;
  MetaFile: TMetafile;
begin
  PDF := THotPDF.Create(nil);
  MetaFile := TMetafile.Create;
  try
    PDF.BeginDoc;
    PDF.AddPage;

    // Load metafile
    MetaFile.LoadFromFile('diagram.emf');

    // Display centered on page (default)
    PDF.CurrentPage.ShowMetafile(MetaFile);

    PDF.EndDoc;
  finally
    MetaFile.Free;
    PDF.Free;
  end;
end;
        

Position Control Usage


// Delphi example - Advanced positioning
procedure ShowMetafileAdvanced;
var
  PDF: THotPDF;
  MetaFile: TMetafile;
begin
  PDF := THotPDF.Create(nil);
  MetaFile := TMetafile.Create;
  try
    PDF.BeginDoc;
    PDF.AddPage;

    MetaFile.LoadFromFile('chart.wmf');

    // Top-left corner display
    PDF.CurrentPage.ShowMetafile(MetaFile, mpLeftTop);

    // Custom position display
    PDF.CurrentPage.ShowMetafile(MetaFile, mpCustom, 100, 200);

    PDF.EndDoc;
  finally
    MetaFile.Free;
    PDF.Free;
  end;
end;
        

Per-call Rendering Options


// Delphi example - Per-call options
procedure ShowMetafileWithOptions;
var
  PDF: THotPDF;
  MetaFile: TMetafile;
  Options: THPDFEmfOptions;
begin
  PDF := THotPDF.Create(nil);
  MetaFile := TMetafile.Create;
  Options := THPDFEmfOptions.Create;
  try
    Options.EmfBitmapScale := 1;
    Options.ColorImageCompression := icFlate;
    Options.UseScreenReferenceDC := False;
    Options.CanvasOver := True;
    Options.Redraw := False;
    Options.UseFrame := True;
    Options.ShowNullPen := True;
    Options.ShowNullBrush := True;

    PDF.BeginDoc;
    PDF.AddPage;
    MetaFile.LoadFromFile('diagram.emf');
    PDF.CurrentPage.ShowMetafile(MetaFile, mpCenter, 0, 0, Options);
    PDF.EndDoc;
  finally
    Options.Free;
    MetaFile.Free;
    PDF.Free;
  end;
end;
        

Enhanced Methods

The enhanced EMF/WMF support introduces several new methods:

THPDFPage.ShowMetafile Overloads

  • ShowMetafile(MetaFile: TMetafile) - Default centered display
  • ShowMetafile(MetaFile: TMetafile; Position: THPDFMetafilePosition) - Position control
  • ShowMetafile(MetaFile: TMetafile; Position: THPDFMetafilePosition; X, Y: Single) - Custom positioning
  • ShowMetafile(MetaFile: TMetafile; Options: THPDFEmfOptions) - Per-call rendering options
  • ShowMetafile(MetaFile: TMetafile; Position: THPDFMetafilePosition; X, Y: Single; Options: THPDFEmfOptions) - Position control with per-call rendering options
  • ShowMetafileEx(MetaFile: TMetafile; Options: THPDFEmfOptions) - direct page API with a one-draw EMF/WMF options override

Enhanced Processing Methods

  • EnhancedShowMetafile - Advanced processing with enhanced features
  • ShowEnhancedMetafile(Page, MetaFile, Options) - Global helper with per-call rendering options
  • ShowEnhancedMetafile(Page, MetaFile, Position, X, Y, Options) - Global helper with position control and per-call rendering options
  • ShowEnhancedMetafile(Page, FileName, Options) - File-name helper with per-call rendering options
  • ShowEnhancedMetafile(Page, FileName, Position, X, Y, Options) - File-name helper with position control and per-call rendering options
  • RenderEmfDirect - Direct EMF rendering for vector preservation
  • RenderEmfAsBitmap - Bitmap conversion for compatibility
  • RenderWmfAsBitmap - WMF bitmap rendering with position control

Technical Implementation

The enhanced EMF/WMF support is implemented through the HPDFEmf.pas unit, which provides:

  • THPDFEmfEnhanced: Core enhancement class for EMF processing
  • THPDFEmfPatcher: Utility class for EMF conversion and rendering
  • THPDFMetafilePosition: Enumeration for position control
  • Enhanced EMF Record Processing: Support for advanced EMF records

Advanced Features

EMF Record Support

  • EMR_BITBLT - Bitmap operations
  • EMR_STRETCHDIBITS - Stretched bitmap operations
  • EMR_ALPHABLEND - Alpha blending operations
  • Enhanced text rendering
  • Complex path operations

Rendering Options

  • Vector Preservation: Maintains vector graphics quality
  • Bitmap Fallback: Automatic fallback for complex graphics
  • Scaling Options: Automatic and manual scaling control
  • Quality Control: Configurable rendering quality settings
  • Canvas Layering: Set THPDFEmfOptions.CanvasOver when existing page content should paint above replayed Canvas metafile content
  • Redraw Control: Leave THPDFEmfOptions.Redraw enabled for the default temporary redraw path or disable it to parse the original metafile directly
  • Frame Clipping: Set THPDFEmfOptions.UseFrame to clip EMF output to the header frame when imported metafiles rely on frame bounds
  • Hatch Brush Patterns: EMF/WMF hatch brushes map to PDF tiling patterns so imported drawings keep their fill texture
  • Null Object Diagnostics: Keep null pens and null brushes invisible by default, or set THPDFEmfOptions.ShowNullPen and THPDFEmfOptions.ShowNullBrush when diagnostics need those paths or fills to become visible

Performance Optimizations

  • Memory-efficient processing for large metafiles
  • Optimized EMF record parsing
  • Intelligent caching for repeated elements
  • Reduced memory footprint during conversion

Compatibility and Standards

  • Windows EMF 1.0 and Enhanced EMF support
  • Windows Metafile (WMF) backward compatibility
  • Cross-platform rendering consistency
  • PDF vector graphics standards compliance

Troubleshooting

Common Issues and Solutions:

  • Large Metafiles: Use bitmap rendering for very large or complex metafiles
  • Text Rendering: Ensure fonts are available on the target system
  • Complex Graphics: Consider direct EMF rendering for better quality
  • Positioning Issues: Use custom positioning for precise placement

See Also