Dear All
I would like to update an existing sdltb file with the sdl sdk. Has anyone experience regarding this matter.
Best
Marco
Dear All
I would like to update an existing sdltb file with the sdl sdk. Has anyone experience regarding this matter.
Best
Marco
Hi Marco Holzer,
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);
}
Hi Marco Holzer,
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);
}
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
executed your code.
works fine, import/export both of them
nice !!
Dear Gerhard Kordmann
Many thanks for your response. This looks quite complicated.
Best Marco
We went through that here already: https://community.sdl.com/developers-more/developers/language-developers/f/sdk_qa/26930/multiterm-import
The API has not magically changed since then :)
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.
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
You asked about importing with merging duplicates. This seems to be impossible due to a bug in the API, therefore I can't do anything else for you there. I have spent much more time trying to figure that out than with the image code.
Further questions about imports please in the import thread. Cross-thread discussions are confusing.
I was confused
I have thought, by your comment, you did NOT make it because you are so busy.
.
Now I am a little bit clearer, so gave it up totally - only for today, I have tried several hours again and again to try codes.
Thanks
An API user should not know, that you store XML nor should the user have to construct XML. Presentation layer? Information Disclosure?