Enumerating PowerPoint slides in order - c#-4.0

I'm trying to analyze an existing PowerPoint 2010 .pptx file using the OpenXML SDK 2.0.
What I'm trying to achieve is to
enumerate the slides in order (as they appear in the PPTX)
extracting all textual bits from each slide
I've started and gotten so far - I can enumerate the SlideParts from the PresentationPart - but I cannot seem to find a way to make this an ordered enumeration - the slides are being returned in pretty much arbitrary order...
Any trick to get these slides in the order defined in the PPTX file?
using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
foreach (var slide in presentationPart.SlideParts)
{
...
}
}
I was hoping to find something like a SlideID or Sequence number or something - some item or property I could use in a Linq expression like
.OrderBy(s => s.SlideID)
on that slideparts collection.

It's a bit more involved than I had hoped for - and the docs are a bit sketchy at times....
Basically, I had to enumerate the SlideIdList on the PresentationPart and do some XML-foo to get from that SlideId to the actual slide in the OpenXML presentation.
Something along the lines of:
using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
// get the SlideIdList
var items = presentationPart.Presentation.SlideIdList;
// enumerate over that list
foreach (SlideId item in items)
{
// get the "Part" by its "RelationshipId"
var part = presentationPart.GetPartById(item.RelationshipId);
// this part is really a "SlidePart" and from there, we can get at the actual "Slide"
var slide = (part as SlidePart).Slide;
// do more stuff with your slides here!
}
}

The closest I found was this snippet:
[ISO/IEC 29500-1 1st Edition]
sld (Presentation Slide)
This element specifies a slide within a slide list. The slide list is
used to specify an ordering of slides.
[Example: Consider the following custom show with an ordering of
slides.
<p:custShowLst>
<p:custShow name="Custom Show 1" id="0">
<p:sldLst>
<p:sld r:id="rId4"/>
<p:sld r:id="rId3"/>
<p:sld r:id="rId2"/>
<p:sld r:id="rId5"/>
</p:sldLst>
</p:custShow>
</p:custShowLst>In the above example the order specified to present the slides is slide 4, then 3, 2 and finally 5. end example]
In the MSDN documentation for the slide class
It seems that slides have an r:id of the form rId## where ## is the number of the slide. Maybe that's enough to get you going again?

Related

Changing table column width using Google-Slide-API and python

I have a Spreadsheet document and by using Google-Sheet-API I'm fetching the data from it. Then by using Google-Slide-API I'm creating a Slide document with 'n' slides. On each slide I'm creating a 2-column table with 'n' rows. Every first column on every slide always contains data from the Spreadsheet's A column. The second columns contain data from the other Spreadsheet's columns (from B to 'n'). Then I'm changing the text size. So far so good.
Now, when I try to adjust the columns width of each table on each slide it doesn't do anything which is an issue because some tables contain more information and therefore the tables don't fit the slide. This is the part of the code that doesn't work:
for i in range(number_of_slides):
regs = [
{'updateTableColumnProperties': {
'objectId': tableID[i],
'columnIndices': [j],
'tableColumnProperties': {
'columnWidth': {'magnitude': mag[j], 'unit': 'PT'}
},
'fields': 'columnWidth'
}
} for j in range(2) ]
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=deckID).execute()
The tables always remain the same with or without this part so it doesn't have any affect whatsoever. The code doesn't return any errors or messages.
I believe your goal as follows.
You have a Google Slides.
The Google Slides has the several slides, each slide has a table which has 2 columns.
You want to change the column width of 2 columns.
You want to achieve this using googleapis for python.
You have already been able to use the batchUpdate method using Google Slides API.
Modification points:
I think that your request body is correct. But, I would like to propose one modification point. In your script, batchUpdate method is used in a loop. I think that when batchUpdate is used, updateTableColumnProperties for all slides in a Google Slides can be run by one API call.
Although I'm not sure about your values of mag and your whole script, as a sample script for achieving your above goal, how about the following modified script? If this helped you to understand Slides API, I'm glad.
Modified script:
About creds, please use your authorization script. Also, you can see it at Quickstart for python.
service = build('slides', 'v1', credentials=creds)
PRESENTATION_ID = '###' # Please set Google Slides ID.
magnitude = [100, 300] # Please set the widths for the columns A and B in each table. In this sample, 100 and 300 PT are set.
# 1. Retrieve an object for each slides.
presentation = service.presentations().get(presentationId=PRESENTATION_ID).execute()
# 2. Create a request body for the batchUpdate method.
slides = presentation.get('slides')
requests = []
for slide in slides:
pe = slide.get('pageElements')
if pe:
for pageElement in pe:
t = pageElement.get('table')
if t:
for i, m in enumerate(magnitude):
requests.append({
'updateTableColumnProperties': {
'objectId': pageElement['objectId'],
'columnIndices': [i],
'tableColumnProperties': {
'columnWidth': {'magnitude': m, 'unit': 'PT'}
},
'fields': 'columnWidth'
}
})
# 3. Request the request body.
service.presentations().batchUpdate(body={'requests': requests}, presentationId=PRESENTATION_ID).execute()
Note:
When above script is run, the widths of columns "A" and "B" of the table in each slide are modified. In this sample script, 100 and 300 PT are set for the columns "A" and "B", respectively. About this, please modify for your actual situation.
References:
Method: presentations.get
Method: presentations.batchUpdate
UpdateTableColumnPropertiesRequest

