![]() |
VOOZH | about |
dotnet add package DigitalPublications.Episerver --version 1.0.9
NuGet\Install-Package DigitalPublications.Episerver -Version 1.0.9
<PackageReference Include="DigitalPublications.Episerver" Version="1.0.9" />
<PackageVersion Include="DigitalPublications.Episerver" Version="1.0.9" />Directory.Packages.props
<PackageReference Include="DigitalPublications.Episerver" />Project file
paket add DigitalPublications.Episerver --version 1.0.9
#r "nuget: DigitalPublications.Episerver, 1.0.9"
#:package DigitalPublications.Episerver@1.0.9
#addin nuget:?package=DigitalPublications.Episerver&version=1.0.9Install as a Cake Addin
#tool nuget:?package=DigitalPublications.Episerver&version=1.0.9Install as a Cake Tool
Digital Publications is a .NET library for converting documents to JSON, HTML and PDF.
This package requires a valid license, which can be purchasable by contacting HiQ. The license should preferably be placed in the root folder of the project, and be named digipub_episerver_license.xml.
Below is an example of how to implement a basic backend and frontend in Episerver:
Example Backend:
public ActionResult Index(DigiPubTestPage currentPage, int page = 1)
{
var file = currentPage.Document;
var filePath = _urlResolver.GetUrl(file);
MediaData media;
byte[] blobBytes = { };
if (_contentLoader.TryGet(file, out media))
{
blobBytes = _blobFactory.GetBlob(media.BinaryData.ID).ReadAllBytes();
}
string licensePath = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), @"digipub_episerver_license.xml");
var DP = new DocumentParser(licensePath);
var parsedDocument = DP.Parse(filePath, blobBytes, "digipub", file.ID.ToString());
if (parsedDocument == null)
{
return View(new DigiPubTestPageViewModel(currentPage));
}
var sourcePath = "/digipub/" + file.ID;
var pdfPath = sourcePath + "/" + Path.GetFileNameWithoutExtension(filePath) + ".pdf";
var model = new DigiPubTestPageViewModel(currentPage)
{
ParsedDocument = parsedDocument,
SourcePath = sourcePath,
PDFPath = pdfPath
};
return View(model);
}
Example Frontend:
@using EpiserverTestSiteAlloy
@model DigiPubTestPageViewModel
@{
var tocPageCounter = 1;
}
@if (Model.ParsedDocument != null)
{
<div class="digipub TOC-enabled">
<div class="digipub-summary">
<div class="digipub-introduction">
<h1 class="digipub-introduction-heading">
Introduktionstext
</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="digipub-document-information">
<h2 class="digipub-document-information-heading">Dokumentinformation</h2>
<p>Typ av publikation: Goda råd</p>
<p>Publicerade: 2019</p>
<p>Avsändare: Försäkringsavdelningen</p>
<p>Antal sidor vid utskrift: 9</p>
<div class="digipub-document-information-buttons">
<a class="create-pdf" href="@Model.PDFPath" target="_blank">Skapa PDF</a>
<a class="print" href="javascript:var pdfWindow = window.open('@Html.Raw(Model.PDFPath)');pdfWindow.print();">Skriv ut</a>
</div>
</div>
</div>
<div class="digipub-content-grid">
<div class="digipub-table-of-contents">
<h2>Innehållsförteckning</h2>
@RenderTableOfContents(Model.ParsedDocument.ToC, tocPageCounter)
</div>
<div class="digipub-content">
@foreach (var element in Model.ParsedDocument.Elements)
{
if (element is BulletListElement)
{
var bulletListElement = (BulletListElement)element;
<ul style="list-style-type:disc">
@foreach (var item in bulletListElement.Items)
{
<li>
@RenderElement(item)
</li>
}
</ul>
}
else if (element is NumberListElement)
{
var numberListElement = (NumberListElement)element;
<ol type="1">
@foreach (var item in numberListElement.Items)
{
<li>
@RenderElement(item)
</li>
}
</ol>
}
else if (element is TableElement)
{
var tableElement = (TableElement)element;
if (tableElement.Rows == null)
{
break;
}
<table border="1">
@foreach (var row in tableElement.Rows)
{
<tr>
@foreach (var cell in row.Cells)
{
<td>
@RenderElement(cell)
</td>
}
</tr>
}
</table>
}
else
{
@RenderElement(element)
}
}
<div class="navigation-buttons">
<a class="navigation-button @(Model.Page <= 1 ? "disabled" : "")" id="btn-page-previous" @(Model.Page > 1 ? "href=?page=" + (Model.Page - 1) : "")>Föregående kapitel</a>
<a class="navigation-button @(Model.Page >= Model.TotalPages ? "disabled" : "")" id="btn-page-next" @(Model.Page < Model.TotalPages ? "href=?page=" + (Model.Page + 1) : "")>Nästa kapitel</a>
</div>
</div>
</div>
</div>
}
@helper RenderToCElement(ToCLevel element, int depthIndex, int tocPageCounter)
{
if (depthIndex > 3)
{
return;
}
foreach (var child in element.Children)
{
RenderToCElement(child, depthIndex + 1, tocPageCounter);
}
if (element.Name == "")
{
return;
}
if (depthIndex == 1 && element.Children.Count > 0)
{
<li id="table-of-contents-chapter-@element.Id" class="table-of-contents-chapter accordion @(tocPageCounter == Model.Page ? "digipub-accordion-active" : "")" aria-expanded="false">
<div class="accordion-title">
<a class="accordion-title-text" href="?page=@(tocPageCounter)#chapter-@(element.Id)">@element.Name</a>
<button class="toc-expand" type="button" aria-controls="table-of-contents-chapter-@(tocPageCounter).@depthIndex"></button>
</div>
<ul class="chapter-level-@depthIndex @(depthIndex == 1 ? "panel" : "")">
@foreach (var childElement in element.Children)
{
@RenderToCElement(childElement, depthIndex + 1, tocPageCounter)
}
</ul>
</li>
}
else
{
<li id="table-of-contents-chapter-@element.Id" class="table-of-contents-chapter">
<a class="accordion-title-text" href="?page=@(tocPageCounter)#chapter-@(element.Id)">@element.Name</a>
@if (element.Children.Count > 0)
{
<ul class="chapter-level-@depthIndex @(depthIndex == 1 ? "panel" : "")">
@foreach (var childElement in element.Children)
{
@RenderToCElement(childElement, depthIndex + 1, tocPageCounter)
}
</ul>
}
</li>
}
}
@helper RenderTableOfContents(ToCLevel ToC, int tocPageCounter)
{
<ul class="table-of-contents">
@foreach (var element in ToC.Children)
{
@RenderToCElement(element, 1, tocPageCounter);
tocPageCounter++;
}
</ul>
}
@helper RenderElement(DocumentElement element)
{
if (element is HeadingElement)
{
var headingElement = (HeadingElement)element;
var headingLevel = Math.Max(1, Math.Min(headingElement.Level, 6));
<a id="chapter-@element.Id" class="anchor"></a>
@Html.Raw(string.Format("<h{0} {1}>{2}</h{0}>", headingLevel, headingElement.Justification != null ? "class='justify-" + headingElement.Justification + "'" : "", headingElement.Text))
}
else if (element is ParagraphElement)
{
var paragraphElement = (ParagraphElement)element;
<p @Html.Raw(string.Format("{0}", paragraphElement.Justification != null ? "class='justify-" + paragraphElement.Justification + "'" : ""))>@(Html.Raw(paragraphElement.Text))</p>
}
else if (element is ImageElement)
{
var imageElement = (ImageElement)element;
<img src="@string.Format("{0}/{1}", Model.SourcePath, imageElement.Src)" width="@(imageElement.width)" height="@(imageElement.height)" />
}
else if (element is BlipElement)
{
var blipElement = (BlipElement)element;
<img src="@string.Format("{0}/{1}", Model.SourcePath, blipElement.Src)" width="@(blipElement.width)" height="@(blipElement.height)" />
}
}
<script src="~/Static/js/digipub.js"></script>
If you want to implement pagination in your view and split the document into pages based on top level headings, you can use the included function SplitDocElementsIntoPages, which takes the list of Elements as parameter.
Below is an example of Backend with Pagination:
public ActionResult Index(DigiPubTestPage currentPage, int page = 1)
{
var file = currentPage.Document;
var filePath = _urlResolver.GetUrl(file);
MediaData media;
byte[] blobBytes = { };
if (_contentLoader.TryGet(file, out media))
{
blobBytes = _blobFactory.GetBlob(media.BinaryData.ID).ReadAllBytes();
}
string licensePath = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), @"digipub_episerver_license.xml");
var DP = new DocumentParser(licensePath);
var parsedDocument = DP.Parse(filePath, blobBytes, "digipub", file.ID.ToString());
if (parsedDocument == null)
{
return View(new DigiPubTestPageViewModel(currentPage));
}
var pageList = DP.SplitDocElementsIntoPages(parsedDocument.Elements);
if (page > 0 && page <= pageList.Count)
parsedDocument.Elements = pageList[page - 1];
else
{
parsedDocument.Elements = pageList[0];
}
var sourcePath = "/digipub/" + file.ID;
var pdfPath = sourcePath + "/" + Path.GetFileNameWithoutExtension(filePath) + ".pdf";
var model = new DigiPubTestPageViewModel(currentPage)
{
PageList = pageList,
ParsedDocument = parsedDocument,
Page = page,
TotalPages = pageList.Count,
SourcePath = sourcePath,
PDFPath = pdfPath
};
return View(model);
}
To get going quickly with some styling, there are basic stylesheets and scripts you can use. These are not currently hosted anywhere however. Contact HiQ if you want these sent to you.
There is an attribute class included in the package called ValidateDigipubLicenseAttribute. Use this attribute on your ContentReference property for the document to be parsed to receive license errors in directly edit view. This notifies editors if the license expires or is invalid for some reason.
Example:
[ValidateDigipubLicense]
public virtual ContentReference Document { get; set; }
You can parse your document directly to a HTML string by calling the method ParseAsHtml. Note that this method is currently not supporting documents with images.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
Added support for footnotes per page if document has no H1