Search code examples
c#imagems-wordcrystal-reportsole

How to get a part of docx file as image, not whole page


I have been trying to insert a docx file contents into crystal reports. (In Crystal Reports if I insert OLE object and select a docx file, program imports only the used part of the word file, not the whole page.)

I have searched to convert Word document to image, but all I found was about getting a whole page or a specific image in a page. In my case I can have text and also line objects and maybe some graphic files in maybe 6 - 7 paragraphs. And only used part of the page(6 - 7 paragraphs) should be my image file.

Our customers are using this Word files as a header of the report. For now the only way I can do is to have screen shoot of the design. And it doesn't always look good. Is there a way to get this part as image?

Note: I can't use directly doc file in crystal reports, I need to have the data in SQL database. So it has to be graphic file.


Solution

  • You may find the Selection.CopyAsPicture method which works the same way as the Copy method. Basically you need to select the required content in a Word Document and then use this method to take a picture of selected content and keep it in a memory for further usage. For example, the following example copies the contents of the active document as a picture and pastes it as a picture at the end of the document:

    Sub CopyPasteAsPicture() 
     ActiveDocument.Content.Select 
     With Selection 
       .CopyAsPicture 
       .Collapse Direction:=wdCollapseEnd 
       .PasteSpecial DataType:=wdPasteMetafilePicture 
     End With 
    End Sub
    

    In C# you could use the same properties and methods, the Word object model is common for all kind of applications.