Convert OneNote to HTML in Java
Contents
[
Hide
Show
]Save to Byte Array With Embedded Resources
1Document document = new Document(dataDir + "Sample1.one");
2
3HtmlSaveOptions options = new HtmlSaveOptions();
4options.setExportCss(ResourceExportType.ExportEmbedded);
5options.setExportImages(ResourceExportType.ExportEmbedded);
6options.setExportFonts(ResourceExportType.ExportEmbedded);
7options.setFontFaceTypes(FontFaceType.Ttf);
8
9ByteArrayOutputStream r = new ByteArrayOutputStream();
10document.save(r, options);
Save to Byte Array with Callbacks
1Document document = new Document(dataDir + "Sample1.one");
2
3UserSavingCallbacks savingCallbacks = new UserSavingCallbacks();
4savingCallbacks.setRootFolder("documentFolder");
5savingCallbacks.setCssFolder("css");
6savingCallbacks.setKeepCssStreamOpened(true);
7savingCallbacks.setImagesFolder("images");
8savingCallbacks.setFontsFolder("fonts");
9
10HtmlSaveOptions options = new HtmlSaveOptions();
11options.setFontFaceTypes(FontFaceType.Ttf);
12options.setCssSavingCallback(savingCallbacks);
13options.setImageSavingCallback(savingCallbacks);
14options.setFontSavingCallback(savingCallbacks);
15options.setExportCss(ResourceExportType.ExportEmbedded);
16options.setExportImages(ResourceExportType.ExportEmbedded);
17options.setExportFonts(ResourceExportType.ExportEmbedded);
18
19File dir = new File(savingCallbacks.getRootFolder());
20if (!dir.exists()) {
21 dir.mkdir();
22}
23
24document.save(Paths.get(savingCallbacks.getRootFolder(), "document.html").toString(), options);
25
26try (OutputStreamWriter writer = new OutputStreamWriter(savingCallbacks.getCssStream(), "utf-8")) {
27 writer.write(System.lineSeparator());
28 writer.write("/* This line is appended to stream manually by user */");
29 writer.close();
30}
The above sample will require the following class for execution.
1public final int getCssSaved() {
2 return auto_CssSaved;
3}
4
5private void setCssSaved(int value) {
6 auto_CssSaved = value;
7}
8
9private int auto_CssSaved;
10
11public final int getFontsSaved() {
12 return auto_FontsSaved;
13}
14
15private void setFontsSaved(int value) {
16 auto_FontsSaved = value;
17}
18
19private int auto_FontsSaved;
20
21public final int getImagessSaved() {
22 return auto_ImagessSaved;
23}
24
25private void setImagessSaved(int value) {
26 auto_ImagessSaved = value;
27}
28
29private int auto_ImagessSaved;
30
31public final String getRootFolder() {
32 return auto_RootFolder;
33}
34
35public final void setRootFolder(String value) {
36 auto_RootFolder = value;
37}
38
39private String auto_RootFolder;
40
41public final boolean getKeepCssStreamOpened() {
42 return auto_KeepCssStreamOpened;
43}
44
45public final void setKeepCssStreamOpened(boolean value) {
46 auto_KeepCssStreamOpened = value;
47}
48
49private boolean auto_KeepCssStreamOpened;
50
51public final String getCssFolder() {
52 return auto_CssFolder;
53}
54
55public final void setCssFolder(String value) {
56 auto_CssFolder = value;
57}
58
59private String auto_CssFolder;
60
61public final OutputStream getCssStream() {
62 return auto_CssStream;
63}
64
65private void setCssStream(OutputStream value) {
66 auto_CssStream = value;
67}
68
69private OutputStream auto_CssStream;
70
71public final String getFontsFolder() {
72 return auto_FontsFolder;
73}
74
75public final void setFontsFolder(String value) {
76 auto_FontsFolder = value;
77}
78
79private String auto_FontsFolder;
80
81public final String getImagesFolder() {
82 return auto_ImagesFolder;
83}
84
85public final void setImagesFolder(String value) {
86 auto_ImagesFolder = value;
87}
88
89private String auto_ImagesFolder;
90
91public final void fontSaving(FontSavingArgs args) {
92 String uri = null;
93 OutputStream stream = null;
94 String[] referenceToUri = { uri };
95 OutputStream[] referenceToStream = { stream };
96 this.createResourceInFolder(this.getFontsFolder(), args.getFileName(), /* out */ referenceToUri,
97 /* out */ referenceToStream);
98 uri = referenceToUri[0];
99 stream = referenceToStream[0];
100 args.setStream(stream);
101 args.setUri(Paths.get("..", uri).toString().replace("\\", "\\\\"));
102
103 this.setFontsSaved(this.getFontsSaved() + 1);
104}
105
106public final void cssSaving(CssSavingArgs args) {
107 String uri = null;
108 OutputStream stream = null;
109 String[] referenceToUri = { uri };
110 OutputStream[] referenceToStream = { stream };
111 this.createResourceInFolder(this.getCssFolder(), args.getFileName(), /* out */ referenceToUri,
112 /* out */ referenceToStream);
113 uri = referenceToUri[0];
114 stream = referenceToStream[0];
115 this.setCssStream(stream);
116 args.setStream(stream);
117 args.setKeepStreamOpen(this.getKeepCssStreamOpened());
118 args.setUri(uri);
119
120 this.setCssSaved(this.getCssSaved() + 1);
121}
122
123public final void imageSaving(ImageSavingArgs args) {
124 String uri = null;
125 OutputStream stream = null;
126 String[] referenceToUri = { uri };
127 OutputStream[] referenceToStream = { stream };
128 this.createResourceInFolder(this.getImagesFolder(), args.getFileName(), /* out */ referenceToUri,
129 /* out */ referenceToStream);
130 uri = referenceToUri[0];
131 stream = referenceToStream[0];
132 args.setStream(stream);
133 args.setUri(uri);
134
135 this.setImagessSaved(this.getImagessSaved() + 1);
136}
137
138private void createResourceInFolder(String folder, String filename, /* out */ String[] uri,
139 /* out */ OutputStream[] stream) {
140 String relativePath = folder;
141
142 String fullPath = Paths.get(this.getRootFolder(), relativePath).toString();
143 File dir = new File(fullPath);
144 try {
145 if (!dir.exists()) {
146 dir.mkdir();
147 }
148
149 stream[0] = new FileOutputStream(Paths.get(fullPath, filename).toFile());
150 } catch (IOException e) {
151 throw new RuntimeException(e);
152 }
153 uri[0] = Paths.get(relativePath, filename).toString();
154}
Save as HTML File with Resources In Separate File
1Document document = new Document(dataDir + "Sample1.one");
2
3HtmlSaveOptions options = new HtmlSaveOptions();
4options.setExportCss(ResourceExportType.ExportEmbedded);
5options.setExportFonts(ResourceExportType.ExportEmbedded);
6options.setExportImages(ResourceExportType.ExportEmbedded);
7
8document.save(dataDir + "document.html", options);