locked
how to use images in word templates in SharePoint 2010 RRS feed

  • Question

  • Hi,

    I am using word template attached to a content type. I have few site columns that I am placing in my template and generating document based on template using C# code. The problem is with the image columns. I need to insert images in the word document that are attached to a sharepoint list. When I define a site column of type Hyperlink Or Picture and attach this column to content type. When I try to arrange the image in word template the way I did for other columns it is not visible in quick parts. But, I see when I try to create a document directly from document library. Any ideas? how to solve this

    Thanks,

    Wednesday, October 31, 2012 5:48 PM

Answers

All replies

  • Hi AlexMathew,

    I think you need not use site column to do it. When you create document you can insert a picture in event receiver .

    You can refer to the following C# code.

    http://msdn.microsoft.com/en-us/library/office/bb497430.aspx

    Thanks,

    Jack

    • Proposed as answer by AliYasir Monday, November 5, 2012 4:10 AM
    Monday, November 5, 2012 3:16 AM
  • Ok. I have multiple images to choose according to the business scenario. With out using open XML can I achieve this using my approach? Or Can I combine both the approaches to insert an image to a document and process remaining using site columns(text fieldds)? Appreciate your reply.

    Thanks,

    Tuesday, November 6, 2012 3:21 PM
  • Hi AlexMathew,

    I am not sure why you don’t want to use XML. In fact, you should only provide the picture path is enough.

    string document = @"C:\Users\Public\Documents\Word9.docx";
    string fileName = @"C:\Users\Public\Documents\MyPic.jpg";
    InsertAPicture(document, fileName);
    publicstaticvoid InsertAPicture(string document, string fileName)
    {
        using (WordprocessingDocument wordprocessingDocument = 
            WordprocessingDocument.Open(document, true))
        {
            MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
            ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                imagePart.FeedData(stream);
            }
            AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
        }
    }
    privatestaticvoid AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
    {
        // Define the reference of the image.var element =
             new Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = 990000L, Cy = 792000L },
                     new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, 
                         RightEdge = 0L, BottomEdge = 0L },
                     new DW.DocProperties() { Id = (UInt32Value)1U, 
                         Name = "Picture 1" },
                     new DW.NonVisualGraphicFrameDrawingProperties(
                         new A.GraphicFrameLocks() { NoChangeAspect = true }),
                     new A.Graphic(
                         new A.GraphicData(
                             new PIC.Picture(
                                 new PIC.NonVisualPictureProperties(
                                     new PIC.NonVisualDrawingProperties() 
                                        { Id = (UInt32Value)0U, 
                                            Name = "New Bitmap Image.jpg" },
                                     new PIC.NonVisualPictureDrawingProperties()),
                                 new PIC.BlipFill(
                                     new A.Blip(
                                         new A.BlipExtensionList(
                                             new A.BlipExtension() 
                                                { Uri = 
                                                    "{28A0092B-C50C-407E-A947-70E740481C1C}" })
                                     ) 
                                     { Embed = relationshipId, 
                                         CompressionState = 
                                         A.BlipCompressionValues.Print },
                                     new A.Stretch(
                                         new A.FillRectangle())),
                                 new PIC.ShapeProperties(
                                     new A.Transform2D(
                                         new A.Offset() { X = 0L, Y = 0L },
                                         new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                     new A.PresetGeometry(
                                         new A.AdjustValueList()
                                     ) { Preset = A.ShapeTypeValues.Rectangle }))
                         ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                 ) { DistanceFromTop = (UInt32Value)0U, 
                     DistanceFromBottom = (UInt32Value)0U, 
                     DistanceFromLeft = (UInt32Value)0U, 
                     DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" });
       // Append the reference to body, the element should be in a Run.
       wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
    }

    Thanks,

    Jack

    Wednesday, November 7, 2012 1:49 AM
  • Hello Jack,

    Thanks for your reply!

    You can not use "WordprocessingDocument" code in event receiver. Correct me if I am wrong. I already tried using but unable to hit debug in eventreceiver. Appreciate your help!

    Thanks,

    Wednesday, November 7, 2012 2:25 PM
  • Hi AlexMathew,

    Open XML SDK 2.0 for Microsoft Office is an external component. So, first you should download and install it.  Before you install it, you should close your visual studio 2008 or 2010.

    Download : http://www.microsoft.com/en-us/download/details.aspx?id=5124

    Then Add a dll reference like the following snapshot.

    You can refer to the following link.

    http://blogs.msdn.com/b/ericwhite/archive/2007/12/20/what-is-the-difference-between-the-system-io-packaging-and-microsoft-office-documentformat-openxml-packaging-namespaces.aspx

    Thanks,

    Jack

    • Marked as answer by star.wars Friday, November 9, 2012 7:11 AM
    Thursday, November 8, 2012 2:36 AM