none
open XML SDK 2.0 in VS 2008 ... watermark for docx RRS feed

  • Question

  • Hi,

    i just want to add a watermark to a docx document, using the open XML SDK 2.0 in Visual Studio 2008. but i didn't succeed ... any ideas, guys ?
    my doc is a dynamic one, generated by a webserver, on demand.

    thanks
    Monday, December 14, 2009 3:56 PM

All replies

  • Thanks for your question, agrothic!

    Have you tried the Open XML Productivity Tool released along with Open XML SDK 2.0. December CTP?
    For your scenario, you would follow the steps to get sample code for adding watermark:
    1. Install Open XML SDK 2.0 Dec CTP and its productivity tool
    2. Create an empty docx document and save it, such as source.docx
    3. Add a watermark to the empty docx and save it as target.docx
    4. Open the productivity tool, click "Compare Files" to compare the source.docx and target.docx files
    5. In the "File Comparison" tab, click "View/Hide Code"

    And then you would see the generated code which shows you how to add a watermark.
    Tuesday, December 15, 2009 3:54 AM
    Moderator
  • Hi Ethan,

    i didn't noticed the update of the SDK, less than one week ago. thank you !

    by the way, you're suggestion will show me the open XML generated code, but i want to add the watermark in a .Net program (on the fly), using the SDK API.

    plus ... i don't have word2007 on my computer ;) so i can't do that.

    Nico.
    • Edited by agrotic Tuesday, December 15, 2009 8:21 AM
    Tuesday, December 15, 2009 8:00 AM
  • Please have a try with the latest release, and hope it will help you with your work.

    The tool has two feature which are "Reflected Code" and generated code from differences between two documents. They would generate sample code with which you could learn how to write code with Open XML SDK for sepecific scenarios. And I think they would be useful for adding watermark.

    If you still have problems about it, please provide more detailed information about what you're dealing with, and I might help you.

    Tuesday, December 15, 2009 8:12 AM
    Moderator
  • OK, thanks.

    i just downloaded the latest SDK and installed it.

    i'll try to have a "watermaked" doc without having word 2007 installed ;)

    Nico.

    [update] i watermarked my doc with office 2003 and saved as docx
    • Edited by agrotic Tuesday, December 15, 2009 2:46 PM
    Tuesday, December 15, 2009 9:08 AM
  • OK guys.

    i've watermarked a doc, reflected the code, paste it into VS 2008 and generated a new doc...
    but it doesn't have the watermark. so, i used the SDK tool to compare the 2 docx. some strange differences : "t" became "true"...

    let me show you : i get this ...
         <v:path textpathok="true" o:connecttype="custom" o:connectlocs="@9,0;@10,10800;@11,21600;@12,10800" o:connectangles="270,180,90,0"/>
         <v:textpath on="true" fitshape="true"/>
         <v:handles>
          <v:h position="#0,bottomRight" xrange="6629,14971"/>
         </v:handles>
         <o:lock v:ext="edit" text="true" shapetype="true"/>
        </v:shapetype>
        <v:shape id="PowerPlusWaterMarkObject3" style="position:absolute;margin-left:0;margin-top:0;width:519.55pt;height:119.9pt;rotation:315;z-index:-251657728;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" o:spid="_x0000_s2050" o:allowincell="false" fillcolor="black" stroked="false" type="#_x0000_t136">
         <v:fill opacity=".5"/>
         <v:textpath style="font-family:"Calibri";font-size:1pt" string="NE PAS DIFFUSER"/>
        </v:shape>
    
    but i want this ...
         
         <v:path textpathok="t" o:connecttype="custom" o:connectlocs="@9,0;@10,10800;@11,21600;@12,10800" o:connectangles="270,180,90,0"/>
         <v:textpath on="t" fitshape="t"/>
         <v:handles>
          <v:h position="#0,bottomRight" xrange="6629,14971"/>
         </v:handles>
         <o:lock v:ext="edit" text="t" shapetype="t"/>
        </v:shapetype>
        <v:shape id="PowerPlusWaterMarkObject2" style="position:absolute;margin-left:0;margin-top:0;width:519.55pt;height:119.9pt;rotation:315;z-index:-251658752;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" o:spid="_x0000_s2049" o:allowincell="f" fillcolor="black" stroked="f" type="#_x0000_t136">
         <v:fill opacity=".5"/>
         <v:textpath style="font-family:"Calibri";font-size:1pt" string="NE PAS DIFFUSER"/>
        </v:shape>
    

    is that why i don't get the watermark ? sounds strange to me...
    Tuesday, December 15, 2009 10:32 AM
  • that must be more complex than that ...

    3 different headers in my docx with watermark... and the watermark text in each header (???) really strange !
    Tuesday, December 15, 2009 2:45 PM
  • I believe you can take a look at the Open XML Power tools: http://www.codeplex.com/PowerTools, which should contain libraries that you can reuse. One of the reusable pieces of code is to insert watermarks into a document. That approach may help you out here.
    Zeyad Rajabi (MS)
    Thursday, December 17, 2009 6:49 PM
  • OK, I saw that but the PowerTools are design for SDK 1.1, not for SDK 2.0
    So I don't know if that will work or not and if my dev will be put into garbage in few weeks. so, I hesitate ...
    Monday, December 21, 2009 8:20 AM
  • "t" and "true" are both valida in Open XML, and they mean logical "true".
    Since then you already have the tool and have a doc with watermark. You can try to compile and run the reflected code.
    I believe the code could tell you how to add a watermark to a document.
    If you still have questions, you could paste the code that you don't understand. We can do analysis on the code.
    Wednesday, January 20, 2010 2:16 AM
    Moderator
  • Hi,

    I have put down the code below, use it to Add WaterMark in you document.

    Pre-requisite : Document must have a header, if not we can change the Code accordingly to add Header,

    public

     

    static void AddWaterMark(WordprocessingDocument doc)

    {

     

    foreach (HeaderPart headerPart in doc.MainDocumentPart.HeaderParts)

    {

     

    SdtBlock sdtBlock1 = new SdtBlock();

     

    SdtProperties sdtProperties1 = new SdtProperties();

     

    SdtId sdtId1 = new SdtId() { Val = 87908844 };

     

    SdtContentDocPartObject sdtContentDocPartObject1 = new SdtContentDocPartObject();

     

    DocPartGallery docPartGallery1 = new DocPartGallery() { Val = "Watermarks" };

     

    DocPartUnique docPartUnique1 = new DocPartUnique();

    sdtContentDocPartObject1.Append(docPartGallery1);

    sdtContentDocPartObject1.Append(docPartUnique1);

    sdtProperties1.Append(sdtId1);

    sdtProperties1.Append(sdtContentDocPartObject1);

     

    SdtContentBlock sdtContentBlock1 = new SdtContentBlock();

     

    Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00656E18", RsidRunAdditionDefault = "00656E18" };

     

    ParagraphProperties paragraphProperties2 = new ParagraphProperties();

     

    ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Header" };

    paragraphProperties2.Append(paragraphStyleId2);

     

    Run run1 = new Run();

     

    RunProperties runProperties1 = new RunProperties();

     

    NoProof noProof1 = new NoProof();

     

    Languages languages1 = new Languages() { EastAsia = "zh-TW" };

    runProperties1.Append(noProof1);

    runProperties1.Append(languages1);

     

    Picture picture1 = new Picture();

    V.

    Shapetype shapetype1 = new V.Shapetype() { Id = "_x0000_t136", CoordinateSize = "21600,21600", OptionalNumber = 136, Adjustment = "10800", EdgePath = "m@7,l@8,m@5,21600l@6,21600e" };

    V.

    Formulas formulas1 = new V.Formulas();

    V.

    Formula formula1 = new V.Formula() { Equation = "sum #0 0 10800" };

    V.

    Formula formula2 = new V.Formula() { Equation = "prod #0 2 1" };

    V.

    Formula formula3 = new V.Formula() { Equation = "sum 21600 0 @1" };

    V.

    Formula formula4 = new V.Formula() { Equation = "sum 0 0 @2" };

    V.

    Formula formula5 = new V.Formula() { Equation = "sum 21600 0 @3" };

    V.

    Formula formula6 = new V.Formula() { Equation = "if @0 @3 0" };

    V.

    Formula formula7 = new V.Formula() { Equation = "if @0 21600 @1" };

    V.

    Formula formula8 = new V.Formula() { Equation = "if @0 0 @2" };

    V.

    Formula formula9 = new V.Formula() { Equation = "if @0 @4 21600" };

    V.

    Formula formula10 = new V.Formula() { Equation = "mid @5 @6" };

    V.

    Formula formula11 = new V.Formula() { Equation = "mid @8 @5" };

    V.

    Formula formula12 = new V.Formula() { Equation = "mid @7 @8" };

    V.

    Formula formula13 = new V.Formula() { Equation = "mid @6 @7" };

    V.

    Formula formula14 = new V.Formula() { Equation = "sum @6 0 @5" };

    formulas1.Append(formula1);

    formulas1.Append(formula2);

    formulas1.Append(formula3);

    formulas1.Append(formula4);

    formulas1.Append(formula5);

    formulas1.Append(formula6);

    formulas1.Append(formula7);

    formulas1.Append(formula8);

    formulas1.Append(formula9);

    formulas1.Append(formula10);

    formulas1.Append(formula11);

    formulas1.Append(formula12);

    formulas1.Append(formula13);

    formulas1.Append(formula14);

    V.

    Path path1 = new V.Path() { AllowTextPath = true, ConnectionPointType = Ovml.ConnectValues.Custom, ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", ConnectAngles = "270,180,90,0" };

    V.

    TextPath textPath1 = new V.TextPath() { On = true, FitShape = true };

    V.

    ShapeHandles shapeHandles1 = new V.ShapeHandles();

    V.

    ShapeHandle shapeHandle1 = new V.ShapeHandle() { Position = "#0,bottomRight", XRange = "6629,14971" };

    shapeHandles1.Append(shapeHandle1);

    Ovml.

    Lock lock1 = new Ovml.Lock() { Extension = V.ExtensionHandlingBehaviorValues.Edit, TextLock = true, ShapeType = true };

    shapetype1.Append(formulas1);

    shapetype1.Append(path1);

    shapetype1.Append(textPath1);

    shapetype1.Append(shapeHandles1);

    shapetype1.Append(lock1);

    V.

    Shape shape1 = new V.Shape() { Id = "PowerPlusWaterMarkObject357476642", Style = "position:absolute;left:0;text-align:left;margin-left:0;margin-top:0;width:527.85pt;height:131.95pt;rotation:315;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin", OptionalString = "_x0000_s2049", AllowInCell = false, FillColor = "silver", Stroked = false, Type = "#_x0000_t136" };

    V.

    Fill fill1 = new V.Fill() { Opacity = ".5" };

    V.

    TextPath textPath2 = new V.TextPath() { Style = "font-family:\"Calibri\";font-size:1pt", String = "CONFIDENTIAL" };

    Wvml.

    TextWrap textWrap1 = new Wvml.TextWrap() { AnchorX = Wvml.HorizontalAnchorValues.Margin, AnchorY = Wvml.VerticalAnchorValues.Margin };

    shape1.Append(fill1);

    shape1.Append(textPath2);

    shape1.Append(textWrap1);

    picture1.Append(shapetype1);

    picture1.Append(shape1);

    run1.Append(runProperties1);

    run1.Append(picture1);

    paragraph2.Append(paragraphProperties2);

    paragraph2.Append(run1);

    sdtContentBlock1.Append(paragraph2);

    sdtBlock1.Append(sdtProperties1);

    sdtBlock1.Append(sdtContentBlock1);

    headerPart.Header.Append(sdtBlock1);

     

    //break;

    }

    }

    Hope this helps.

     

     

    Tuesday, June 15, 2010 7:14 AM
  • Please tell how to add header ...I m very new to OpenXml.
    Tuesday, July 5, 2011 9:21 AM
  • @Hitendra_MSDN Your code works excellent if a Header already exists (as long as you add the: headerPart.Header.Save(); after your last line).  Thanks!  How do you go about creating a header if it doesn't exist?  I can create the header just fine, but it seems like when I add the reference to the header the document can no longer be opened because it's corrupt.

     

    this is my header adding code:

    if (doc.MainDocumentPart.HeaderParts.Count() == 0)

                {

                    doc.MainDocumentPart.DeleteParts(doc.MainDocumentPart.HeaderParts);

                    var newHeaderPart = doc.MainDocumentPart.AddNewPart<HeaderPart>();

                    var rId = doc.MainDocumentPart.GetIdOfPart(newHeaderPart);

                    var headerRef = new HeaderReference();

                    headerRef.Id = rId;

                    var sectionProps = doc.MainDocumentPart.Document.Body.Elements<SectionProperties>().LastOrDefault();

                    if (sectionProps == null)

                    {

                        sectionProps = new SectionProperties();

                        doc.MainDocumentPart.Document.Body.Append(sectionProps);

                    }

                    sectionProps.RemoveAllChildren<HeaderReference>();

                    sectionProps.Append(headerRef);

                var header = new Header();

                var paragraph = new Paragraph();

                var run = new Run();

                var text = new Text();

                text.Text = "";

                run.Append(text);

                paragraph.Append(run);

                header.Append(paragraph);

                newHeaderPart.Header = header;

                newHeaderPart.Header.Save();

                }

    Tuesday, August 9, 2011 7:43 PM
  • I have just done this and I have the code but I dont knwo where I am supposed to put it.

    Monday, March 12, 2012 1:57 PM