An ePub parser for the Universal Windows Platform (UWP). Also has some basic controls to demonstrate displaying ebooks.
This details basic usage of the core Papyrus parser, the Papyrus HTML parser and the Papyrus UI controls.
var targetFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Dawn of Wonder - Jonathan Renshaw");
using (var stream = await ePubFile.OpenStreamForReadAsync())
{
var archive = new ZipArchive(stream);
archive.ExtractToDirectory(targetFolder.Path);
}
var eBook = new EBook(targetFolder);
await eBook.InitializeAsync();
This part is optional. At this point you can get HTML content from your eBook. Papyrus HTML Parser allows you to get a collection of Block
elements which can be displayed in a RichTextBlock
, representing the HTML. It makes for a much cleaner layout, but could potentially misrepresent the actual layout as displayed in HTML.
The EBook.GetContentsAsync()
method has three different signatures. It can take a NavPoint
, a SpineItem
or a ManifestItem
. This demo will use a SpineItem
.
var html = await eBook.GetContentsAsync(spineItem);
Getting the CSS styles is optional, but will help to display the UI more closely to how it is displayed in HTML. You may use whatever method you wish to get the CSS content, now that you can parse the location of the stylesheets from the HTML. The EBook class does have an extension method which takes an absolute path to a file inside its folder and returns the file. E.g. Book.GetFileAsync("C:/Path/To/The/EBook/styles/styles.css");
I intend to build the CSS loading functionality right into Papyrus in the future. If you opt not to get the CSS, just pass an empty string to the parser.
var converter = new Converter();
converter.Convert(html, css);
You now have access to a collection of Block
elements. (converter.ConvertedBlocks
)
You're now ready to display your content. Papyrus has two controls to make this easier: TableOfContents
and Parchment
.
xmlns:papyrus="using:Papyrus.UI"
<papyrus:TableOfContents x:Name="TableOfContents"
Parchment="{x:Bind MainParchment, Mode=OneWay}"
Source={x:Bind MyEBook.TableOfContents, Mode=OneWay}">
<papyrus:TableOfContents.Header>
<TextBlock FontSize="16"
FontWeight="SemiBold"
Margin="12"
Text="Table of contents" />
</papyrus:TableOfContents.Header>
</papyrus:TableOfContents>
We provided a Parchment
to the Parchment
property. This allows the TableOfContents
to take care of loading the correct content into the Parchment
for us. If you can't or don't want to do this, you can tap into the SelectionChanged
event of the TableOfContents
instead, and take care of loading it yourself.
xmlns:papyrus="using:Papyrus.UI"
<papyrus:Parchment x:Name="MainParchment"
Background="White"
Padding="24"
ParagraphIndentation="24"
Source="{x:Bind MyEBook, Mode=OneWay}" />
You're all set! you should now be able to display your eBook's content. For more advanced scenarios, check out the available properties and methods below.
Components in the Papyrus
library.
Properties and methods in the EBook
class.
ContentLocation (String): A relative path to the content.opf
file.
Cover (ImageSource): An ImageSource
object with the eBook cover.
Manifest (Dictionary<string, ManifestItem>): The Manifest
object for this eBook.
Metadata (Metadata): The Metadata
object for this eBook.
RootPath (String): An absolute path to the eBook's root folder.
Spine (Spine): The Spine
object for this eBook.
TableOfContents (TableOfContents): The TableOfContents
object for this eBook.
InitializeAsync(): Initializes the EBook
, verifying the mimetype and loading the Metadata, Manifest
, Spine, TableOfContents and Cover
.
GetContentsAsync(ManifestItem|SpineItem|NavPoint, bool embedImages = true): An extension method which gets HTML content for a resource. If embedImages
is true
, images will be embedded with base64-encoded sources.
GetFileAsync(string path): An extension method which takes an absolute path to a file which is in the eBook's root folder and returns the file.
GetSpineItem(ManifestItem item): Gets a SpineItem which matches the ManifestItem.
Properties and methods in the ManifestItem
class.
ContentLocation (string): A relative path to the content to which this ManifestItem
points.
Id (string): The identifier for this ManifestItem
.
MediaType (string): The type of media to which this ManifestItem
points.
Properties and methods in the Metadata
class.
AlternativeTitle (string): An alternative name for the resource.
Available (DateTime): Date that the resource became or will become available.
Audience (string): A class of entity for whom the resource is intended or useful.
Contributor (string): An entity responsible for making contributions to the resource.
Created (DateTime): Date of creation of the resource.
Creator (string): An entity primarily responsible for making the resource. This will be the author.
Date (DateTime): Publication date.
Description (string): A description of the eBook.
Language (string): A language code for the eBook.
Title (string): The title of the eBook.
Properties and methods in the NavPoint
class.
ContentPath (string): A relative path to the content to which this NavPoint
points.
Id (string): The identifier for this NavPoint
.
Items (ObservableCollection<NavPoint>): Sub-items for this NavPoint
.
Level (Int32): The nesting level of this NavPoint
.
PlayOrder (Int32): The order in which this NavPoint
should appear.
Text (string): The text to display for this NavPoint
.
Properties and methods in the Spine
class.
Toc (string): The Id of the ManifestItem which represents the table of contents for this Spine
.
Next(SpineItem item): An extension method which gets the next item in the spine. (spine.Next(item)
)
Previous(SpineItem item): An extension method which gets the previous item in the spine. (spine.Previous(item)
)
Properties in the SpineItem
class
IdRef (string): The Id of the ManifestItem to which this SpineItem
points.
Properties in the TableOfContents
class
FlatItems (IEnumerable<NavPoint>): A flattened list of the items in this TableOfContents
.
Items (ObservableCollection<NavPoint>): The items in this TableOfContents
.
Title (string): The title of this TableOfContents
.