wordpress hit counter
How to correctly insert JPEG images ? - WordprocessingML - Formats - OpenXML Developer

How to correctly insert JPEG images ?

Formats

Discussions about working with different Open XML Formats

How to correctly insert JPEG images ?

  • rated by 0 users
  • This post has 5 Replies |
  • 0 Followers
  • I'm not using the SDK, rather using C++ to create and construct the docx content.
    So far evrythings works when inserting images of type other than JPEG : gif, png, etc : I insert the image in the package, create and point to a reference in the document relationship file.
    This also works for JPEG images unless I specify dimensions in the xml drawing tag other than the real ones ...
    When the dimensions are not the same, the document is corrupt and after MSWord2007 corrects it, I note that the JPEG picture real dimensions are altered to meet those specified in documents.xml.
    Any comment ?
  • Hi

    The dimension should be given in EMU.  You can covert pixel value into EMU and set the value. For refernce 1 pixel = 9525 EMU. I am giving an example in c#.

    int widthInEMUs = Width * 9525;

    int heightInEMUs = Height * 9525;

    And the xml like

    <w:drawing>

    <wp:inline>

    <wp:extent cx=\"{WIDTH-IN-EMUS}\" cy=\"{HEIGHT-IN-EMUS}\" /> .......

    Thanks
    Prosanta

  • Hi 

            How to insert image in the Microsoft Word Document by OPEN XML SDK 2.0 Using C#?

    Can I get the  example in c#. My email is zrc6666@163.com!

            Thank you !

  • Hi

    You can use this example.

    const String constdrawingTemplateParaStart = "";


    const String constdrawingTemplate = "
    + " xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
    + " xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" "
    + " xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" "
    + " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    +""
    + ""
    + ""
    + ""
    + ""
    +""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    + ""
    +""
    +""
    +""
    +" "
    +"";

    -->

    Rich Text Editor Issue.... Complete example attached.

    private void InsertImagePart(String nodeValue, XmlNode oNode, int Width, int Height)
    {
    string imageFileName = new FileInfo(nodeValue).Name;
    string imageType = new FileInfo(nodeValue).Extension.ToLower();
    //insert image
    // Create an image part and add it to the document.
    ImagePartType oPartType = ImagePartType.Jpeg;

    if (imageType.Equals(".bmp"))
    oPartType = ImagePartType.Bmp;
    else if (imageType.Equals(".emf"))
    oPartType = ImagePartType.Emf;
    else if (imageType.Equals(".gif"))
    oPartType = ImagePartType.Gif;
    else if (imageType.Equals(".Icon"))
    oPartType = ImagePartType.Icon;
    else if (imageType.Equals(".jpeg"))
    oPartType = ImagePartType.Jpeg;
    else if (imageType.Equals(".jpg"))
    oPartType = ImagePartType.Jpeg;
    else if (imageType.Equals(".pcx"))
    oPartType = ImagePartType.Pcx;
    else if (imageType.Equals(".png"))
    oPartType = ImagePartType.Png;
    else if (imageType.Equals(".tiff"))
    oPartType = ImagePartType.Tiff;
    else if (imageType.Equals(".wmf"))
    oPartType = ImagePartType.Wmf;

    ImagePart imagePart = oMainDocPartGlobal.AddImagePart(oPartType);

    SizeF imageSize;

    using (FileStream stream = new FileStream(nodeValue, FileMode.Open))
    {

    imagePart.FeedData(stream);

    }
    using (FileStream stream = new FileStream(nodeValue, FileMode.Open))
    {

    using (Image image = Image.FromStream(stream))
    {
    imageSize = image.PhysicalDimension;
    }

    }

    //set image width + height
    if (Width == 0)
    Width = (int)imageSize.Width;

    if (Height == 0)
    Height = (int)imageSize.Height;


    // And set some values, mainly on the attribute nodes
    int widthInEMUs = Width * 9525;
    //int heightInEMUs = (int)((float)widthInEMUs / (Width / Height));
    int heightInEMUs = Height * 9525;


    // Get the reference ID for the image added to the package.
    // You will use the image part reference ID to insert the
    // image to the document.xml file.
    string _imagePartRID = oMainDocPartGlobal.GetIdOfPart(imagePart);

    String drawingTemplate = constdrawingTemplate;

    //replace constants
    drawingTemplate = drawingTemplate.Replace("{WIDTH-IN-EMUS}", widthInEMUs.ToString());
    drawingTemplate = drawingTemplate.Replace("{HEIGHT-IN-EMUS}", heightInEMUs.ToString());
    drawingTemplate = drawingTemplate.Replace("{DRAWING-OBJECT-ID}", bookmarkCounter.ToString());
    drawingTemplate = drawingTemplate.Replace("{FILENAME-OF-IMAGE}", ProcessDataForXML(imageFileName));
    drawingTemplate = drawingTemplate.Replace("{PICTURE-ID}", bookmarkCounter.ToString());
    drawingTemplate = drawingTemplate.Replace("{PICTUREID}", "Picture " + bookmarkCounter.ToString());
    drawingTemplate = drawingTemplate.Replace("{RELATIONSHIP-ID-OF-IMAGE-PART}", _imagePartRID);



    XmlDocumentFragment oDocFrag = mainDocument.CreateDocumentFragment();
    oDocFrag.InnerXml = drawingTemplate;

    if (oNode.InnerXml.IndexOf("w:tc") > 0)
    {
    XmlNode tcNode = oNode.SelectSingleNode("w:tc//w:p", mainNamespaceMrg);
    if (tcNode != null)
    {
    oDocFrag.InnerXml = constdrawingTemplateParaStart + oDocFrag.InnerXml + "
    ";
    tcNode.ParentNode.ReplaceChild(oDocFrag, tcNode);
    }
    }
    else
    {
    //check for parent node- if it is a paragraph node then replace whole paragraph
    if (oNode.ParentNode.Name.Equals("w:p", StringComparison.CurrentCultureIgnoreCase))
    {
    oNode.ParentNode.ReplaceChild(oDocFrag, oNode);
    }
    else
    {
    oDocFrag.InnerXml = constdrawingTemplateParaStart + oDocFrag.InnerXml + "";
    oNode.ParentNode.ReplaceChild(oDocFrag, oNode);
    }
    }
    }
  • Why are you spoiling my post with dirty C# code ?
  • Reply for thread 7188 (zrc6666 )
Page 1 of 1 (6 items)