none
How to copy an image from a docx to pptx using C# and Open XML SDK 2.0 ? RRS feed

  • Question

  • Hie,

    i would like to know if it's possible to copy an image from a docx file to a pptx presentation using C# and Open XML SDK 2.0, and if there is some sample code ?

    Thanks.

    Monday, October 25, 2010 6:20 AM

Answers

  • Hi,

    I think that should not be any problem.

    You need to:

    1. Open Docx
    2. Get to the image
    3. Get the stream of the image part
    4. Insert an image part in PowerPoint
    5. Pass the stream from step 3 into this part

    For most of the points, youu should be able to use OpenXMLSDk to get the code and use it.

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Marked as answer by Ankush_Bhatia Thursday, October 28, 2010 5:41 PM
    Thursday, October 28, 2010 5:41 PM
  • and here is the sample code

     

     

     

    public static SlidePart GetFirstSlide(PresentationDocument presentationDocument)

    {

     

     

    // Get relationship ID of the first slide

     

     

    PresentationPart part = presentationDocument.PresentationPart;

     

     

    SlideId slideId = part.Presentation.SlideIdList.GetFirstChild<SlideId>();

     

     

    string relId = slideId.RelationshipId;

     

     

    // Get the slide part by the relationship ID.

     

     

    SlidePart slidePart = (SlidePart)part.GetPartById(relId);

     

     

    return slidePart;

    }

     

     

    private void CopyImage(){

     

     

    using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(@"sample.docx", false))

    {

     

     

    ImagePart imgPart = wdDoc.MainDocumentPart.ImageParts.First();

     

     

    using (PresentationDocument presDoc = PresentationDocument.Open(@"sample.pptx", true))

    {

     

     

    // Get relationship ID of the first slide

     

     

    PresentationPart part = presDoc.PresentationPart;

     

     

    SlideId slideId = part.Presentation.SlideIdList.GetFirstChild<SlideId>();

     

     

    string relId = slideId.RelationshipId;

     

     

    // Get the slide part by the relationship ID.

     

     

    SlidePart slidePart = (SlidePart)part.GetPartById(relId);

     

     

    ImagePart imagePart1 = slidePart.AddImagePart(ImagePartType.Jpeg,"Test");

     

     

    ShapeTree spTree=slidePart.Slide.CommonSlideData.ShapeTree;

     

    imagePart1.FeedData(imgPart.GetStream());

     

     

    Picture pict = GeneratePicture("Test");

    spTree.Append(pict);

    part.Presentation.Save();

    }

    }

    }

     

     

    // Creates an Picture instance and adds its children.

     

     

    public Picture GeneratePicture(string RelationShipID)

    {

     

     

    Picture picture1 = new Picture();

     

     

    NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();

     

     

    NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 3" };

     

     

    NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();

    A.

     

    PictureLocks pictureLocks1 = new A.PictureLocks() { NoChangeAspect = true };

    nonVisualPictureDrawingProperties1.Append(pictureLocks1);

     

     

    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

    nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);

    nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

    nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties1);

     

     

     

    BlipFill blipFill1 = new BlipFill();

    A.

     

    Blip blip1 = new A.Blip() { Embed = RelationShipID };

    A.

     

    BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

    A.

     

    BlipExtension blipExtension1 = new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };

    A14.

     

    UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi() { Val = false };

    useLocalDpi1.AddNamespaceDeclaration(

     

    "a14", "http://schemas.microsoft.com/office/drawing/2010/main");

    blipExtension1.Append(useLocalDpi1);

    blipExtensionList1.Append(blipExtension1);

    blip1.Append(blipExtensionList1);

     

     

    ShapeProperties shapeProperties1 = new ShapeProperties();

    A.

     

    Transform2D transform2D1 = new A.Transform2D();

    A.

     

    Offset offset1 = new A.Offset() { X = 0L, Y = 0L };

    A.

     

    Extents extents1 = new A.Extents() { Cx = 9144000L, Cy = 6858000L };

    transform2D1.Append(offset1);

    transform2D1.Append(extents1);

    A.

     

    PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };

    A.

     

    AdjustValueList adjustValueList1 = new A.AdjustValueList();

    presetGeometry1.Append(adjustValueList1);

    shapeProperties1.Append(transform2D1);

    shapeProperties1.Append(presetGeometry1);

    picture1.Append(nonVisualPictureProperties1);

    A.

     

    Stretch stretch1 = new A.Stretch();

    A.

     

    FillRectangle fillRectangle1 = new A.FillRectangle();

    stretch1.Append(fillRectangle1);

    blipFill1.Append(blip1);

    blipFill1.Append(stretch1);

    picture1.Append(blipFill1);

     

    picture1.Append(shapeProperties1);

     

     

    return picture1;

    }

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Marked as answer by Ankush_Bhatia Friday, October 29, 2010 12:29 PM
    Friday, October 29, 2010 12:28 PM

