Feeds:
Posts
Comments

Posts Tagged ‘ooxml’

I need to create a presentation with quite recurring contents. So first created a template presentation using Powerpoint. Then I tried to unpack and modify with XSL. I did this with word before, and was quite happy. But for PPTX you have to create two new files per slide. That ended in a mess.

That brought me to the Open Xml SDK 2.0. Still a mess, but it is a little better.

I want to clone a template slide multiple times, and then “inject” my contents into the predefined shapes. That should be easy.

But it took me a while to find out how to correctly clone a slide. So I thought I’d share that with you.

var presentationFile = "SomeFile.pptx";
using (PresentationDocument presentationDocument 
    = PresentationDocument.Open(presentationFile, true))
{
    var presentationPart = presentationDocument.PresentationPart;
    var templatePart = presentationPart.GetSlidePartsInOrder().Last();

    var newSlidePart = templatePart.CloneSlide();

    presentationPart.AppendSlide(newSlidePart);
}

The real code…

… is in this extensions. I created something to find slide parts in the right order, because the order of slide parts stored can be different to the order they appear in. Then I created a utility that clones a slide, and another one that appends the new slide part to the presentation part, which will finally show it.

public static class OpenXmlUtils
{
    public static IEnumerable<SlidePart> GetSlidePartsInOrder(this PresentationPart presentationPart)
    {
        SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

        return slideIdList.ChildElements
            .Cast<SlideId>()
            .Select(x => presentationPart.GetPartById(x.RelationshipId))
            .Cast<SlidePart>();
    }

    public static SlidePart CloneSlide(this SlidePart templatePart)
    {
        // find the presentationPart: makes the API more fluent
        var presentationPart = templatePart.GetParentParts()
            .OfType<PresentationPart>()
            .Single();

        // clone slide contents
        Slide currentSlide = (Slide)templatePart.Slide.CloneNode(true);
        var slidePartClone = presentationPart.AddNewPart<SlidePart>();
        currentSlide.Save(slidePartClone);

        // copy layout part
        slidePartClone.AddPart(templatePart.SlideLayoutPart);

        return slidePartClone;
    }

    public static void AppendSlide(this PresentationPart presentationPart, SlidePart newSlidePart)
    {
        SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

        // find the highest id
        uint maxSlideId = slideIdList.ChildElements
            .Cast<SlideId>()
            .Max(x => x.Id.Value);

        // Insert the new slide into the slide list after the previous slide.
        var id = maxSlideId + 1;

        SlideId newSlideId = new SlideId();
        slideIdList.Append(newSlideId);
        newSlideId.Id = id;
        newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);
    }
}

Read Full Post »

Since both converting all those strange ooxml units manually and searching the OOXML specification is not possible anywhere else, I created a dashboard for OOXML Development. I’ll add stuff to it whenever I feel for it.

Open the OOXML Developer Dashboard.

Read Full Post »

After I got confused with the Office Open XML (OOXML) measuring units for a couple of hours, I thought I’d share what I found on it.

I order to avoid floating point calculations and still maintain high precision the format uses some odd measurement units. Don’t think you can get away with inches, centimeters or pixels!

UPDATE: I created a conversion tool for all those units, available here.

Twentieths of a point (dxa)

The main unit in OOXML is a twentieth of a point. This is used for specifying page dimensions, margins, tabs, etc.

The international default letter size is ISO 216 A4 (210x297mm ~ 8.3×11.7in) and expressed as this:

// pageSize: with and height in 20th of a point
<w:pgSz w:w="11906" w:h="16838"/>

Calculations

  20th of a Point Points

(
dxa/20)
Inches

(pt*72)
Centimeters

(
in*2,54)
Width 11906 595.3 8,27… 21.00086…
Height 16838 841.9 11.69… 29.70036…

As you see it doesn’t really come out even, but even enough. As you can see here, word processes files at 72dpi.

Half-points

Half-points are used to specify font sizes. A font-size of 12pt equals 24 half points:

// run properties
<w:rPr>
  // size value in half-points
  <w:sz w:val="24"/>
</w:rPr>

Fiftieths of a Percent

This is used for relative measurements in some places. It can for example be used for specifying tables total with, cell with and margins.

<w:tbl>
    <w:tblPr>
      <!-- table width in 50th of a percent -->
      <w:tblW w:w="2500" w:type="pct"/>
    </w:tblPr>
    <w:tblGrid/>
    <w:tr>
        <w:tc>
            <w:p>
                <w:r>
                    <w:t>Hello, World!</w:t>
                </w:r>
            </w:p>
        </w:tc>
    </w:tr>
</w:tbl>

This prints a table that takes 50% of the available with. If you want to specify the with in twentieth of points instead, you have to use specify w:type=”dxa”.

Calculation

0.5 * 5000 = 2500pct

or

(50*50)%

EMUs (English Metric Unit)

EMUs are used for coordinates in vector-based drawings and embedded pictures. The EMU is a virtual unit to bridge both centimeters and inches. One inch equates to 914400 EMUs and a centimeter is 360000. Actually I found out that the number 914400 is calculated by (the least common multiple of 100 and 254) times 72. As I understand it, this ensures that you can convert forth and back between integer 100th inches, millimeters and pixels with out any floating points. Is that correct, anybody?

Since OOXML is too verbose, the full markup would just confuse even more. Lets say we have a picture that we want to fit into a table cell.

The cell with is 4,25cm which equals to 2410 dxa.

<w:tcW w:w="2410" w:type="dxa"/>

The original picture is 295x413px at 72dpi and is embedded using DrawingML. The target picture size in the document has to be specified twice. Once for the drawing canvas (extent) and a second time for the embedded picture itself.

When you try to draw this in word itself you’ll have a hard time to match the actual cell size. This version is set by manually calculating the target picture size.

<w:drawing>
    <wp:anchor ...>
        ...
        <!-- drawing canvas size -->
        <wp:extent cx="1530350" cy="2142490"/>
        ...
        <a:graphic>
            <a:graphicData>
                <pic:pic>
                    ...
                    <!-- shape properties -->
                    <pic:spPr bwMode="auto">
                        <!-- 2D transform -->
                        <a:xfrm>
                            ...
                            <!-- picture size -->
                            <a:ext cx="1530350" cy="2142490"/>
                        </a:xfrm>
                        ...
                    </pic:spPr>
                </pic:pic>
            </a:graphicData>
        </a:graphic>
    </wp:anchor>
</w:drawing>

Calculations

  20th of a Point Points

(
dxa/20)
Inches

(pt/72)
EMU

(in*914400)
Cell Width 2410 120.5 1,67361… 1530350

Even though this is the most logical way to calculate it, it involves floating points. You can simply avoid those by calculating:

2410 * 914400 / 72 / 20 = 1530350

which can be simplified to:

2410 * 635 = 1530350

Since we want to maintain the aspect ratio of the original picture at 295:413 we have to calculate the height for the cell in dxas and for the pictures in EMUs.

The width-to-height-ratio for this picture is 1.4. So the height for the cell is 2410*1.4 = 3374 dxa and 3374*635 = 2142490 emu.

Thanks for reading! I hope this helps.

Read Full Post »

Follow

Get every new post delivered to your Inbox.

Join 36 other followers