2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. In .NET Core targeting applications, metafile images (.wmf and *.emf) have some limitations. Internally, if the Word document contains metafile images (.wmf” or *.emf”), Essential DocIO preserves those images as RedX images with the same size as the original metafile images during Word to PDF or Image conversions to avoid pagination issues. To convert and replace EMF image in word document to PNG of the same size using C#
C# using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocIORenderer; using Syncfusion.Pdf; using System.Drawing; using System.Drawing.Imaging; VB Imports System.Drawing Imports System.Drawing.Imaging Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS Imports Syncfusion.DocIORenderer Imports Syncfusion.Pdf
C# //Open the file as a Stream. using (FileStream docStream = new FileStream(@"../../../SyncfusionConvertWordToPdfIssueDoc.docx", FileMode.Open, FileAccess.Read)) { //Load a file stream into a Word document. using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic)) { ConvertEMFToPNG(wordDocument); //Instantiation of DocIORenderer for Word to PDF conversion. DocIORenderer render = new DocIORenderer(); //Convert a Word document into a PDF document. using (PdfDocument pdfDocument = render.ConvertToPDF(wordDocument)) { //Save the PDF file. using (FileStream outputFile = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) pdfDocument.Save(outputFile); } } }
VB 'Open the file as a Stream Using docStream As FileStream = New FileStream("../../../SyncfusionConvertWordToPdfIssueDoc.docx", FileMode.Open, FileAccess.Read) 'Load a file stream into a Word document Using wordDocument As WordDocument = New WordDocument(docStream, FormatType.Automatic) ConvertEMFToPNG(wordDocument) 'Instantiation of DocIORenderer for Word to PDF conversion Dim render As DocIORenderer = New DocIORenderer 'Convert a Word document into a PDF document Using pdfDocument As PdfDocument = render.ConvertToPDF(wordDocument) 'Saves the PDF file Using outputFile As FileStream = New FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite) pdfDocument.Save(outputFile) End Using End Using End Using End Using
C# private static void ConvertEMFToPNG(WordDocument wordDocument) { WTextBody textbody = wordDocument.Sections[0].Body; //Iterates through the paragraphs of the textbody foreach (WParagraph paragraph in textbody.Paragraphs) { //Iterates through the child elements of a paragraph foreach (ParagraphItem item in paragraph.ChildEntities) { if (item is WPicture) { WPicture picture = item as WPicture; Image image = Image.FromStream(new MemoryStream(picture.ImageBytes)); if (image.RawFormat.Equals(ImageFormat.Emf)) { float height = picture.Height; float width = picture.Width; FileStream imgFile = new FileStream("Output.png", FileMode.OpenOrCreate, FileAccess.ReadWrite); image.Save(imgFile, ImageFormat.Png); imgFile.Dispose(); image.Dispose(); FileStream imageStream = new FileStream(@"Output.png", FileMode.Open, FileAccess.ReadWrite); picture.LoadImage(imageStream); picture.LockAspectRatio = false; picture.Height = height; picture.Width = width; } } } } }
VB
Private Sub ConvertEMFToPNG(ByVal wordDocument As WordDocument) Dim textbody As WTextBody = wordDocument.Sections(0).Body 'Iterates through the paragraphs of the textbody For Each paragraph As WParagraph In textbody.Paragraphs 'Iterates through the child elements of a paragraph For Each item As ParagraphItem In paragraph.ChildEntities If (TypeOf item Is WPicture) Then Dim picture As WPicture = CType(item, WPicture) Dim image As Image = Image.FromStream(New MemoryStream(picture.ImageBytes)) If image.RawFormat.Equals(ImageFormat.Emf) Then Dim height As Single = picture.Height Dim width As Single = picture.Width Dim imgFile As FileStream = New FileStream("Output.png", FileMode.OpenOrCreate, FileAccess.ReadWrite) image.Save(imgFile, ImageFormat.Png) imgFile.Dispose() image.Dispose Dim imageStream As FileStream = New FileStream("Output.png", FileMode.Open, FileAccess.ReadWrite) picture.LoadImage(imageStream) picture.LockAspectRatio = False picture.Height = height picture.Width = width End If End If Next Next End Sub
A complete working example in C# can be downloaded from the sample. Take a moment to peruse the documentation, where you can find the basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples. Explore more about the rich set of Syncfusion Word Framework features. Note: Starting with v16.2.0.x, if you refer to Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
|
2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.