All replies

  • Hi,

    I think that should not be any problem.

    You need to:

    1. Open Docx
    2. Get to the image
    3. Get the stream of the image part
    4. Insert an image part in PowerPoint
    5. Pass the stream from step 3 into this part

    For most of the points, youu should be able to use OpenXMLSDk to get the code and use it.

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Marked as answer by Ankush_Bhatia Thursday, October 28, 2010 5:41 PM
    Thursday, October 28, 2010 5:41 PM
  • and here is the sample code

     

     

     

    public static SlidePart GetFirstSlide(PresentationDocument presentationDocument)

    {

     

     

    // Get relationship ID of the first slide

     

     

    PresentationPart part = presentationDocument.PresentationPart;

     

     

    SlideId slideId = part.Presentation.SlideIdList.GetFirstChild<SlideId>();

     

     

    string relId = slideId.RelationshipId;

     

     

    // Get the slide part by the relationship ID.

     

     

    SlidePart slidePart = (SlidePart)part.GetPartById(relId);

     

     

    return slidePart;

    }

     

     

    private void CopyImage(){

     

     

    using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(@"sample.docx", false))

    {

     

     

    ImagePart imgPart = wdDoc.MainDocumentPart.ImageParts.First();

     

     

    using (PresentationDocument presDoc = PresentationDocument.Open(@"sample.pptx", true))

    {

     

     

    // Get relationship ID of the first slide

     

     

    PresentationPart part = presDoc.PresentationPart;

     

     

    SlideId slideId = part.Presentation.SlideIdList.GetFirstChild<SlideId>();

     

     

    string relId = slideId.RelationshipId;

     

     

    // Get the slide part by the relationship ID.

     

     

    SlidePart slidePart = (SlidePart)part.GetPartById(relId);

     

     

    ImagePart imagePart1 = slidePart.AddImagePart(ImagePartType.Jpeg,"Test");

     

     

    ShapeTree spTree=slidePart.Slide.CommonSlideData.ShapeTree;

     

    imagePart1.FeedData(imgPart.GetStream());

     

     

    Picture pict = GeneratePicture("Test");

    spTree.Append(pict);

    part.Presentation.Save();

    }

    }

    }

     

     

    // Creates an Picture instance and adds its children.

     

     

    public Picture GeneratePicture(string RelationShipID)

    {

     

     

    Picture picture1 = new Picture();

     

     

    NonVisualPictureProperties nonVisualPictureProperties1 = new NonVisualPictureProperties();

     

     

    NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Picture 3" };

     

     

    NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new NonVisualPictureDrawingProperties();

    A.

     

    PictureLocks pictureLocks1 = new A.PictureLocks() { NoChangeAspect = true };

    nonVisualPictureDrawingProperties1.Append(pictureLocks1);

     

     

    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

    nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);

    nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

    nonVisualPictureProperties1.Append(applicationNonVisualDrawingProperties1);

     

     

     

    BlipFill blipFill1 = new BlipFill();

    A.

     

    Blip blip1 = new A.Blip() { Embed = RelationShipID };

    A.

     

    BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

    A.

     

    BlipExtension blipExtension1 = new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };

    A14.

     

    UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi() { Val = false };

    useLocalDpi1.AddNamespaceDeclaration(

     

    "a14", "http://schemas.microsoft.com/office/drawing/2010/main");

    blipExtension1.Append(useLocalDpi1);

    blipExtensionList1.Append(blipExtension1);

    blip1.Append(blipExtensionList1);

     

     

    ShapeProperties shapeProperties1 = new ShapeProperties();

    A.

     

    Transform2D transform2D1 = new A.Transform2D();

    A.

     

    Offset offset1 = new A.Offset() { X = 0L, Y = 0L };

    A.

     

    Extents extents1 = new A.Extents() { Cx = 9144000L, Cy = 6858000L };

    transform2D1.Append(offset1);

    transform2D1.Append(extents1);

    A.

     

    PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Rectangle };

    A.

     

    AdjustValueList adjustValueList1 = new A.AdjustValueList();

    presetGeometry1.Append(adjustValueList1);

    shapeProperties1.Append(transform2D1);

    shapeProperties1.Append(presetGeometry1);

    picture1.Append(nonVisualPictureProperties1);

    A.

     

    Stretch stretch1 = new A.Stretch();

    A.

     

    FillRectangle fillRectangle1 = new A.FillRectangle();

    stretch1.Append(fillRectangle1);

    blipFill1.Append(blip1);

    blipFill1.Append(stretch1);

    picture1.Append(blipFill1);

     

    picture1.Append(shapeProperties1);

     

     

    return picture1;

    }

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Marked as answer by Ankush_Bhatia Friday, October 29, 2010 12:29 PM
    Friday, October 29, 2010 12:28 PM
  • uh, what is A14 referencing?

    i'm guessing you're using the 2010-only drawing class.  Is there a way to do it with 2007?

    Monday, March 28, 2011 5:50 PM