Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import as text and graphics #30

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions OnenoteAddin/AddIn.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Extensibility;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.OneNote;
Expand Down
3 changes: 3 additions & 0 deletions OnenoteAddin/ComLocalServer/ManagedCOMLocalServe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Text;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Collections;
using System.Configuration;

namespace RemarkableSync.OnenoteAddin
{
Expand Down
12 changes: 12 additions & 0 deletions OnenoteAddin/OneNoteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public void AppendPageImages(string pageId, List<Bitmap> images, double zoom = 1
_application.UpdatePageContent(pageDoc.ToString(), DateTime.MinValue, XMLSchema.xs2013);
}

public void AppendPageImage(string pageId, Bitmap image, double zoom = 1.0)
{
string xml;
_application.GetPageContent(pageId, out xml, PageInfo.piAll, XMLSchema.xs2013);
var pageDoc = XDocument.Parse(xml);

int yPos = GetBottomContentYPos(pageDoc);
AppendImage(pageDoc, image, zoom, yPos);

_application.UpdatePageContent(pageDoc.ToString(), DateTime.MinValue, XMLSchema.xs2013);
}

private int AppendImage(XDocument pageDoc, Bitmap bitmap, double zoom, int yPos)
{
int height = (int) Math.Round(bitmap.Height * zoom);
Expand Down
1 change: 0 additions & 1 deletion OnenoteAddin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down
50 changes: 41 additions & 9 deletions OnenoteAddin/RmDownloadForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 97 additions & 13 deletions OnenoteAddin/RmDownloadForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -11,6 +12,14 @@ namespace RemarkableSync.OnenoteAddin
{
public partial class RmDownloadForm : Form
{
enum ImportMode
{
Text = 0,
Graphics,
Both,
Unknown
}

class RmTreeNode : TreeNode
{
public RmTreeNode(string id, string visibleName, bool isCollection)
Expand Down Expand Up @@ -125,13 +134,26 @@ private async void btnOk_Click(object sender, EventArgs e)
}

RmTreeNode rmTreeNode = (RmTreeNode) rmTreeView.SelectedNode;
bool importAsGraphics = chkImportAsGraphics.Checked;
double zoom = (double)numericGraphicWidth.Value / 100.0;
Logger.LogMessage($"Selected: {rmTreeNode.VisibleName} | {rmTreeNode.ID}");

ImportMode mode = ImportMode.Unknown;
if (radioBtnImportText.Checked)
{
mode = ImportMode.Text;
}
else if (radioBtnImportGraphics.Checked)
{
mode = ImportMode.Graphics;
}
else if (radioBtnImportBoth.Checked)
{
mode = ImportMode.Both;
}

try
{
bool success = await ImportDocument(rmTreeNode, importAsGraphics, zoom);
bool success = await ImportDocument(rmTreeNode, mode, zoom);
Logger.LogMessage("Import " + (success ? "successful" : "failed"));
}
catch (Exception err)
Expand All @@ -144,7 +166,7 @@ private async void btnOk_Click(object sender, EventArgs e)
}


private async Task<bool> ImportDocument(RmTreeNode rmTreeNode, bool importAsGraphics, double zoom = 0.5)
private async Task<bool> ImportDocument(RmTreeNode rmTreeNode, ImportMode mode, double zoom = 0.5)
{
if (rmTreeNode.IsCollection)
{
Expand All @@ -170,21 +192,29 @@ private async Task<bool> ImportDocument(RmTreeNode rmTreeNode, bool importAsGrap
}
}

return importAsGraphics ?
ImportContentAsGraphics(pages, rmTreeNode.VisibleName, zoom) :
await ImportContentAsText(pages, rmTreeNode.VisibleName);
switch (mode)
{
case ImportMode.Text:
return await ImportContentAsText(pages, rmTreeNode.VisibleName);
case ImportMode.Graphics:
return ImportContentAsGraphics(pages, rmTreeNode.VisibleName, zoom);
case ImportMode.Both:
return await ImportContentAsBoth(pages, rmTreeNode.VisibleName, zoom);
default:
Logger.LogMessage($"ImportDocument() - unknown import mode: {mode}");
break;
}
return true;
}

private async Task<bool> ImportContentAsText(List<RmPage> pages, string visibleName)
{
lblInfo.Text = $"Digitising {visibleName}...";
MyScriptClient hwrClient = new MyScriptClient(_configStore);
Logger.LogMessage("requesting hand writing recognition");
MyScriptResult result = await hwrClient.RequestHwr(pages);

if (result != null)
List<string> results = await GetHwrResultAsync(pages);
if (results != null)
{
UpdateOneNoteWithHwrResult(visibleName, result);
UpdateOneNoteWithHwrResult(visibleName, results);
lblInfo.Text = $"Imported {visibleName} successfully.";
Task.Run(() =>
{
Expand All @@ -199,12 +229,24 @@ private async Task<bool> ImportContentAsText(List<RmPage> pages, string visibleN
return true;
}

private void UpdateOneNoteWithHwrResult(string name, MyScriptResult result)
private async Task<List<string>> GetHwrResultAsync(List<RmPage> pages)
{
Logger.LogMessage($"GetHwrResultAsync() - requesting hand writing recognition for {pages.Count} pages");
MyScriptClient hwrClient = new MyScriptClient(_configStore);
var hwrResults = (await Task.WhenAll(pages.Select((page, index) => hwrClient.RequestHwr(page, index)))).ToList();
hwrResults.Sort((result1, result2) => result1.Item1.CompareTo(result2.Item1));
return hwrResults.Select(result => result.Item2).ToList();
}

private void UpdateOneNoteWithHwrResult(string name, List<string> result)
{
OneNoteHelper oneNoteHelper = new OneNoteHelper(_application);
string currentSectionId = oneNoteHelper.GetCurrentSectionId();
string newPageId = oneNoteHelper.CreatePage(currentSectionId, name);
oneNoteHelper.AddPageContent(newPageId, result.label);
foreach (string content in result)
{
oneNoteHelper.AddPageContent(newPageId, content);
}
}

private bool ImportContentAsGraphics(List<RmPage> pages, string visibleName, double zoom)
Expand All @@ -225,6 +267,48 @@ private bool ImportContentAsGraphics(List<RmPage> pages, string visibleName, dou
return true;
}

private async Task<bool> ImportContentAsBoth(List<RmPage> pages, string visibleName, double zoom)
{
lblInfo.Text = $"Importing {visibleName} as both text and graphics...";

List<string> textResults = await GetHwrResultAsync(pages);
List<Bitmap> graphicsResults = RmLinesDrawer.DrawPages(pages);
if (textResults.Count != graphicsResults.Count)
{
Logger.LogMessage($"ImportContentAsBoth() - got {textResults.Count} text results and {graphicsResults.Count} graphics results");
lblInfo.Text = $"Imported {visibleName} as both text and graphics encountered error.";
return true;
}

List<Tuple<string, Bitmap>> result = new List<Tuple<string, Bitmap>>(textResults.Count);
for(int i = 0; i < textResults.Count; ++i)
{
result.Add(Tuple.Create(textResults[i], graphicsResults[i]));
}

UpdateOneNoteWithHwrResultAndGraphics(visibleName, result, zoom);

lblInfo.Text = $"Imported {visibleName} as both text and graphics successful.";
Task.Run(() =>
{
Thread.Sleep(500);
}).Wait();
Close();
return true;
}

private void UpdateOneNoteWithHwrResultAndGraphics(string name, List<Tuple<string, Bitmap>> result, double zoom)
{
OneNoteHelper oneNoteHelper = new OneNoteHelper(_application);
string currentSectionId = oneNoteHelper.GetCurrentSectionId();
string newPageId = oneNoteHelper.CreatePage(currentSectionId, name);
foreach (var pageResult in result)
{
oneNoteHelper.AddPageContent(newPageId, pageResult.Item1);
oneNoteHelper.AppendPageImage(newPageId, pageResult.Item2, zoom);
}
}

private void RmDownloadForm_FormClosing(object sender, FormClosingEventArgs e)
{
_rmDataSource?.Dispose();
Expand Down
Loading