Code not saving when trying to wrap promoted tiles in SharePoint Online

Apologies for another question re this, but I've tried so hard to get this working (I'm fairly new to SharePoint, don't have extensive coding knowledge, but know HTML and generally alright at trouble shooting).
We are using SharePoint online and we have SharePoint Tiles. We have recently added a few more tiles and it's obviously not wrapping these tiles, thus having to scroll right to access some.
I have found the code for wrapping the tiles here and when editing the page source, it appears to be working... until I save it. The code I put in is stripped out when I next go to the content editor.
I've read a few pages on it and have tried things such as the content editor web part, but for the life of me cannot get it to work.
If anyone would know if there's a step to step guide to ensure wrapped tiles are saved, I may be able to get it to work.
Any help is greatly appreciated.
If it changes anything, we use SharePoint online that is part of our Office 365 account.
Add the following code into script editor web part in the page.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Update this value to the number of links you want to show per row
var numberOfLinksPerRow = 6;
// local variables
var pre = "<tr><td><div class='ms-promlink-body' id='promlink_row_";
var post = "'></div></td></tr>";
var numberOfLinksInCurrentRow = numberOfLinksPerRow;
var currentRow = 1
// find the number of promoted links we're displaying
var numberOfPromotedLinks = $('.ms-promlink-body > .ms-tileview-tile-root').length;
// if we have more links then we want in a row, let's continue
if (numberOfPromotedLinks > numberOfLinksPerRow) {
// we don't need the header anymore, no cycling through links
$('.ms-promlink-root > .ms-promlink-header').empty();
// let's iterate through all the links after the maximum displayed link
for (i = numberOfLinksPerRow + 1; i <= numberOfPromotedLinks; i++) {
// if we're reached the maximum number of links to show per row, add a new row
// this happens the first time, with the values set initially
if (numberOfLinksInCurrentRow == numberOfLinksPerRow) {
// i just want the 2nd row to
currentRow++;
// create a new row of links
$('.ms-promlink-root > table > tbody:last').append(pre + currentRow + post);
// reset the number of links for the current row
numberOfLinksInCurrentRow = 0;
}
// move the Nth (numberOfLinksPerRow + 1) div to the current table row
$('#promlink_row_' + currentRow).append($('.ms-promlink-body > .ms-tileview-tile-root:eq(' + (numberOfLinksPerRow) + ')'));
// increment the number of links in the current row
numberOfLinksInCurrentRow++;
}
}
});
</script>
We can also use CSS style below in script editor web part in the page to achieve it.
<style>
.ms-promlink-body {
width: 960px;
}
</style>

F# Winforms Charting Asynchronous Updating

I'm trying to create a chart in winforms that databinds to a list in memory, and gets updated dynamically as the list changes. Here is my code:
open System
open System.Linq
open System.Collections
open System.Collections.Generic
open System.Drawing
open System.Windows.Forms
open System.Windows.Forms.DataVisualization
open System.Windows.Forms.DataVisualization.Charting
let link = new LinkedList<double>()
let rnd = new System.Random()
for i in 1 .. 10 do link.AddFirst(rnd.NextDouble()) |> ignore
let series = new Series()
let chart = new System.Windows.Forms.DataVisualization.Charting.Chart(Dock = DockStyle.Fill, Palette = ChartColorPalette.Pastel)
series.Points.DataBindY(link)
let form = new Form(Visible = true, Width = 700, Height = 500)
form.Controls.Add(chart)
let formloop = async {
while not chart.IsDisposed do
link.AddFirst((new System.Random()).NextDouble()) |> ignore
link.RemoveLast()
}
do
Async.StartImmediate(formloop)
Application.Run(form)
Console.WriteLine("Done")
Console.ReadLine() |> ignore
The async seems to work, but the chart never shows anything. It just shows a blank window. What am I doing wrong?
LinkedList<T> has no way to signal that it's been updated, so Chart has no way of knowing when to redraw itself.
In order for databinding to update the view, the source list must implement IBindingList and raise appropriate event when the contents change.
Separately, I must point out that it's dangerous to directly access UI properties/methods from non-UI threads (such as chart.IsDisposed in your code). In WinForms, this limitation is rarely actually enforced, so sometimes this might seem to work fine, only to crash later on a customer's machine with no way to attach a debugger.
You need to add the series to the SeriesCollection of the chart.
chart.Series.Add series
You need to construct a chart area and add it to the ChartAreaCollection of the chart.
let area = new ChartArea()
chart.ChartAreas.Add area
You need to make sure that the data binding method is called after the chart and form are set up.
...
form.Controls.Add chart
series.Points.DataBindY link
And now there's no way to communicate changes of your bound collection to the DataPointCollection of the series, as mentioned in Fyodor Soikin's answer. I'm not quite sure that IBindingList is an appropriate response;
while it's possible to hook into the ListChanged event, we could as well manipulate the series' DataPointCollection directly.
let formloop = async{
while not chart.IsDisposed do
series.Points.RemoveAt 0
series.Points.AddY(rnd.NextDouble()) |> ignore
do! Async.Sleep 100 }
Finally I'd like to point out this contribution by John Atwood which adresses both points raised by Fyodor; the data binding issue (by not using it) and the UI-thread safety issue.

