Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Expand description

A DOCX file opened and laid out once for a target: its display list, which an output draws or writes, the places of its lines, and the text blocks of its body. It holds no state of the parse or the layout: what the layout left out or drew in another way comes out of Document::open beside it.

Implementations§

Source§

impl Document

Source

pub fn to_plain_text(&self) -> String

Exports the body text as UTF-8 plain text (paragraphs separated by blank lines, tables as tab-separated rows).

Source

pub fn to_markdown(&self) -> String

Exports the body text as UTF-8 Markdown (ATX headings, paragraphs, external hyperlinks, and GitHub-flavored pipe tables).

Source§

impl Document

Source

pub fn begin_open_prepared( prepared: PreparedDocument, fonts: &FontDb, art: &ArtPictures, ) -> Result<(Warned<Self>, Option<LayoutContinuation>, LayoutProgress), Error>

Begins opening a PreparedDocument incrementally, returning a drawable Document as soon as Page 1 is ready along with an optional LayoutContinuation if more pages or a second pass remain.

§Errors

Returns an error if layout fails.

Source§

impl Document

Source

pub fn open( docx: &[u8], fonts: &FontDb, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX file and lays it out into pages for the target of options, with the warnings of what it left out or drew in another way. The layout runs once, here.

§Errors

Returns an error when the file is not a readable DOCX package or its content cannot be laid out, or when the configuration or the target’s profile does not read.

Source

pub fn open_with_art( docx: &[u8], fonts: &FontDb, art: &ArtPictures, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX file as Document::open does, with the pictures of the art page borders art, which the caller adds beside the fonts (ArtPictures::add). A page border’s side of an art style whose picture art lacks is not drawn, with a warning.

§Errors

As Document::open.

Source

pub fn open_file( path: &Path, fonts: &FontDb, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX file from disk and lays it out into pages for the target of options, reading only the ZIP central directory and the parts needed for layout without loading the entire file into memory.

§Errors

Returns an error when the file cannot be opened as a DOCX package, its content cannot be laid out, or the configuration or target profile does not read.

Source

pub fn open_file_with_art( path: &Path, fonts: &FontDb, art: &ArtPictures, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX file from disk as Document::open_file does, with art page-border pictures art.

§Errors

As Document::open_file.

Source

pub fn open_source( source: Arc<dyn RandomAccessRead>, fonts: &FontDb, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX package from a caller-provided random-access byte source (such as a browser File.slice() or OPFS sync handle) without copying the whole archive into memory.

§Errors

As Document::open.

Source

pub fn open_source_with_art( source: Arc<dyn RandomAccessRead>, fonts: &FontDb, art: &ArtPictures, options: &Options, ) -> Result<Warned<Self>, Error>

Opens a DOCX package from a random-access byte source as Document::open_source does, with art page-border pictures art.

§Errors

As Document::open.

Source

pub fn display_list(&self) -> &DisplayList

The display list of the pages, the only border between the layout and an output.

Source

pub fn fidelity(&self) -> &FidelityReport

Typed rendering fidelity summary derived from layout font records and warnings.

Source

pub fn pages(&self) -> &[Page]

The pages in order: the pages of the display list (Document::display_list).

Source

pub fn page_count(&self) -> usize

The number of pages.

Source

pub fn page(&self, index: usize) -> Option<&Page>

The page at index, counted from 0, or none past the last page.

Source

pub fn page_size(&self, index: usize) -> Option<(Length, Length)>

The width and the height of the page at index, counted from 0, without drawing it, or none past the last page.

Source

pub fn text_blocks(&self) -> &[TextBlock]

The paragraphs and tables of the body in the order of the document, each with its address, which counts from 1, its kind with its style and text, and the indices of the pages that its lines lie on, which count from 0 as Document::page does. The pages are those of this document’s layout, so of the configuration that it was opened with (Options::configuration): the same file can give other pages for another target. The paragraphs of a table’s cells follow the table. Headers, footers, notes and text boxes are not text blocks.

Source

pub fn text_map(&self, index: usize) -> Vec<TextLine>

The text map of the page at index, counted from 0 (Text map): its lines in reading order, the header first, then the body, the endnotes, the frames, the text boxes, the footnotes and the footer. Each line names its story and its paragraph in Document::story_text, with its range of the paragraph’s text, its line box and its clusters in visual order with their edges on the page. It is empty past the last page.

Source

pub fn text_maps(&self, indices: &[usize]) -> Vec<Vec<TextLine>>

The text maps of the pages at indices, each as Document::text_map gives it, from one read of each story on them: a viewer that needs many pages, such as for a search, reads the body once and not once for each page.

Source

pub fn story_text(&self, story: TextStory) -> Vec<StoryParagraph>

The text of story by paragraph (Story text): the texts of the paragraph’s laid out lines joined, so that a field gives its shown result, a list label is given as drawn, hidden text is left out, and an automatic hyphen at a wrap is not in it.

Source

pub fn write_pdf(&self, fonts: &FontDb) -> Result<Warned<Vec<u8>>, Error>

Writes the pages as a PDF file, with the fonts subset and embedded, and with the warnings of the PDF writer.

Each page becomes one PDF page of the same size, with every glyph at its laid out position and with its text, so that a viewer can search and copy it. The same document and fonts always give the same bytes.

§Errors

Returns an error when the pages cannot be written as PDF.

Source

pub fn write_pdf_to<W: Write>( &self, fonts: &FontDb, writer: &mut W, ) -> Result<Warned<()>, Error>

Streams the pages directly as a PDF 1.7 document into writer without buffering the complete PDF bytes in memory.

§Errors

Returns an error when the pages cannot be written as PDF or when writing to writer fails.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.