How to Convert Specific Page of RTF into EMF and set its Page Size using .NET

ist it possible to convert an RTF file into one or more EMFs, with a given, maybe different with?
e.g. given an RTF, and I need an EMF with the size of W by H pixels, and the rest with the width of W’ pixels?
If yes, is it possible to control, how is it cutted? I mean, if the first height is in the middle of a textrow, or can be set a “keep together” for the paragraphs?


Yes, you can convert RTF to EMF file format. Please check the following code example.

Document doc = new Document(MyDir + "input.docx");

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Emf)
    Resolution = 300,
    PageCount = 2
options.PageSavingCallback = new CustomPageFileNamePageSavingCallback();
doc.Save(MyDir + "20.9.Emf", options);

private class CustomPageFileNamePageSavingCallback : IPageSavingCallback
    public void PageSaving(PageSavingArgs args)
        string outFileName = $"{MyDir}SavingCallback.PageFileName.Page_{args.PageIndex}.emf";

        // Specify name of the output file for the current page either in this 
        args.PageFileName = outFileName;

        // ..or by setting up a custom stream
        args.PageStream = new FileStream(outFileName, FileMode.Create);

You can change the page size of document using PageSetup.PageWidth and PageSetup.PageHeight properties and convert the document to EMF files using above code example.

Could you please share some more detail about your requirement along with input RTF and expected output EMF files? We will then provide you more information on it.

thanks a lot, it is the solution for the question one!
With the PageSetup yes I can set the width and the height, but just global for the whole output. But I need different width and different height for the different pages, also for the different emf files. Is that possible?

And is it possible to control, what should keep together?


Yes, it is possible. We suggest you please use the page splitter utility to split document’s pages into separate documents, set their page width and height, and export the final document to EMF. Please get the code of page splitter utility from following Github link.

1 Like