Create and export Anki collections, decks, notes and cards from your C# applications.
var noteType = new AnkiNoteType(
name: "Basic",
cardTypes: new[] {
new AnkiCardType(
Name: "Card 1",
Ordinal: 0,
QuestionFormat: "{{Front}}",
AnswerFormat: "{{Front}}<hr id=\"answer\">{{Back}}"
)
},
fieldNames: new[] { "Front", "Back" }
);
var collection = new AnkiCollection();
var noteTypeId = collection.CreateNoteType(noteType);
var deckId = collection.CreateDeck("My Anki Deck");
collection.CreateNote(deckId, noteTypeId, "Hello", "Bonjour");
await AnkiFileWriter.WriteToFileAsync("MyCollection.apkg", collection);
Anki.NET is a fork form the the archived AnkiSharp project from Clement-Jean. Thanks a lot for the hard work!
Start by creating an AnkiCollection
. To add notes to the collection, you need a notes model (AnkiNoteType
), you can pass in the constructor, like this.
A note can correspond to one or several cards, if their model has several card tempates ('AnkiCardType').
var cardTypes = new[]
{
new AnkiCardType(
"Forwards",
0,
"{{Front}}<br/>{{hint:Help}}",
"{{Front}}<hr id=\"answer\">{{Back}}"
),
new AnkiCardType(
"Backwards",
1,
"{{Back}}<br/>{{hint:Help}}",
"{{Back}}<hr id=\"answer\">{{Front}}"
)
};
var noteType = new AnkiNoteType(
"Basic (With hints)",
cardTypes,
new[] { "Front", "Back", "Help" }
);
var collection = new AnkiCollection();
var noteTypeId = collection.CreateNoteType(noteType);
var collection = new AnkiCollection();
var deckId = collection.CreateDeck("French vocabulary");
bool deckExists = collection.TryGetDeckById(deckId, out var deck);
With the above AnkiNoteType
(which has two card types), each added note will generate 2 different cards.
collection.CreateNote(deckId, notetypeId, "Hello", "Bonjour", "");
collection.CreateNote(deckId, noteTypeId, "House", "Maison", "Starts with \"M\"");
var noteType = new AnkiNoteType(
name: "Basic",
cardTypes: cardTypes,
fieldNames: names,
css: @".card{
color: red;
}",
);
AnkiCollection collection = await AnkiFileReader.ReadFromFileAsync("collection.apkg");
var collection = new AnkiCollection();
await AnkiFileWriter.WriteToFileAsync("MyCollection.apkg", collection);