Skip to content

Commit

Permalink
Examples for GroupDocs.Viewer for .NET 19.8
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-litvinchik committed Aug 10, 2019
1 parent b9ccbd3 commit 385a878
Show file tree
Hide file tree
Showing 385 changed files with 3,010 additions and 74,233 deletions.
39 changes: 32 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bld/
[Bb]in/
[Oo]bj/

# Visual Studo 2015 cache/options directory
# Visual Studio 2015 cache/options directory
.vs/

# MSTest test Results
Expand All @@ -38,6 +38,10 @@ TestResult.xml
[Rr]eleasePS/
dlldata.c

# DNX
project.lock.json
artifacts/

*_i.c
*_p.c
*_i.h
Expand Down Expand Up @@ -90,7 +94,7 @@ _ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# JustCode is a .NET coding addin-in
# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
Expand Down Expand Up @@ -129,9 +133,11 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
## TODO: Comment the next line if you want to checkin your
## web deploy settings but do note that will include unencrypted
## passwords
#*.pubxml

*.publishproj

# NuGet Packages
Expand All @@ -150,8 +156,13 @@ csx/
# Windows Store app package directory
AppPackages/

# Others
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Others
ClientBin/
[Ss]tyle[Cc]op.*
~$*
Expand All @@ -161,7 +172,7 @@ ClientBin/
*.pfx
*.publishsettings
node_modules/
bower_components/
orleans.codegen.cs

# RIA/Silverlight projects
Generated_Code/
Expand Down Expand Up @@ -194,3 +205,17 @@ FakesAssemblies/

# Visual Studio 6 workspace options file
*.opt

# LightSwitch generated files
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml

#VSCode
.vscode/

#NuGet
*.nuspec

#Batch
*.bat
4 changes: 4 additions & 0 deletions Examples/.gitignore
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 removed Examples/Data/SampleFiles/RunProgramCS.png
Binary file not shown.
Binary file removed Examples/Data/SampleFiles/UnzippedFolder.png
Binary file not shown.
Binary file removed Examples/Data/Storage/password-protected.zip
Binary file not shown.
Binary file removed Examples/Data/Storage/sample.cdr
Binary file not shown.
Binary file removed Examples/Data/Storage/sample.ost
Binary file not shown.
Binary file removed Examples/Data/Storage/sample.pptx
Binary file not shown.
Binary file removed Examples/Data/Storage/sample.xlsx
Binary file not shown.
Binary file removed Examples/Data/Storage/sample.zip
Binary file not shown.
Binary file removed Examples/Data/Storage/sample2.cdr
Binary file not shown.
Binary file removed Examples/Data/Storage/spreadsheet.xlsx
Binary file not shown.
Binary file removed Examples/Data/Storage/word.doc
Binary file not shown.
Binary file not shown.
Binary file removed Examples/Data/screenshots/Cells Viewing.PNG
Binary file not shown.
Binary file removed Examples/Data/screenshots/PDF Rendering 16.11.PNG
Binary file not shown.
Binary file removed Examples/Data/screenshots/PDF Viewing.PNG
Binary file not shown.
Binary file removed Examples/Data/screenshots/Slides Rendering 16.11.PNG
Binary file not shown.
Binary file removed Examples/Data/screenshots/Slides Viewing.PNG
Binary file not shown.
Binary file not shown.
Binary file removed Examples/Data/screenshots/Word Viewing.PNG
Binary file not shown.
Binary file removed Examples/Data/screenshots/arch.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/licensekey.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/presentation.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/reorder.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/rotation.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/viewer.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/watermark.png
Binary file not shown.
Binary file removed Examples/Data/screenshots/watermark1.png
Binary file not shown.
29 changes: 0 additions & 29 deletions Examples/GroupDocs.Viewer.Examples.CSharp.sln

This file was deleted.

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;
}
}
}
}
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}.");
}
}
}
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}.");
}
}
}

Loading

0 comments on commit 385a878

Please sign in to comment.