forked from groupdocs-viewer/GroupDocs.Viewer-for-.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples for GroupDocs.Viewer for .NET 19.8
- Loading branch information
1 parent
b9ccbd3
commit 385a878
Showing
385 changed files
with
3,010 additions
and
74,233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#GroupDocs.Viewer.Examples.CSharp/.vs/* | ||
GroupDocs.Viewer.Examples.CSharp/bin/* | ||
GroupDocs.Viewer.Examples.CSharp/obj/* | ||
GroupDocs.Viewer.Examples.CSharp/Output |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
167 changes: 167 additions & 0 deletions
167
...oupDocs.Viewer.Examples.CSharp/AdvancedUsage/Caching/HowToUseCustomCacheImplementation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using GroupDocs.Viewer.Caching; | ||
using GroupDocs.Viewer.Options; | ||
using StackExchange.Redis; | ||
|
||
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Caching | ||
{ | ||
class HowToUseCustomCacheImplementation | ||
{ | ||
/// <summary> | ||
/// This example demonstrates how to implement custom cache when rendering document. | ||
/// </summary> | ||
public static void Run() | ||
{ | ||
string outputDirectory = Constants.GetOutputDirectoryPath(); | ||
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html"); | ||
|
||
RedisCache cache = new RedisCache("sample_"); | ||
ViewerSettings settings = new ViewerSettings(cache); | ||
|
||
using (Viewer viewer = new Viewer(Constants.SAMPLE_DOCX, settings)) | ||
{ | ||
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat); | ||
|
||
Stopwatch stopWatch = Stopwatch.StartNew(); | ||
viewer.View(options); | ||
stopWatch.Stop(); | ||
|
||
Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds); | ||
|
||
stopWatch.Restart(); | ||
viewer.View(options); | ||
stopWatch.Stop(); | ||
|
||
Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds); | ||
} | ||
|
||
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}."); | ||
} | ||
} | ||
|
||
public class RedisCache : ICache, IDisposable | ||
{ | ||
private readonly string _cacheKeyPrefix; | ||
|
||
private readonly ConnectionMultiplexer _redis; | ||
private readonly IDatabase _db; | ||
private readonly string _host = "192.168.0.1:6379"; | ||
|
||
public RedisCache(string cacheKeyPrefix) | ||
{ | ||
_cacheKeyPrefix = cacheKeyPrefix; | ||
_redis = ConnectionMultiplexer.Connect(_host); | ||
_db = _redis.GetDatabase(); | ||
} | ||
|
||
public void Set(string key, object data) | ||
{ | ||
if (data == null) | ||
return; | ||
|
||
string prefixedKey = GetPrefixedKey(key); | ||
using (MemoryStream stream = GetStream(data)) | ||
{ | ||
_db.StringSet(prefixedKey, RedisValue.CreateFrom(stream)); | ||
} | ||
} | ||
|
||
public bool TryGetValue<TEntry>(string key, out TEntry value) | ||
{ | ||
var prefixedKey = GetPrefixedKey(key); | ||
var redisValue = _db.StringGet(prefixedKey); | ||
|
||
if (redisValue.HasValue) | ||
{ | ||
var data = typeof(TEntry) == typeof(Stream) | ||
? ReadStream(redisValue) | ||
: Deserialize(redisValue); | ||
|
||
value = (TEntry)data; | ||
return true; | ||
} | ||
|
||
|
||
value = default; | ||
return false; | ||
} | ||
|
||
public IEnumerable<string> GetKeys(string filter) | ||
{ | ||
return _redis.GetServer(_host).Keys(pattern: $"*{filter}*") | ||
.Select(x => x.ToString().Replace(_cacheKeyPrefix, string.Empty)) | ||
.Where(x => x.StartsWith(filter, StringComparison.InvariantCultureIgnoreCase)) | ||
.ToList(); | ||
} | ||
|
||
private string GetPrefixedKey(string key) | ||
=> $"{_cacheKeyPrefix}{key}"; | ||
|
||
private object ReadStream(RedisValue redisValue) | ||
{ | ||
return new MemoryStream(redisValue); | ||
} | ||
|
||
private object Deserialize(RedisValue redisValue) | ||
{ | ||
object data; | ||
using (MemoryStream stream = new MemoryStream(redisValue)) | ||
{ | ||
BinaryFormatter formatter = new BinaryFormatter(); | ||
formatter.Binder = new IgnoreAssemblyVersionSerializationBinder(); | ||
|
||
try | ||
{ | ||
data = formatter.Deserialize(stream); | ||
} | ||
catch (SerializationException) | ||
{ | ||
data = null; | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
private MemoryStream GetStream(object data) | ||
{ | ||
MemoryStream result = new MemoryStream(); | ||
|
||
if (data is Stream stream) | ||
{ | ||
stream.Position = 0; | ||
stream.CopyTo(result); | ||
} | ||
else | ||
{ | ||
BinaryFormatter formatter = new BinaryFormatter(); | ||
formatter.Serialize(result, data); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_redis.Dispose(); | ||
} | ||
|
||
private class IgnoreAssemblyVersionSerializationBinder : SerializationBinder | ||
{ | ||
public override Type BindToType(string assemblyName, string typeName) | ||
{ | ||
string assembly = Assembly.GetExecutingAssembly().FullName; | ||
Type type = Type.GetType($"{typeName}, {assembly}"); | ||
|
||
return type; | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...GroupDocs.Viewer.Examples.CSharp/AdvancedUsage/Caching/UseCacheWhenProcessingDocuments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using GroupDocs.Viewer.Caching; | ||
using GroupDocs.Viewer.Options; | ||
|
||
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Caching | ||
{ | ||
/// <summary> | ||
/// This example demonstrates how to enable cache when render document. | ||
/// </summary> | ||
class UseCacheWhenProcessingDocuments | ||
{ | ||
public static void Run() | ||
{ | ||
string outputDirectory = Constants.GetOutputDirectoryPath(); | ||
string cachePath = Path.Combine(outputDirectory, "cache"); | ||
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html"); | ||
|
||
FileCache cache = new FileCache(cachePath); | ||
ViewerSettings settings = new ViewerSettings(cache); | ||
|
||
using (Viewer viewer = new Viewer(Constants.SAMPLE_DOCX, settings)) | ||
{ | ||
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat); | ||
|
||
Stopwatch stopWatch = Stopwatch.StartNew(); | ||
viewer.View(options); | ||
stopWatch.Stop(); | ||
|
||
Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds); | ||
|
||
stopWatch.Restart(); | ||
viewer.View(options); | ||
stopWatch.Stop(); | ||
|
||
Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds); | ||
} | ||
|
||
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}."); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Examples/GroupDocs.Viewer.Examples.CSharp/AdvancedUsage/Loading/LoadDocumentsWithEncoding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using GroupDocs.Viewer.Options; | ||
|
||
namespace GroupDocs.Viewer.Examples.CSharp.AdvancedUsage.Loading | ||
{ | ||
/// <summary> | ||
/// This example demonstrates how to specify encoding. | ||
/// </summary> | ||
class LoadDocumentsWithEncoding | ||
{ | ||
public static void Run() | ||
{ | ||
string filePath = Constants.SAMPLE_TXT_SHIFT_JS_ENCODED; | ||
string outputDirectory = Constants.GetOutputDirectoryPath(); | ||
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html"); | ||
|
||
GroupDocs.Viewer.Common.Func<LoadOptions> loadOptions = () => | ||
new LoadOptions(FileType.TXT, null, Encoding.GetEncoding("shift_jis")); | ||
|
||
using (Viewer viewer = new Viewer(filePath, loadOptions)) | ||
{ | ||
HtmlViewOptions options = | ||
HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat); | ||
|
||
viewer.View(options); | ||
} | ||
|
||
Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}."); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.