Office.Interop.Word: 如何在不压缩的情况下将图片添加到文档
Office.Interop.Word: How to add a picture to document without getting compressed
如何使用 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
参数但是保存的图像是压缩的并且不需要 link 因为图片文件不能存在于外部。
对于 Excel 有 Shapes.AddPicture2
Method 和 MsoPictureCompress
参数似乎是为了这个。但是我找不到 Word 的任何等效项。
到目前为止,我只找到了解决此问题的方法:
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();
根据 Microsoft 文档2002066,您可以添加以下 DWORD 条目...
AutomaticPictureCompressionDefault = 0
...到以下注册表项:
对于 PowerPoint:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\PowerPoint\Options
单词:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\Word\Options
对于Excel:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Options
这在 Office 2019/Office 365 中仍然有效(然后您需要将 12.0
更改为 16.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();
}
如何使用 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
参数但是保存的图像是压缩的并且不需要 link 因为图片文件不能存在于外部。
对于 Excel 有 Shapes.AddPicture2
Method 和 MsoPictureCompress
参数似乎是为了这个。但是我找不到 Word 的任何等效项。
到目前为止,我只找到了解决此问题的方法:
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();
根据 Microsoft 文档2002066,您可以添加以下 DWORD 条目...
AutomaticPictureCompressionDefault = 0
...到以下注册表项:
对于 PowerPoint:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\PowerPoint\Options
单词:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\Word\Options
对于Excel:
HKEY_CURRENT_USER\Software\Microsoft\Office.0\Excel\Options
这在 Office 2019/Office 365 中仍然有效(然后您需要将 12.0
更改为 16.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();
}