Skip to content

Commit 385a878

Browse files
Examples for GroupDocs.Viewer for .NET 19.8
1 parent b9ccbd3 commit 385a878

File tree

385 files changed

+3010
-74233
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

385 files changed

+3010
-74233
lines changed

.gitignore

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bld/
2222
[Bb]in/
2323
[Oo]bj/
2424

25-
# Visual Studo 2015 cache/options directory
25+
# Visual Studio 2015 cache/options directory
2626
.vs/
2727

2828
# MSTest test Results
@@ -38,6 +38,10 @@ TestResult.xml
3838
[Rr]eleasePS/
3939
dlldata.c
4040

41+
# DNX
42+
project.lock.json
43+
artifacts/
44+
4145
*_i.c
4246
*_p.c
4347
*_i.h
@@ -90,7 +94,7 @@ _ReSharper*/
9094
*.[Rr]e[Ss]harper
9195
*.DotSettings.user
9296

93-
# JustCode is a .NET coding addin-in
97+
# JustCode is a .NET coding add-in
9498
.JustCode
9599

96100
# TeamCity is a build add-in
@@ -129,9 +133,11 @@ publish/
129133
# Publish Web Output
130134
*.[Pp]ublish.xml
131135
*.azurePubxml
132-
# TODO: Comment the next line if you want to checkin your web deploy settings
133-
# but database connection strings (with potential passwords) will be unencrypted
134-
*.pubxml
136+
## TODO: Comment the next line if you want to checkin your
137+
## web deploy settings but do note that will include unencrypted
138+
## passwords
139+
#*.pubxml
140+
135141
*.publishproj
136142

137143
# NuGet Packages
@@ -150,8 +156,13 @@ csx/
150156
# Windows Store app package directory
151157
AppPackages/
152158

153-
# Others
159+
# Visual Studio cache files
160+
# files ending in .cache can be ignored
154161
*.[Cc]ache
162+
# but keep track of directories ending in .cache
163+
!*.[Cc]ache/
164+
165+
# Others
155166
ClientBin/
156167
[Ss]tyle[Cc]op.*
157168
~$*
@@ -161,7 +172,7 @@ ClientBin/
161172
*.pfx
162173
*.publishsettings
163174
node_modules/
164-
bower_components/
175+
orleans.codegen.cs
165176

166177
# RIA/Silverlight projects
167178
Generated_Code/
@@ -194,3 +205,17 @@ FakesAssemblies/
194205

195206
# Visual Studio 6 workspace options file
196207
*.opt
208+
209+
# LightSwitch generated files
210+
GeneratedArtifacts/
211+
_Pvt_Extensions/
212+
ModelManifest.xml
213+
214+
#VSCode
215+
.vscode/
216+
217+
#NuGet
218+
*.nuspec
219+
220+
#Batch
221+
*.bat

