viewer error.PNG (149.5 KB)
sharing my viewer for your reference, and also my code below
private LoadDocumentEntity GetDocumentPages(PostedDataEntity postedData, bool loadAllPages)
{
// get/set parameters
string documentGuid = GetDocumentPath(postedData.guid); //postedData.guid;
string password = string.IsNullOrEmpty(postedData.password) ? null : postedData.password;
var fileFolderName = Path.GetFileName(documentGuid).Replace(".", "_");
string fileCacheSubFolder = Path.Combine(cachePath, fileFolderName);
if (!File.Exists(documentGuid))
{
throw new GroupDocsViewerException("File not found.");
}
IViewerCache cache = new FileViewerCache(cachePath, fileCacheSubFolder);
LoadDocumentEntity loadDocumentEntity;
var htmlMode = string.IsNullOrEmpty(postedData.IsHTMLMode) ? "false" : postedData.IsHTMLMode;
if (htmlMode != "false")
{
using (HtmlViewer htmlViewer = new HtmlViewer(documentGuid, cache, GetLoadOptions(password)))
{
loadDocumentEntity = GetLoadDocumentEntity(loadAllPages, documentGuid, fileCacheSubFolder, htmlViewer, htmlMode);
}
}
else
{
using (PngViewer pngViewer = new PngViewer(documentGuid, cache, GetLoadOptions(password)))
{
loadDocumentEntity = GetLoadDocumentEntity(loadAllPages, documentGuid, fileCacheSubFolder, pngViewer, htmlMode);
}
}
return loadDocumentEntity;
}
public HtmlViewer(IndexedFileInfo fileInfo, IViewerCache cache, string password)
{
this.cache = cache;
sFileInfo = fileInfo;
Directory.CreateDirectory(sFileInfo.FileCacheFolderPath);
var loadOptions = new LoadOptions
{
Password = password,
};
viewer = new GroupDocs.Viewer.Viewer(sFileInfo.FilePath, loadOptions);
viewOptions = CreateHtmlViewOptions();
}
private HtmlViewOptions CreateHtmlViewOptions(int passedPageNumber = -1, int newAngle = 0)
{
HtmlViewOptions htmlViewOptions = HtmlViewOptions.ForEmbeddedResources(
pageNumber =>
{
string fileName = $"p{pageNumber}.html";
string cacheFilePath = this.cache.GetCacheFilePath(fileName);
return File.Create(cacheFilePath);
});
htmlViewOptions.SpreadsheetOptions.TextOverflowMode = TextOverflowMode.HideText;
htmlViewOptions.SpreadsheetOptions.SkipEmptyColumns = true;
htmlViewOptions.SpreadsheetOptions.SkipEmptyRows = true;
SetWatermarkOptions(htmlViewOptions);
if (passedPageNumber >= 0 && newAngle != 0)
{
Rotation rotationAngle = GetRotationByAngle(newAngle);
htmlViewOptions.RotatePage(passedPageNumber, rotationAngle);
}
return htmlViewOptions;
}
private static void SetWatermarkOptions(ViewOptions options)
{
Watermark watermark = null;
if (!string.IsNullOrEmpty(globalConfiguration.GetViewerConfiguration().GetWatermarkText()))
{
GroupDocs.Viewer.Drawing.Argb32Color argb32 = new GroupDocs.Viewer.Drawing.Argb32Color();
// Set watermark properties
watermark = new Watermark(globalConfiguration.GetViewerConfiguration().GetWatermarkText())
{
//Color = System.Drawing.Color.Blue,
Color = argb32,
Position = Position.Diagonal,
};
}
if (watermark != null)
{
options.Watermark = watermark;
}
}
private static Rotation GetRotationByAngle(int newAngle)
{
switch (newAngle)
{
case 90:
return Rotation.On90Degree;
case 180:
return Rotation.On180Degree;
case 270:
return Rotation.On270Degree;
default:
return Rotation.On90Degree;
}
}
private static LoadDocumentEntity GetLoadDocumentEntity(bool loadAllPages, string documentGuid, string fileCacheSubFolder, ICustomViewer customViewer, string htmlMode = "")
{
if (loadAllPages)
{
customViewer.CreateCache();
}
dynamic viewInfo = GetviewInfo(htmlMode, customViewer);// customViewer.GetViewer().GetViewInfo(ViewInfoOptions.ForHtmlView());
LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
if (!Directory.Exists(cachePath))
{
Directory.CreateDirectory(cachePath);
}
TryCreatePagesInfoXml(fileCacheSubFolder, viewInfo, out string pagesInfoPath);
for (var i = 0; i < viewInfo.Pages.Count; i++)
{
var page = viewInfo.Pages[i];
PageDescriptionEntity pageData = new PageDescriptionEntity
{
Number = i + 1,
//Angle = (int)(page.GetType().GetProperty("Angle")?.GetValue(page, null) as int?), // Adjust type as needed
Width = (int)(page.GetType().GetProperty("Width")?.GetValue(page, null) as int?), // Adjust type as needed
Height = (int)(page.GetType().GetProperty("Height")?.GetValue(page, null) as int?) // Adjust type as needed
};
loadDocumentEntity.SetPages(pageData);
}
loadDocumentEntity.SetGuid(documentGuid);
return loadDocumentEntity;
}
private ViewInfo GetViewInfo()
{
string cacheKey = "view_info.dat";
if (!this.cache.Contains(cacheKey))
{
using (new CrossProcessLock(this.filePath))
{
if (!this.cache.Contains(cacheKey))
{
return this.cache.GetValue(cacheKey, () => this.ReadViewInfo());
}
}
}
return this.cache.GetValue<ViewInfo>(cacheKey);
}
private static dynamic GetviewInfo(string isHtmlMode, ICustomViewer customViewer)
{
if (isHtmlMode != "false")
{
return customViewer.GetViewer().GetViewInfo(ViewInfoOptions.ForHtmlView());
}
else
{
return customViewer.GetViewer().GetViewInfo(ViewInfoOptions.ForPngView(false));
}
}
private static void TryCreatePagesInfoXml(string fileCacheSubFolder, dynamic viewInfo, out string pagesInfoPath)
{
if (!Directory.Exists(fileCacheSubFolder))
{
Directory.CreateDirectory(fileCacheSubFolder);
}
pagesInfoPath = Path.Combine(fileCacheSubFolder, "PagesInfo.xml");
if (!File.Exists(pagesInfoPath))
{
var xdoc = new XDocument(new XElement("Pages"));
foreach (var page in viewInfo.Pages)
{
xdoc.Element("Pages")
.Add(new XElement(
"PageData",
new XElement("Number", page.Number),
new XElement("Angle", 0)));
}
xdoc.Save(pagesInfoPath);
}
}