我正在更新切割并将PowerPoint幻灯片拼接在一起以供最终用户使用的软件。幻灯片包含图表。我需要找到一种方法来隐藏接收文件的用户的原始图表数据。使用MS Office Interop隐藏来自Powerpoint图表的数据
无论如何在PowerPoint互操作中本身做到这一点? 我试过只读,但用户仍然可以获取数据。
我正在更新切割并将PowerPoint幻灯片拼接在一起以供最终用户使用的软件。幻灯片包含图表。我需要找到一种方法来隐藏接收文件的用户的原始图表数据。使用MS Office Interop隐藏来自Powerpoint图表的数据
无论如何在PowerPoint互操作中本身做到这一点? 我试过只读,但用户仍然可以获取数据。
好的。这是我对我的问题的最终答案。 这显示完成的代码,可以完美地复制幻灯片,同时保持图表不被编辑,即购买将其转换为.png。
这也解决了前面的占位符剩下的问题。
希望这些代码中的一部分对像我那样拖网的人有帮助。
//Open the slides presentation
var pres = _application.Presentations.Open2007(item.PresentationPath,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
//For slide ranges
foreach (var i in range)
{
//Get the old slide
var oldSlide = pres.Slides[i];
oldSlide.Copy();
//Paste the slide into the new presentation
var newSlide = newPresentation.Slides.Paste(totalSlides + 1);
newSlide.Design = oldSlide.Design;
newSlide.ColorScheme = oldSlide.ColorScheme;
/* The shapes haven't retained their content because we are not in slide...
view because there is no window. */
//Delete all the shapes that were just pasted in because of the slide paste.
for (int k = newSlide.Shapes.Count; k > 0; k--)
{
newSlide.Shapes[k].Delete();
}
//Put in our shapes
//Loop forwards, because we arn't editing the list and forward is required to
//maintain the zorder we want on some slides.
for (int j = 1; j <= oldSlide.Shapes.Count; j++)
{
var oldShape = oldSlide.Shapes[j];
oldShape.Copy();
//Paste Put it where it should be on the page
/* This is a special case where the client have put textboxes in the
Powerpoint and rotated throwing off the position, so we need treat as rotated
shapes to make it right. */
if (oldShape.HasTextFrame == MsoTriState.msoTrue && Math.Abs(oldShape.Rotation) > 0)
{
//Paste as a shape because it's a more complex object
//set ALL THE PROPERTIES just in case.
var newShape = newSlide.Shapes.PasteSpecial(PpPasteDataType.ppPasteShape);
newShape.Rotation = oldShape.Rotation;
newShape.Top = oldShape.Top;
newShape.Left = oldShape.Left;
newShape.TextFrame.Orientation = oldShape.TextFrame.Orientation;
newShape.TextFrame.WordWrap = oldShape.TextFrame.WordWrap;
newShape.TextFrame.VerticalAnchor = oldShape.TextFrame.VerticalAnchor;
}
else // Act normally
{
//Paste the old shape into the new slide as an image to ENSURE FORMATTING
var newShape = newSlide.Shapes.PasteSpecial(PpPasteDataType.ppPastePNG);
newShape.Top = oldShape.Top;
newShape.Left = oldShape.Left;
}
}
totalSlides += ((item.EndIndex - item.StartIndex) + 1);
pres.Close();
}
新演示文稿编译完成后,您必须删除所有占位符。
//Loop through all slides
foreach (Slide exportSlide in newPresentation.Slides)
{
//Delete the placeholders
for (int i = exportSlide.Shapes.Placeholders.Count; i > 0; i--)
{
exportSlide.Shapes.Placeholders[i].Delete();
}
}
VBA取消组合图表(以去除原始数据的任何连接)
Sub UngroupAChart()
Dim oSh As Shape
Dim oNewSh As Shape
Dim oNewShapes As ShapeRange
Dim oSl As Slide
' for demo purposes, use the currently selected
' shape; up to tester to select a chart
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Get a reference to the shape's parent slide
' We'll need it later
Set oSl = oSh.Parent
' put the shape on the clipboard
oSh.Copy
Set oNewSh = oSl.Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)
oNewSh.Ungroup
' once you're done testing, delete the original chart
'oSh.Delete
End Sub
使用的史蒂夫的代码,我创建了一个定制一个为我使用的主体。由于我作为组件服务与PowerPoint进行交互,因此我没有ActiveWindow或其他任何东西。
//Loop through all slides
foreach (Slide exportSlide in newPresentation.Slides)
{
//Loop through all shapes
foreach (Shape shape in exportSlide.Shapes)
{
//If the shape is a chart
if (shape.HasChart == MsoTriState.msoTrue)
{
//Copy to clipboard
shape.Copy();
//Paste as an image
var newShape = exportSlide.Shapes.PasteSpecial(PpPasteDataType.ppPastePNG);
//Move back to chart position
newShape.Left = shape.Left;
newShape.Top = shape.Top;
//Delete the original shape
shape.Delete();
}
}
}
这适用于用图像替换图表(根本没有数据)。现在唯一的问题是,因为使用包含图表的布局创建的幻灯片,当您在Powerpoint中打开幻灯片时,GUI会显示一个“插入图表”框。我试过了:
exportSlide.Layout = PpSlideLayout.ppLayoutBlank;
但是,由于我的客户自定义模板,这不是空白的,所以它不可用。
您已经遇到了PowerPoint的一个特点。当你删除一个形状时,它会消失,但是当你删除一个包含在占位符中的形状(例如你的图表)时,你最终会得到一个空的占位符,它也需要被删除。顺便说一句,您需要向后逐步通过形状集合(即从Shapes.Count到1),并通过索引引用形状,而不是在每个循环中使用a。否则,你很可能会错过一些形状。 –
如果您取消组合图表,它会抛出数据并将PPT向您显示的图表的EMF/WMF图片留给您。 –
任何想法,我会如何与C#做到这一点?我目前的想法是将幻灯片导出为图像,然后以幻灯片的形式重新导入它们。 – Callum
不知道C#,但从VBA翻译应该相对简单,在下面/上面的新答案中给出。 –