From b929107e9cb472444731c392a0ae58c68b062ee1 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Tue, 27 Aug 2024 13:51:05 +0000 Subject: [PATCH 1/3] Added new kb article convert-wmf-to-png-radwordsprocessing --- .../convert-wmf-to-png-radwordsprocessing.md | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 knowledge-base/convert-wmf-to-png-radwordsprocessing.md diff --git a/knowledge-base/convert-wmf-to-png-radwordsprocessing.md b/knowledge-base/convert-wmf-to-png-radwordsprocessing.md new file mode 100644 index 00000000..675eb37f --- /dev/null +++ b/knowledge-base/convert-wmf-to-png-radwordsprocessing.md @@ -0,0 +1,92 @@ +--- +title: Converting WMF Images to PNG in RTF Documents with RadWordsProcessing +description: Learn how to programmatically convert WMF images to PNG within RTF files using RadWordsProcessing. +type: how-to +page_title: How to Convert WMF to PNG in RTF Files Using Telerik Document Processing +slug: convert-wmf-to-png-radwordsprocessing +tags: radwordsprocessing, document processing, rtf, wmf, png, image conversion +res_type: kb +ticketid: 1662500 +--- + +## Environment + +| Version | Product | Author | +| --- | --- | ---- | +| 2024.3.806| RadWordsProcessing|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +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. + +## Solution + +To convert WMF images to PNG format within an RTF document using RadWordsProcessing, follow these steps: + +1. [Import the RTF file](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#import) into a `RadFlowDocument`. + +2. Iterate through the [floating images]({%slug radwordsprocessing-model-floatingimage%}) in the document. + +3. Convert each WMF image to PNG format. + +4. [Export the modified document](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#export). + +Here is a sample code snippet demonstrating this process: + +```csharp +static void Main(string[] args) +{ + string inputFilePath = "yourfile.rtf"; + Telerik.Windows.Documents.Flow.Model.RadFlowDocument document; + + Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider(); + + using (Stream input = File.OpenRead(inputFilePath)) + { + document = provider.Import(input); + } + + ConvertInlineWmfImagesToPng(document); + + string outputFilePath = "converted.rtf"; + File.Delete(outputFilePath); + using (Stream output = File.Create(outputFilePath)) + { + provider.Export(document, output); + } + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); +} + +private static void ConvertInlineWmfImagesToPng(RadFlowDocument document) +{ + foreach (FloatingImage image in document.EnumerateChildrenOfType()) + { + if (image.Image.ImageSource.Extension.Equals("wmf", StringComparison.InvariantCultureIgnoreCase)) + { + using (MemoryStream wmfImageStream = new MemoryStream(image.Image.ImageSource.Data)) + { + using (MemoryStream pngImageStream = new MemoryStream()) + { + var imageDrawing = System.Drawing.Image.FromStream(wmfImageStream); + imageDrawing.Save(pngImageStream, System.Drawing.Imaging.ImageFormat.Png); + byte[] pngBytes = pngImageStream.ToArray(); + + image.Image.ImageSource = new ImageSource(pngBytes, "png"); + } + } + } + } +} +``` + +### Notes + +- Ensure you have referenced all necessary assemblies and namespaces, particularly those related to Telerik Document Processing and System.Drawing for image conversion. +- 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. +- 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. + +## See Also + +- [RadWordsProcessing Documentation]({%slug radwordsprocessing-overview%}) +- [RadFlowDocument Overview]({%slug radwordsprocessing-model-radflowdocument%}) +- [RtfFormatProvider Documentation]({%slug radwordsprocessing-formats-and-conversion-rtf-rtfformatprovider%}) From 57ceb01739d431207923257cfcebb9157c24de70 Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Tue, 27 Aug 2024 18:04:55 +0300 Subject: [PATCH 2/3] add links --- libraries/radwordsprocessing/model/floatingimage.md | 1 + libraries/radwordsprocessing/model/imageinline.md | 1 + 2 files changed, 2 insertions(+) diff --git a/libraries/radwordsprocessing/model/floatingimage.md b/libraries/radwordsprocessing/model/floatingimage.md index b2f2979c..7de2239c 100644 --- a/libraries/radwordsprocessing/model/floatingimage.md +++ b/libraries/radwordsprocessing/model/floatingimage.md @@ -167,3 +167,4 @@ WordsProcessing enables you to export documents with floating images to PDF. How * [Document model]({%slug radwordsprocessing-model%}) * [ImageInline]({%slug radwordsprocessing-model-imageinline%}) * [Paragraph]({%slug radwordsprocessing-model-paragraph%}) + * [Converting WMF Images to PNG in RTF Documents with RadWordsProcessing]({%slug convert-wmf-to-png-radwordsprocessing%}) diff --git a/libraries/radwordsprocessing/model/imageinline.md b/libraries/radwordsprocessing/model/imageinline.md index d441cf3b..cce71cee 100644 --- a/libraries/radwordsprocessing/model/imageinline.md +++ b/libraries/radwordsprocessing/model/imageinline.md @@ -143,3 +143,4 @@ This section explains the behavior of the __Size__ property of The __Image__ obj * [Document model]({%slug radwordsprocessing-model%}) * [FloatingImage]({%slug radwordsprocessing-model-floatingimage%}) * [Paragraph]({%slug radwordsprocessing-model-paragraph%}) + * [Converting WMF Images to PNG in RTF Documents with RadWordsProcessing]({%slug convert-wmf-to-png-radwordsprocessing%}) From 3128a003c98130ebc4760d8c98213f3ce26ef25d Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Wed, 4 Sep 2024 18:16:03 +0300 Subject: [PATCH 3/3] addressed feedback --- .../convert-wmf-to-png-radwordsprocessing.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/knowledge-base/convert-wmf-to-png-radwordsprocessing.md b/knowledge-base/convert-wmf-to-png-radwordsprocessing.md index 675eb37f..cede2038 100644 --- a/knowledge-base/convert-wmf-to-png-radwordsprocessing.md +++ b/knowledge-base/convert-wmf-to-png-radwordsprocessing.md @@ -4,7 +4,7 @@ description: Learn how to programmatically convert WMF images to PNG within RTF type: how-to page_title: How to Convert WMF to PNG in RTF Files Using Telerik Document Processing slug: convert-wmf-to-png-radwordsprocessing -tags: radwordsprocessing, document processing, rtf, wmf, png, image conversion +tags: wordsprocessing, rtf, wmf, png, document, processing, image, conversion res_type: kb ticketid: 1662500 --- @@ -17,13 +17,13 @@ ticketid: 1662500 ## Description -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. +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. ## Solution To convert WMF images to PNG format within an RTF document using RadWordsProcessing, follow these steps: -1. [Import the RTF file](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#import) into a `RadFlowDocument`. +1. [Import the RTF file](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/rtf/rtfformatprovider#import) as a `RadFlowDocument`. 2. Iterate through the [floating images]({%slug radwordsprocessing-model-floatingimage%}) in the document. @@ -34,6 +34,11 @@ To convert WMF images to PNG format within an RTF document using RadWordsProcess Here is a sample code snippet demonstrating this process: ```csharp +using System.Diagnostics; +using Telerik.Windows.Documents.Flow.Model; +using Telerik.Windows.Documents.Flow.Model.Shapes; +using Telerik.Windows.Documents.Media; + static void Main(string[] args) { string inputFilePath = "yourfile.rtf";