Skip to content

Commit d07f83b

Browse files
Merge pull request #463 from telerik/new-kb-convert-wmf-to-png-radwordsprocessing-85cc741356324f489626473853158122
Added new kb article convert-wmf-to-png-radwordsprocessing
2 parents befbc82 + 3128a00 commit d07f83b

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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%})

libraries/radwordsprocessing/model/floatingimage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,4 @@ WordsProcessing enables you to export documents with floating images to PDF. How
167167
* [Document model]({%slug radwordsprocessing-model%})
168168
* [ImageInline]({%slug radwordsprocessing-model-imageinline%})
169169
* [Paragraph]({%slug radwordsprocessing-model-paragraph%})
170+
* [Converting WMF Images to PNG in RTF Documents with RadWordsProcessing]({%slug convert-wmf-to-png-radwordsprocessing%})

libraries/radwordsprocessing/model/imageinline.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ This section explains the behavior of the __Size__ property of The __Image__ obj
143143
* [Document model]({%slug radwordsprocessing-model%})
144144
* [FloatingImage]({%slug radwordsprocessing-model-floatingimage%})
145145
* [Paragraph]({%slug radwordsprocessing-model-paragraph%})
146+
* [Converting WMF Images to PNG in RTF Documents with RadWordsProcessing]({%slug convert-wmf-to-png-radwordsprocessing%})

0 commit comments

Comments
 (0)