Examples/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#GroupDocs.Viewer.Examples.CSharp/.vs/*
2+
GroupDocs.Viewer.Examples.CSharp/bin/*
3+
GroupDocs.Viewer.Examples.CSharp/obj/*
4+
GroupDocs.Viewer.Examples.CSharp/Output
-117 KB
Binary file not shown.
-48.6 KB
Binary file not shown.
-480 KB
Binary file not shown.

Examples/Data/Storage/sample.cdr

-282 KB
Binary file not shown.

Examples/Data/Storage/sample.ost

-2.2 MB
Binary file not shown.

Examples/Data/Storage/sample.pptx

-29.4 KB
Binary file not shown.

Examples/Data/Storage/sample.xlsx

-149 KB
Binary file not shown.

Examples/Data/Storage/sample.zip

-657 KB
Binary file not shown.

Examples/Data/Storage/sample2.cdr

-19.1 KB
Binary file not shown.
-8.56 KB
Binary file not shown.

Examples/Data/Storage/word.doc

-32.5 KB
Binary file not shown.
Binary file not shown.
-27.3 KB
Binary file not shown.
Binary file not shown.
-34.1 KB
Binary file not shown.
Binary file not shown.
-32.8 KB
Binary file not shown.
Binary file not shown.
-41.3 KB
Binary file not shown.

Examples/Data/screenshots/arch.png

-44 KB
Binary file not shown.
-22.5 KB
Binary file not shown.
-90.3 KB
Binary file not shown.

Examples/Data/screenshots/reorder.png

-538 KB
Binary file not shown.
-578 KB
Binary file not shown.

Examples/Data/screenshots/viewer.png

-147 KB
Binary file not shown.
-269 KB
Binary file not shown.
-603 KB
Binary file not shown.

Examples/GroupDocs.Viewer.Examples.CSharp.sln

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Runtime.Serialization;
8+
using System.Runtime.Serialization.Formatters.Binary;
9+
using GroupDocs.Viewer.Caching;
10+
using GroupDocs.Viewer.Options;
11+
using StackExchange.Redis;
12+
13+
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Caching
14+
{
15+
class HowToUseCustomCacheImplementation
16+
{
17+
/// <summary>
18+
/// This example demonstrates how to implement custom cache when rendering document.
19+
/// </summary>
20+
public static void Run()
21+
{
22+
string outputDirectory = Constants.GetOutputDirectoryPath();
23+
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");
24+
25+
RedisCache cache = new RedisCache("sample_");
26+
ViewerSettings settings = new ViewerSettings(cache);
27+
28+
using (Viewer viewer = new Viewer(Constants.SAMPLE_DOCX, settings))
29+
{
30+
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
31+
32+
Stopwatch stopWatch = Stopwatch.StartNew();
33+
viewer.View(options);
34+
stopWatch.Stop();
35+
36+
Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
37+
38+
stopWatch.Restart();
39+
viewer.View(options);
40+
stopWatch.Stop();
41+
42+
Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
43+
}
44+
45+
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
46+
}
47+
}
48+
49+
public class RedisCache : ICache, IDisposable
50+
{
51+
private readonly string _cacheKeyPrefix;
52+
53+
private readonly ConnectionMultiplexer _redis;
54+
private readonly IDatabase _db;
55+
private readonly string _host = "192.168.0.1:6379";
56+
57+
public RedisCache(string cacheKeyPrefix)
58+
{
59+
_cacheKeyPrefix = cacheKeyPrefix;
60+
_redis = ConnectionMultiplexer.Connect(_host);
61+
_db = _redis.GetDatabase();
62+
}
63+
64+
public void Set(string key, object data)
65+
{
66+
if (data == null)
67+
return;
68+
69+
string prefixedKey = GetPrefixedKey(key);
70+
using (MemoryStream stream = GetStream(data))
71+
{
72+
_db.StringSet(prefixedKey, RedisValue.CreateFrom(stream));
73+
}
74+
}
75+
76+
public bool TryGetValue<TEntry>(string key, out TEntry value)
77+
{
78+
var prefixedKey = GetPrefixedKey(key);
79+
var redisValue = _db.StringGet(prefixedKey);
80+
81+
if (redisValue.HasValue)
82+
{
83+
var data = typeof(TEntry) == typeof(Stream)
84+
? ReadStream(redisValue)
85+
: Deserialize(redisValue);
86+
87+
value = (TEntry)data;
88+
return true;
89+
}
90+
91+
92+
value = default;
93+
return false;
94+
}
95+
96+
public IEnumerable<string> GetKeys(string filter)
97+
{
98+
return _redis.GetServer(_host).Keys(pattern: $"*{filter}*")
99+
.Select(x => x.ToString().Replace(_cacheKeyPrefix, string.Empty))
100+
.Where(x => x.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase))
101+
.ToList();
102+
}
103+
104+
private string GetPrefixedKey(string key)
105+
=> $"{_cacheKeyPrefix}{key}";
106+
107+
private object ReadStream(RedisValue redisValue)
108+
{
109+
return new MemoryStream(redisValue);
110+
}
111+
112+
private object Deserialize(RedisValue redisValue)
113+
{
114+
object data;
115+
using (MemoryStream stream = new MemoryStream(redisValue))
116+
{
117+
BinaryFormatter formatter = new BinaryFormatter();
118+
formatter.Binder = new IgnoreAssemblyVersionSerializationBinder();
119+
120+
try
121+
{
122+
data = formatter.Deserialize(stream);
123+
}
124+
catch (SerializationException)
125+
{
126+
data = null;
127+
}
128+
}
129+
130+
return data;
131+
}
132+
133+
private MemoryStream GetStream(object data)
134+
{
135+
MemoryStream result = new MemoryStream();
136+
137+
if (data is Stream stream)
138+
{
139+
stream.Position = 0;
140+
stream.CopyTo(result);
141+
}
142+
else
143+
{
144+
BinaryFormatter formatter = new BinaryFormatter();
145+
formatter.Serialize(result, data);
146+
}
147+
148+
return result;
149+
}
150+
151+
public void Dispose()
152+
{
153+
_redis.Dispose();
154+
}
155+
156+
private class IgnoreAssemblyVersionSerializationBinder : SerializationBinder
157+
{
158+
public override Type BindToType(string assemblyName, string typeName)
159+
{
160+
string assembly = Assembly.GetExecutingAssembly().FullName;
161+
Type type = Type.GetType($"{typeName}, {assembly}");
162+
163+
return type;
164+
}
165+
}
166+
}
167+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using GroupDocs.Viewer.Caching;
5+
using GroupDocs.Viewer.Options;
6+
7+
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Caching
8+
{
9+
/// <summary>
10+
/// This example demonstrates how to enable cache when render document.
11+
/// </summary>
12+
class UseCacheWhenProcessingDocuments
13+
{
14+
public static void Run()
15+
{
16+
string outputDirectory = Constants.GetOutputDirectoryPath();
17+
string cachePath = Path.Combine(outputDirectory, "cache");
18+
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");
19+
20+
FileCache cache = new FileCache(cachePath);
21+
ViewerSettings settings = new ViewerSettings(cache);
22+
23+
using (Viewer viewer = new Viewer(Constants.SAMPLE_DOCX, settings))
24+
{
25+
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
26+
27+
Stopwatch stopWatch = Stopwatch.StartNew();
28+
viewer.View(options);
29+
stopWatch.Stop();
30+
31+
Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
32+
33+
stopWatch.Restart();
34+
viewer.View(options);
35+
stopWatch.Stop();
36+
37+
Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
38+
}
39+
40+
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
41+
}
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using GroupDocs.Viewer.Options;
5+
6+
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Loading
7+
{
8+
/// <summary>
9+
/// This example demonstrates how to specify encoding.
10+
/// </summary>
11+
class LoadDocumentsWithEncoding
12+
{
13+
public static void Run()
14+
{
15+
string filePath = Constants.SAMPLE_TXT_SHIFT_JS_ENCODED;
16+
string outputDirectory = Constants.GetOutputDirectoryPath();
17+
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");
18+
19+
GroupDocs.Viewer.Common.Func<LoadOptions> loadOptions = () =>
20+
new LoadOptions(FileType.TXT, null, Encoding.GetEncoding("shift_jis"));
21+
22+
using (Viewer viewer = new Viewer(filePath, loadOptions))
23+
{
24+
HtmlViewOptions options =
25+
HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
26+
27+
viewer.View(options);
28+
}
29+
30+
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
31+
}
32+
}
33+
}
34+

0 commit comments

Comments
 (0)