|
| 1 | +--- |
| 2 | +title: Converting WMF Images to PNG in RTF Documents with RadWordsProcessing |
| 3 | +description: Learn how to programmatically convert WMF images to PNG within RTF files using RadWordsProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Convert WMF to PNG in RTF Files Using Telerik Document Processing |
| 6 | +slug: convert-wmf-to-png-radwordsprocessing |
| 7 | +tags: wordsprocessing, rtf, wmf, png, document, processing, image, conversion |
| 8 | +res_type: kb |
| 9 | +ticketid: 1662500 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.3.806| RadWordsProcessing|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This article demonstrates a sample approach how to convert the images in WMF format to PNG within an [RTF]({%slug radwordsprocessing-formats-and-conversion-rtf%}) document. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To convert WMF images to PNG format within an RTF document using RadWordsProcessing, follow these steps: |
| 25 | + |
| 26 | +1. [Import the RTF file](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#import) as a `RadFlowDocument`. |
| 27 | + |
| 28 | +2. Iterate through the [floating images]({%slug radwordsprocessing-model-floatingimage%}) in the document. |
| 29 | + |
| 30 | +3. Convert each WMF image to PNG format. |
| 31 | + |
| 32 | +4. [Export the modified document](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#export). |
| 33 | + |
| 34 | +Here is a sample code snippet demonstrating this process: |
| 35 | + |
| 36 | +```csharp |
| 37 | +using System.Diagnostics; |
| 38 | +using Telerik.Windows.Documents.Flow.Model; |
| 39 | +using Telerik.Windows.Documents.Flow.Model.Shapes; |
| 40 | +using Telerik.Windows.Documents.Media; |
| 41 | + |
| 42 | +static void Main(string[] args) |
| 43 | +{ |
| 44 | + string inputFilePath = "yourfile.rtf"; |
| 45 | + Telerik.Windows.Documents.Flow.Model.RadFlowDocument document; |
| 46 | + |
| 47 | + Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider(); |
| 48 | + |
| 49 | + using (Stream input = File.OpenRead(inputFilePath)) |
| 50 | + { |
| 51 | + document = provider.Import(input); |
| 52 | + } |
| 53 | + |
| 54 | + ConvertInlineWmfImagesToPng(document); |
| 55 | + |
| 56 | + string outputFilePath = "converted.rtf"; |
| 57 | + File.Delete(outputFilePath); |
| 58 | + using (Stream output = File.Create(outputFilePath)) |
| 59 | + { |
| 60 | + provider.Export(document, output); |
| 61 | + } |
| 62 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 63 | +} |
| 64 | + |
| 65 | +private static void ConvertInlineWmfImagesToPng(RadFlowDocument document) |
| 66 | +{ |
| 67 | + foreach (FloatingImage image in document.EnumerateChildrenOfType<FloatingImage>()) |
| 68 | + { |
| 69 | + if (image.Image.ImageSource.Extension.Equals("wmf", StringComparison.InvariantCultureIgnoreCase)) |
| 70 | + { |
| 71 | + using (MemoryStream wmfImageStream = new MemoryStream(image.Image.ImageSource.Data)) |
| 72 | + { |
| 73 | + using (MemoryStream pngImageStream = new MemoryStream()) |
| 74 | + { |
| 75 | + var imageDrawing = System.Drawing.Image.FromStream(wmfImageStream); |
| 76 | + imageDrawing.Save(pngImageStream, System.Drawing.Imaging.ImageFormat.Png); |
| 77 | + byte[] pngBytes = pngImageStream.ToArray(); |
| 78 | + |
| 79 | + image.Image.ImageSource = new ImageSource(pngBytes, "png"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Notes |
| 88 | + |
| 89 | +- Ensure you have referenced all necessary assemblies and namespaces, particularly those related to Telerik Document Processing and System.Drawing for image conversion. |
| 90 | +- The code snippet assumes the presence of a WMF image in the document. If your document contains images in formats other than WMF, they won't be affected. |
| 91 | +- This solution applies to floating images. For inline images, a similar approach can be taken with slight adjustments to target [InlineImage]({%slug radwordsprocessing-model-imageinline%}) instances if necessary. |
| 92 | + |
| 93 | +## See Also |
| 94 | + |
| 95 | +- [RadWordsProcessing Documentation]({%slug radwordsprocessing-overview%}) |
| 96 | +- [RadFlowDocument Overview]({%slug radwordsprocessing-model-radflowdocument%}) |
| 97 | +- [RtfFormatProvider Documentation]({%slug radwordsprocessing-formats-and-conversion-rtf-rtfformatprovider%}) |
0 commit comments