ComponentOne PDF for .NET
Using Metafiles
Using ComponentOne PDF for .NET > Using Metafiles

PDF for .NET makes it very easy to create documents, mainly because the object model mimics the well-known .NET Graphics model. However, not all methods available in the Graphics class are available in PDF for .NET. Plus, you may have existing code that draws to a Graphics object and that you do not want to rewrite even if most methods are very similar.

In these cases, you can reuse your existing .NET code by sending the drawing commands to a Metafile, then rendering the Metafile into PDF for .NET using the DrawImage command. This method allows you to expose any graphics you create as images or as PDF documents.

For example, suppose you have an application that generates documents using the PrintDocument pattern of drawing each page into a Graphics object. You could then use the same methods to create a collection of metafiles, one per page, and then convert the list into a PDF document using the following code:

To write code in Visual Basic

Visual Basic
Copy Code
' Get the document as a list of Metafiles, one per page.
Dim pages As ArrayList = GetMetafiles()
 ' Loop through the pages and create a PDF document.
_c1pdf.Clear()
Dim i As Integer
for i = 0 i <= pages.Count
     ' Get ith page.
    Dim page As Metafile = CType(Metafile.FromFile(pages[i]), Metafile)
    If Not (page Is Nothing) Then
         ' Calculate the page size.
        Dim sz As SizeF = page.PhysicalDimension
        sz.Width = Math.Round(sz.Width * 72.0F / 2540.0F, 2)
        sz.Height = Math.Round(sz.Height * 72.0F / 2540.0F, 2)
         ' Add a page and set the size.
        If i > 0 Then
            _c1pdf.NewPage()
        End If
        _c1pdf.PageSize = sz
         ' Draw the page into the PDF document.
        _c1pdf.DrawImage(page, _c1pdf.PageRectangle)
    End If
Next
 ' Save to file.
_c1pdf.Save("c:\temp\mydoc.pdf")

To write code in C#

C#
Copy Code
// Get the document as a list of Metafiles, one per page.
ArrayList pages = GetMetafiles();
 // Loop through the pages and create a PDF document.
_c1pdf.Clear();
for (int i = 0; i < pages.Count; i++)
{
     // Get ith page.
    Metafile page = (Metafile)Metafile.FromFile(pages[i]);
    if (page == null)
    {
        continue;
    }
     // Calculate the page size.
    SizeF sz = page.PhysicalDimension;
    sz.Width  = (float)Math.Round(sz.Width  * 72f / 2540f, 2);
    sz.Height = (float)Math.Round(sz.Height * 72f / 2540f, 2);
     // Add a page and set the size
    if (i > 0) _c1pdf.NewPage();
    _c1pdf.PageSize = sz;
     // Draw the page into the PDF document
    _c1pdf.DrawImage(page, _c1pdf.PageRectangle);
}
 // Save to file
_c1pdf.Save(@"c:\temp\mydoc.pdf");

The code gets each metafile on the list, calculates its size in points (each page could have a different size), then draws the metafile into the page. The metafiles could be generated by a reporting engine, drawing or charting program, or any application that can create metafile images.

PDF for .NET also supports EMF+ metafiles. According to Microsoft, EMF+ is an extension to EMF that allows GDI+ records to be stored. Previously, all metafiles were transformed to EFM Only. EMF+ metafiles are no longer transformed; therefore, they do not lose their context due to transformation.