Sometimes you may need to copy information between the old Info
store and the new Metadata store.
This example shows how you might do this. Deciding exactly how
to deal with clashes between the two stores is rather application
dependent so you should be expecting that you will want to adapt
this code to your needs.
// We do not copy over the CreationDate or ModDate because we do not have to
// (they have not been deprecated) and we are likely simply to end up with
// two clashing values. A similar situation is true for Trapped.
bool copyCreationDate = false, copyModDate = false, copyTrapped = false;
IndirectObject trailer = doc.ObjectSoup.Trailer;
DictAtom info = trailer.Resolve(Atom.GetItem(trailer.Atom, "Info")) as DictAtom;
if (info != null) {
string title = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Title")));
string author = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Author")));
string subject = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Subject")));
string keywords = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Keywords")));
string creator = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Creator")));
string producer = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Producer")));
string creationDate = copyCreationDate ? Atom.GetText(trailer.Resolve(Atom.GetItem(info, "CreationDate"))) : null;
string modDate = copyModDate ? Atom.GetText(trailer.Resolve(Atom.GetItem(info, "ModDate"))) : null;
string trapped = copyTrapped ? Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Trapped"))) : null;
bool hasValues = (!string.IsNullOrEmpty(title)) || (!string.IsNullOrEmpty(author)) || (!string.IsNullOrEmpty(subject)) ||
(!string.IsNullOrEmpty(keywords)) || (!string.IsNullOrEmpty(creator)) || (!string.IsNullOrEmpty(producer)) ||
(!string.IsNullOrEmpty(creationDate)) || (!string.IsNullOrEmpty(modDate)) || (!string.IsNullOrEmpty(trapped));
if (hasValues) {
Metadata md = doc.ObjectSoup.Catalog.Metadata;
if (md == null) {
md = new Metadata(doc.ObjectSoup);
doc.ObjectSoup.Catalog.Metadata = md;
}
if ((!string.IsNullOrEmpty(title)) && (string.IsNullOrEmpty(md.InfoTitle)))
md.InfoTitle = title;
if ((!string.IsNullOrEmpty(author)) && (string.IsNullOrEmpty(md.InfoAuthor)))
md.InfoAuthor = author;
if ((!string.IsNullOrEmpty(subject)) && (string.IsNullOrEmpty(md.InfoSubject)))
md.InfoSubject = subject;
if ((!string.IsNullOrEmpty(keywords)) && (string.IsNullOrEmpty(md.InfoKeywords)))
md.InfoKeywords = keywords;
if ((!string.IsNullOrEmpty(creator)) && (string.IsNullOrEmpty(md.InfoCreator)))
md.InfoCreator = creator;
if ((!string.IsNullOrEmpty(producer)) && (string.IsNullOrEmpty(md.InfoProducer)))
md.InfoProducer = producer;
if ((!string.IsNullOrEmpty(creationDate)) && (string.IsNullOrEmpty(md.InfoCreationDate)))
md.InfoCreationDate = StringAtom.StringToDate(creationDate).ToString("o");
if ((!string.IsNullOrEmpty(modDate)) && (string.IsNullOrEmpty(md.InfoModDate)))
md.InfoModDate = StringAtom.StringToDate(modDate).ToString("o");
}
Atom.RemoveItem(info, "Title");
Atom.RemoveItem(info, "Author");
Atom.RemoveItem(info, "Subject");
Atom.RemoveItem(info, "Keywords");
Atom.RemoveItem(info, "Creator");
Atom.RemoveItem(info, "Producer");
if (copyCreationDate)
Atom.RemoveItem(info, "CreationDate");
if (copyModDate)
Atom.RemoveItem(info, "ModDate");
if (copyTrapped)
Atom.RemoveItem(info, "Trapped");
}
Dim copyCreationDate As Boolean = False, copyModDate As Boolean = False, copyTrapped As Boolean = False
Dim trailer As IndirectObject = doc.ObjectSoup.Trailer
Dim info As DictAtom = TryCast(trailer.Resolve(Atom.GetItem(trailer.Atom, "Info")), DictAtom)
If info IsNot Nothing Then
Dim title As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Title")))
Dim author As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Author")))
Dim subject As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Subject")))
Dim keywords As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Keywords")))
Dim creator As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Creator")))
Dim producer As String = Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Producer")))
Dim creationDate As String = If(copyCreationDate, Atom.GetText(trailer.Resolve(Atom.GetItem(info, "CreationDate"))), Nothing)
Dim modDate As String = If(copyModDate, Atom.GetText(trailer.Resolve(Atom.GetItem(info, "ModDate"))), Nothing)
Dim trapped As String = If(copyTrapped, Atom.GetText(trailer.Resolve(Atom.GetItem(info, "Trapped"))), Nothing)
Dim hasValues As Boolean = (Not String.IsNullOrEmpty(title)) OrElse (Not String.IsNullOrEmpty(author)) OrElse (Not String.IsNullOrEmpty(subject)) OrElse _
(Not String.IsNullOrEmpty(keywords)) OrElse (Not String.IsNullOrEmpty(creator)) OrElse (Not String.IsNullOrEmpty(producer)) OrElse _
(Not String.IsNullOrEmpty(creationDate)) OrElse (Not String.IsNullOrEmpty(modDate)) OrElse (Not String.IsNullOrEmpty(trapped))
If hasValues Then
Dim md As Metadata = doc.ObjectSoup.Catalog.Metadata
If md Is Nothing Then
md = New Metadata(doc.ObjectSoup)
doc.ObjectSoup.Catalog.Metadata = md
End If
If (Not String.IsNullOrEmpty(title)) AndAlso (String.IsNullOrEmpty(md.InfoTitle)) Then md.InfoTitle = title
If (Not String.IsNullOrEmpty(author)) AndAlso (String.IsNullOrEmpty(md.InfoAuthor)) Then md.InfoAuthor = author
If (Not String.IsNullOrEmpty(subject)) AndAlso (String.IsNullOrEmpty(md.InfoSubject)) Then md.InfoSubject = subject
If (Not String.IsNullOrEmpty(keywords)) AndAlso (String.IsNullOrEmpty(md.InfoKeywords)) Then md.InfoKeywords = keywords
If (Not String.IsNullOrEmpty(creator)) AndAlso (String.IsNullOrEmpty(md.InfoCreator)) Then md.InfoCreator = creator
If (Not String.IsNullOrEmpty(producer)) AndAlso (String.IsNullOrEmpty(md.InfoProducer)) Then md.InfoProducer = producer
If (Not String.IsNullOrEmpty(creationDate)) AndAlso (String.IsNullOrEmpty(md.InfoCreationDate)) Then md.InfoCreationDate = StringAtom.StringToDate(creationDate).ToString("o")
If (Not String.IsNullOrEmpty(modDate)) AndAlso (String.IsNullOrEmpty(md.InfoModDate)) Then md.InfoModDate = StringAtom.StringToDate(modDate).ToString("o")
End If
Atom.RemoveItem(info, "Title")
Atom.RemoveItem(info, "Author")
Atom.RemoveItem(info, "Subject")
Atom.RemoveItem(info, "Keywords")
Atom.RemoveItem(info, "Creator")
Atom.RemoveItem(info, "Producer")
If copyCreationDate Then Atom.RemoveItem(info, "CreationDate")
If copyModDate Then Atom.RemoveItem(info, "ModDate")
If copyTrapped Then Atom.RemoveItem(info, "Trapped")
End If