Converting a .NET Drawing.Imaging.Metafile directly to PDF


#1
I have been looking throughout all the Aspose offerings to see if there is a way to take a .NET Drawing.Imaging.Metafile object and directly write it out to a PDF file. I have found a few different example solutions in the Words, Imaging, and PDF API's that can take various file types and export into a PDF, however I'm hoping there's a solution available to take the Metafile directly without having to save it first. This is because it's difficult (although not impossible) to save the Metafile to disk and still keep it as a vector based image.

So far my best solution has been to use a work-around to be able to save the Metafile to an EMF, then using the Words API and example code taken from the "ConvertImageToPDF" method found here, I have successfully been able to generate the PDF. However, is this the best solution? Is there something I'm missing that can make this all easier?


Any insight is greatly appreciated!

#2

Hi Steven,


Thanks for contacting support and sorry for the delayed response.

We have an API named Aspose.Imaging for .NET which provides the capabilities to manipulate image formats. As per your requirement, I am afraid currently it does not support direct conversion of WMF to PDF format. However you may consider Converting WMF to PNG format and either use Aspose.Words to Convert an Image to PDF or try using Aspose.Pdf for .NET to Add Image to Existing PDF File.

We are sorry for this inconvenience.

#3

Hi Steven,


Adding more to my previous comments, I have further discussed the scenario with respective team and in order to perform direct WMF to PDF conversion, you can only use Aspose.Imaging for .NET API. Please take a look over following code snippet to fulfill the requirement.

In case you encounter any issue, please share the input WMF file, so that we can test the scenario in our environment.

[C#]

// Load an existing WMF image<o:p></o:p>

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load("c:/pdftest/sample.wmf"))

{

// Create an instance of EmfRasterizationOptions class and set different properties

Aspose.Imaging.ImageOptions.EmfRasterizationOptions emfRasterizationOptions = new Aspose.Imaging.ImageOptions.EmfRasterizationOptions();

emfRasterizationOptions.BackgroundColor = Aspose.Imaging.Color.WhiteSmoke;

emfRasterizationOptions.PageWidth = image.Width;

emfRasterizationOptions.PageHeight = image.Height;

// Create an instance of PdfOptions class and provide rasterization option

Aspose.Imaging.ImageOptions.PdfOptions pdfOptions = new Aspose.Imaging.ImageOptions.PdfOptions();

pdfOptions.VectorRasterizationOptions = emfRasterizationOptions;

// Call the save method, provide output path and PdfOptions to convert the WMF file to PDF and save the output

image.Save("c:/pdftest/ConvertWMFToPDF_out.pdf", pdfOptions);

}


#4

Thanks for the replies, however my goal was to use something that didn’t require loading a file from disk, but could use the .Net Metafile directly in memory. Unfortunately it doesn’t sound like there’s anything available for that.


I previously found the code example you have provide somewhere else on the Aspose website, and it didn’t quite work as I expected. I have attached my test WMF file, along with the output PDF. The only thing to note is that my project is in VB, so I altered the example code accordingly.

The code that I have found works best so far is to use the Aspose Words API, example code is below. Using the same WMF as the previous test, the resulting PDF looks correct.


Dim doc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(doc)

'Read the image from file, ensure it Is disposed.
Using image As Drawing.Image = Drawing.Image.FromFile(wmfFilePath)
'Find which dimension the frames in this image represent. For example
'The frames of a BMP Or TIFF are “page dimension” whereas frames of a GIF image are “time dimension”.
Dim dimension As New Drawing.Imaging.FrameDimension(image.FrameDimensionsList(0))

'Get the number of frames in the image.
Dim framesCount As Integer = image.GetFrameCount(dimension)

'Loop through all frames.
For frameIdx As Integer = 0 To framesCount - 1
'Insert a section break before each New page, in case of a multi-frame TIFF.
If (frameIdx <> 0) Then builder.InsertBreak(Aspose.Words.BreakType.SectionBreakNewPage)

'Select active frame.
image.SelectActiveFrame(dimension, frameIdx)

'We want the size of the page to be the same as the size of the image.
'Convert pixels to points to size the page to the actual image size.
Dim ps As Aspose.Words.PageSetup = builder.PageSetup
ps.PageWidth = Aspose.Words.ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution)
ps.PageHeight = Aspose.Words.ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution)

'Insert the image into the document And position it at the top left corner of the page.
builder.InsertImage(image, Aspose.Words.Drawing.RelativeHorizontalPosition.Page, 0, Aspose.Words.Drawing.RelativeVerticalPosition.Page, 0, ps.PageWidth, ps.PageHeight, Aspose.Words.Drawing.WrapType.None)
Next
End Using

'Save the document to PDF.
doc.Save(“C:\temp\AsposeTest.pdf”)


I also tried some example code that uses the PDF API, however that didn’t give the correct result either. It seems odd that three different API’s are can be used to do the same thing but all give different results. At this point it seems best to use the Words API to accomplish what I need, although maybe you can point out something that I’m missing.

Thanks for the assistance!



#5

Hi Steven,


Thanks for your feedback. It is good to know that Aspose.Words is fulfilling your requirement. Your Aspose.Words code to convert image to PDF is fine. However, if you have any further requirement in this regard then please share some more details. We will look into it and will guide you.

Furthermore, please note all APIs have different code base and scope so some variation in results of same file format is expected. However, we are looking into Aspose.Pdf and Aspose.Imaging related issues and will update you accordingly.

We are sorry for the inconvenience.

Best Regards,

#6

Thanks for the reply.


We have been looking into upgrading our Aspose PDF version to the latest, and further utilizing some of its features. One of our main goals was to be able to convert metafiles to PDF, which as mentioned doesn’t seem possible with the current version of the PDF API, but is possible with the Words API. We aren’t keen on requiring the Words API for just this one thing, so my next question is if it’s at all possible to have that functionality brought into the PDF API at some point in the future, or if it would even be considered at all? We are hoping to resolve this and move forward with a solution soon, so any further insight you can provide would be great!

Regards,

Steven


#7

Hi Steven,


Thanks for contacting support.

I am afraid currently Aspose.Pdf for .NET do not support the feature to convert Metafiles directly to PDF format. However for the sake of correction, we already have logged a feature request as PDFNET-35082 in our issue tracking system.

Furthermore, concerning to your query on different results when performing conversion with different APIs, we have tested the scenario of converting WMF using Apose.Imaging API and have managed to reproduce blurred PDF results as you have stated above. A separate issue has been logged with an ID IMAGINGNET-2400. We will further look into the details of this problem and as soon as we have some further updates regarding its resolution, we will let you know.

We are sorry for this inconvenience.

#8

Hello,
I see that the two issues noted here have been set to postponed. Is there any sort of timeframe for when we can expect these fixes to be put in place? We are still having to drag along the Aspose Words API with our installations, which we are keen to avoid and to just be able to make use of the PDF control.

Any update you can provide is appreciated.

Thanks,

Steven


#9

@smkimler,

The linked ticket IDs IMAGINGNET-2400 and PDFNET-35082 are not resolved yet. These tickets could take time because their implementation is long, and other high priority tickets are in queue. In order to escalate priority, we recommend our clients to post their critical issues in the paid support forum. You can also post these ticket IDs in the paid support forum. Please refer to this help topic: Paid Support Helpdesk