HowTo: Visio SDK page Re-Layout FlowChart Top to Bottom

I'm creating a dynamic VSD from a hierarchical set of data that represents a flowchart. I don't want/need to fuddle with absolute positioning of these elements - the automatic layout options will work just fine.
The problem is I can't figure out how to perform this command via code. In the UI (Visio 2010), the commands are on the ribbon here: Design (tab) -> Layout (group) -> Re-Layout (SplitButton).
Any of these will do. Looking through the Visio SDK documentation and Googling for a couple days have turned up nothing of very much use.
Any ideas? (using C#, but VB/VBA would do)
The Page.Layout() method itself is not enough.
In the WBSTreeView.sln sample project (VB.Net) I found how to accomplish this, but couldn't post my answer until 8 hours later :-x
The other layout types are possible by looking through the enums used below.
Compact -> DownRight just ended up being better for most of the flows we're creating.
Translated to C#:
// auto-layout, Compact Tree -> Down then Right
var layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLOPlaceStyle);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visPLOPlaceCompactDownRight);
layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLORouteStyle);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visLORouteFlowchartNS);
//// to change page orientation
//layoutCell = this._page.PageSheet.get_CellsSRC(
// (short)VisSectionIndices.visSectionObject,
// (short)VisRowIndices.visRowPrintProperties,
// (short)VisCellIndices.visPrintPropertiesPageOrientation);
//layoutCell.set_Result(
// VisUnitCodes.visPageUnits,
// (short)VisCellVals.visPPOLandscape);
// curved connector lines
layoutCell = this._page.PageSheet.get_CellsSRC(
(short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowPageLayout,
(short)VisCellIndices.visPLOLineRouteExt);
layoutCell.set_Result(
VisUnitCodes.visPageUnits,
(short)VisCellVals.visLORouteExtNURBS);
// perform the layout
this._page.Layout();
// optionally resize the page to fit the space taken by its shapes
this._page.ResizeToFitContents();
//
Changing Connector Line Colors
If you're unfamiliar with how formulas for colors work, this might also be very frustrating. By default you can give an int as a string to get pre-defined colors, but this isn't very helpful because there isn't an easy way to figure out what each of those colors are. (There is a Page.Colors collection, but you have to inspect each of their RGB values and figure out the color from them.)
Instead, you can use your own RGB values for the formula.
private void SetConnectorLineColor(Shape connector, string colorFormula)
{
var cell = connector.get_Cells("LineColor");
cell.Formula = colorFormula;
}
internal static class AnswerColorFormula
{
public static string Green = "RGB(0,200,0)";
public static string Orange = "RGB(255,100,0)";
public static string Yellow = "RGB(255,200,0)";
public static string Red = "RGB(255,5,5)";
}
Call the Layout method on the Page object. If there are shapes selected on this page then this method will only operate on the current selection. You may want to call DeselectAll on the ActiveWindow first.

Aspose.Words...calculating widt of text in pixels

I have an MVC3 C# .Net web app. I am using Aspose.Words to create an MS Word document. I have a requirement to not include tables in the document. However, on several lines of the document the alignment of the text is mis-aligned depending on the width of the text.
For example:
This looks good
Proposal Name: My Proposal Date:04/24/2012
This does not
Proposal Name: My Prop Date:04/24/2012
It should be
Proposal Name: My Prop Date:04/24/2012
Based on the width of the first bit of text, I need to calculate the width in pixels (I think) and insert a TAB if necessary.
Any ideas how to do this?
you can use Graphics.MeasureString function which gives you the width of your string in pixels based on your font. for more info go Here
Cheers,
Ehsan
The following code example returns the bounding rectangle of the current entity relative to the page top left corner.
Document doc = new Document(MyDir + "in.docx");
LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
var renderObject = layoutCollector.GetEntity(para);
layoutEnumerator.Current = renderObject;
RectangleF location = layoutEnumerator.Rectangle;
Console.WriteLine(location);
}
src: https://www.aspose.com/community/forums/thread/541215/replace-run-text-with-string-of-spaces-of-same-pixel-length.aspx

Resources