如何使用Microsoft.Office.Interop.Word
程序集在不降低质量的情况下将图片添加到Word文档中?
将图片插入Word文档的常用方法是:
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();
这种方式压缩了图片。
有可选的LinkToFile
和SaveWithDocument
参数,但保存的图像会被压缩并且不需要链接,因为图片文件不能在外部存在。
对于Excel,this带有MsoPictureCompress
参数,似乎就是这样。但我找不到Word的任何等价物。
答案 0 :(得分:3)
到目前为止,我只找到了解决此问题的方法:
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();
答案 1 :(得分:0)
根据Microsoft文档2002066,您可以添加以下DWORD条目...
AutomaticPictureCompressionDefault = 0
...到以下注册表项:
对于PowerPoint:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\PowerPoint\Options
对于Word:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Options
对于Excel:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options
这在Office 2019 / Office 365中仍然有效(然后您需要将12.0
更改为16.0
)。但是,所有将来的文档都不会压缩任何图像!这可能会导致文件很大!
答案 2 :(得分:0)
将图像添加到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();
}