microsoft.office.interop.word如何插入图片 - 小众知识

microsoft.office.interop.word如何插入图片

2022-12-25 13:11:08 苏内容
  标签: word
阅读:2378

Office.Interop.Word: How to add a picture to document without getting compressed

How can I add a picture to a word document without loosing quality using the Microsoft.Office.Interop.Word assembly?

The common way to insert a picture to word document is:

Application wordApp = new Application();
Document wordDoc = wordApp.Documents.Add();
Range docRange = wordDoc.Range();

string imageName = @"c:\temp\win10.jpg";
InlineShape pictureShape = docRange.InlineShapes.AddPicture(imageName);

wordDoc.SaveAs2(@"c:\temp\test.docx");
wordApp.Quit();

This way compresses the picture.


So far I have only found a workaround for this issue:

Application wordApp = new Application();
Document wordDoc = wordApp.Documents.Add();
Range docRange = wordDoc.Range();

string imagePath = @"c:\temp\win10.jpg";

// Create an InlineShape in the InlineShapes collection where the picture should be added later
// It is used to get automatically scaled sizes.
InlineShape autoScaledInlineShape = docRange.InlineShapes.AddPicture(imagePath);
float scaledWidth = autoScaledInlineShape.Width;
float scaledHeight = autoScaledInlineShape.Height;
autoScaledInlineShape.Delete();

// Create a new Shape and fill it with the picture
Shape newShape = wordDoc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight);
newShape.Fill.UserPicture(imagePath);

// Convert the Shape to an InlineShape and optional disable Border
InlineShape finalInlineShape = newShape.ConvertToInlineShape();
finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

// Cut the range of the InlineShape to clipboard
finalInlineShape.Range.Cut();

// And paste it to the target Range
docRange.Paste();

wordDoc.SaveAs2(@"c:\temp\test.docx");
wordApp.Quit();




2

According to Microsoft documentation 2002066 you can add the following DWORD entry...

AutomaticPictureCompressionDefault = 0

...to the following registry keys:

For PowerPoint:

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\PowerPoint\Options

For Word:

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Options

For Excel:

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options

This still works in Office 2019/Office 365 (You need to change 12.0 to 16.0 then). However, all future documents will not compress any images! This may result in very large file sizes!




Add Images To Docx:

    private void ImageToDocx(List<string> Images)
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Document wordDoc = wordApp.Documents.Add();
            Range docRange = wordDoc.Range();

            float mHeight = 0;
            for (int i = 0; i <= Images.Count - 1; i++)
            {
                // Create an InlineShape in the InlineShapes collection where the picture should be added later
                // It is used to get automatically scaled sizes.
                InlineShape autoScaledInlineShape = docRange.InlineShapes.AddPicture(Images[i]);
                float scaledWidth = autoScaledInlineShape.Width;
                float scaledHeight = autoScaledInlineShape.Height;
                mHeight += scaledHeight;
                autoScaledInlineShape.Delete();

                // Create a new Shape and fill it with the picture
                Shape newShape = wordDoc.Shapes.AddShape(1, 0, 0, scaledWidth, mHeight);
                newShape.Fill.UserPicture(Images[i]);

                // Convert the Shape to an InlineShape and optional disable Border
                InlineShape finalInlineShape = newShape.ConvertToInlineShape();
                finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

                // Cut the range of the InlineShape to clipboard
                finalInlineShape.Range.Cut();

                // And paste it to the target Range
                docRange.Paste();
            }

            wordDoc.SaveAs2(@"c:\temp\test.docx");
            wordApp.Quit();
}


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1