Skip to content

Commit b929107

Browse files
author
KB Bot
committed
Added new kb article convert-wmf-to-png-radwordsprocessing
1 parent 28dbae7 commit b929107

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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: radwordsprocessing, document processing, rtf, wmf, png, 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 a [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) into 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+
static void Main(string[] args)
38+
{
39+
string inputFilePath = "yourfile.rtf";
40+
Telerik.Windows.Documents.Flow.Model.RadFlowDocument document;
41+
42+
Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
43+
44+
using (Stream input = File.OpenRead(inputFilePath))
45+
{
46+
document = provider.Import(input);
47+
}
48+
49+
ConvertInlineWmfImagesToPng(document);
50+
51+
string outputFilePath = "converted.rtf";
52+
File.Delete(outputFilePath);
53+
using (Stream output = File.Create(outputFilePath))
54+
{
55+
provider.Export(document, output);
56+
}
57+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
58+
}
59+
60+
private static void ConvertInlineWmfImagesToPng(RadFlowDocument document)
61+
{
62+
foreach (FloatingImage image in document.EnumerateChildrenOfType<FloatingImage>())
63+
{
64+
if (image.Image.ImageSource.Extension.Equals("wmf", StringComparison.InvariantCultureIgnoreCase))
65+
{
66+
using (MemoryStream wmfImageStream = new MemoryStream(image.Image.ImageSource.Data))
67+
{
68+
using (MemoryStream pngImageStream = new MemoryStream())
69+
{
70+
var imageDrawing = System.Drawing.Image.FromStream(wmfImageStream);
71+
imageDrawing.Save(pngImageStream, System.Drawing.Imaging.ImageFormat.Png);
72+
byte[] pngBytes = pngImageStream.ToArray();
73+
74+
image.Image.ImageSource = new ImageSource(pngBytes, "png");
75+
}
76+
}
77+
}
78+
}
79+
}
80+
```
81+
82+
### Notes
83+
84+
- Ensure you have referenced all necessary assemblies and namespaces, particularly those related to Telerik Document Processing and System.Drawing for image conversion.
85+
- 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.
86+
- 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.
87+
88+
## See Also
89+
90+
- [RadWordsProcessing Documentation]({%slug radwordsprocessing-overview%})
91+
- [RadFlowDocument Overview]({%slug radwordsprocessing-model-radflowdocument%})
92+
- [RtfFormatProvider Documentation]({%slug radwordsprocessing-formats-and-conversion-rtf-rtfformatprovider%})

0 commit comments

Comments
 (0)