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);
    }

  • Dear Gerhard Kordmann

    Many thanks for your response. This looks quite complicated. 

    Best Marco

  • Former Member
    0 Former Member in reply to Marco Holzer

    actually, this post should moved into "Developer" area.

    if you want to use SDK, you have to familiar with C# and SDL's nice samples.

    Mr. Gerhard Kordmann's code is very standard one and clearly written.

    Regards

  • An API user should not know, that you store XML nor should the user have to construct XML. Presentation layer?  Information Disclosure? 

Reply Children
No Data