How to programmatically insert and export an image with the SDK?

Dear All

I would like to update an existing sdltb file with the sdl sdk. Has anyone experience regarding this matter.

Best

Marco

Parents
  • Hi ,

    images are accessed per entry. Exporting them is very easy, importing a bit more complex: add the image itself to the termbase storage, and then add a field linking to it to the entry. Code is clearer than explanations, so here we go. This assumes your termbase has a multimedia field called "imageField" at the entry level, and you want to edit the entry with id 3

    Sdl.MultiTerm.TMO.Interop.Application _mt;
    TermbaseRepository _tr;
    Termbase _tb;

    public void runTests()
    {
        int entryId = 3;
        Init(@"E:\_temp\media.sdltb");
        AddImageSample(entryId, "imageField", @"E:\_temp\p3.jpg");
        ExtractImageSample(entryId, @"E:\_temp", "mediaexport");
        Exit();
    }

    private void Init(string termbasePath)
    {
        _mt = new Sdl.MultiTerm.TMO.Interop.Application();
        _tr = _mt.LocalRepository;
        _tr.Connect("", "");
        _tr.Termbases.Add(termbasePath, "", "");
        _tb = _tr.Termbases[termbasePath];
    }

    private void Exit()
    {
        _tb.Close();
        _tr.Disconnect();
    }

    private void ExtractImageSample(int entryId, string targetFolder, string subfolderLabel)
    {
        Entry en = _tb.Entries.Item(entryId);
        en.ApplyExportWithMultimedia(_tb.ExportDefinitions["Default export definition"], "", "", targetFolder, subfolderLabel);
        // or alternatively, to system temp folder
        en.ExtractMultimediaToTemp();
    }

    private void AddImageSample(int entryId, string fieldName, string imagePath)
    {
        int mediaId = StoreImage(entryId, imagePath);

        Entry en = _tb.Entries.Item(entryId);
        EntryContent ec = en.Content;
        ec.Content = AddImageToEntryField(ec.Content, fieldName, mediaId.ToString(), imagePath);
        en.LockEntry(MtLockingState.mtLock);
        ec.Update();
        en.LockEntry(MtLockingState.mtUnlock);
    }

    private string AddImageToEntryField(string entryContent, string fieldName, string mediaId, string path)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(entryContent);

        XmlNode conceptGrpNode = doc.SelectSingleNode("conceptGrp");

        XmlNode descripGrpNode = doc.CreateElement("descripGrp");
        conceptGrpNode.AppendChild(descripGrpNode);
        XmlAttribute attr = doc.CreateAttribute("multimedia");
        attr.Value = mediaId.ToString();
        descripGrpNode.Attributes.Append(attr);

        XmlNode descripNode = doc.CreateElement("descrip");
        descripGrpNode.AppendChild(descripNode);
        attr = doc.CreateAttribute("type");
        attr.Value = fieldName;
        descripNode.Attributes.Append(attr);
        descripNode.InnerText = Path.GetFileName(path);

        return conceptGrpNode.OuterXml;
    }

    private int StoreImage(int entryId, string path)
    {
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(new FileStream(path, FileMode.Open)))
        {
            FileInfo fi = new FileInfo(path);
            bytes = br.ReadBytes((int)fi.Length);
        }
        string blob = "<?xml version=\"1.0\"?><root xmlns:dt=\"urn:schemas-microsoft-com:datatypes\"><file1 dt:dt=\"bin.base64\">"
            + Convert.ToBase64String(bytes) +
        "</file1></root>";
        return _tb.AddBLOB(entryId, Path.GetFileName(path), blob);
    }

  • Former Member
    0 Former Member in reply to Gerhard Kordmann

    nice !!

    If you have some extra time, Can you show me how to import "myTerms.mtf.xml" ? I am not so clear about it.

    Regards

  • Former Member
    0 Former Member in reply to Gerhard Kordmann

    right.

    But, you said there "not worth digging into this here"

    Do you really think digging into images for TB is much worhwhile than regular wordings ?

    Wordings should be more 100 times important I guess.

    -I have never used images in TB before and I will not have any chance to use it in the future too.

    .

    And, at that comment, you have not finished-but you spent lots of time here..

    I just want to finish it properly.  

Reply Children