Epub creation - ability to set page margins and fully justified pages

*** conversion code ***


Document doc = new Document(_sourceFilePath);
doc.Save(_destinationFilePath, SaveFormat.Epub);

during the creation of my epub. i would like to set page margins. is this possible via the Aspose apis. also can the text be justified in a similar manner as to how pages appear in books.

Hi

Thanks for your inquiry. To justify text in the document you should specify ParagraphAlignment of each paragraph like shown below:

Document doc = new Document(@"Test001\in.doc");
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach(Paragraph paragraph in paragraphs)
paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
doc.Save(@"Test001\out.epub");

There is an option to output page setup into HTML and EPUB, but it seems EPUB viewers just ignores these settings.

Document doc = new Document(@"Test001\in.doc");
HtmlSaveOptions opt = new HtmlSaveOptions(SaveFormat.Epub);
opt.ExportPageSetup = true;
doc.Save(@"Test001\out.epub", opt);

What EPUB viewer do you use?
Best regards.

primarily Barnes & Noble Desktop eReader but also Adobe Digital Editions

Do I need to follow this approach for setting Page Margins?

Thanks in advance.

Hi

Thank you for additional information. You can try using this option, but as I mentioned at least Adobe Digital Editions ignores page setup.
Most likely EPUB format does not support page margins at all.
Best regards.

actually margins are supported by the epub spec: http://www.idpf.org/doc_library/epub/ops_2.0.1_draft.htm

I notice in the html file in the epub (see file: generated epub.epub.zip) there are margins being set but not honoring the *** section.PageSetup.LeftMargin = ConvertUtil.InchToPoint(5); ***
see screenshot: html file in epub.JPG

I decide to do a test with just generating a stand alone html file (see file: generatedhtml.zip) and checked to see if the margins will be ConvertUtil.InchToPoint(5)
see screenshot: stand alone html file.JPG

I also generated a docx file and the 5 inch margin was honored(see file: generated docx.docx)

It looks like Aspose is hard coding the

margins to 0pt
Is there a way to prevent this?

My code to convert is below:

ParagraphResolver taken from https://forum.aspose.com/t/63225

Document doc = new Document(_sourceFilePath);
ParagraphResolver resolver = new ParagraphResolver();
doc.Accept(resolver);
doc.Range.Replace("\v", string.Empty, false, false);
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach(Paragraph paragraph in paragraphs)
{
    paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
}
foreach(Section section in doc.Sections)
{
    section.PageSetup.LeftMargin = ConvertUtil.InchToPoint(5);
}

// to generate docx file - generated docx.docx
doc.Save(_destinationFilePath, SaveFormat.Docx);

// to generate html file - generatedhtml.zip
HtmlSaveOptions opt = new HtmlSaveOptions();
opt.ExportPageSetup = true;
doc.Save(_destinationFilePath, opt);

// to generate epub file - generated epub.epub.zip
HtmlSaveOptions opt = new HtmlSaveOptions(SaveFormat.Epub);
opt.ExportPageSetup = true;
doc.Save(temp, opt);

Thanks in advance…

Hi

Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed.
Best regards,

Hello!
Thank you for detailed information.
I’m Viktor Sazhaev, an engineer responsible for EPUB export in Aspose.Words.
You are right. OPS specification does support page margins specified as @page at-rule in CSS. But we already output @page at-rules when HtmlSaveOptions.ExportPageSetup is set to true. You can open the underlying HTML in the attached file as text and see that at-rule. Seemingly Adobe Digital Editions (ADE) ignores it. The same result I’ve got with Calibre EPUB viewer. If you can provide a sample EPUB file that defines page margins and shows correctly in ADE please feel free to share. Maybe we do anything wrong with page setup.
Page margins and paragraph margins are different things. You can set paragraph margins, both left and right, as a workaround for this case as my colleague shown. Also you can specify paragraph margins in the Normal style. They will be inherited by all document styles and paragraphs that don’t override them. Aspose.Words doesn’t hardcode zero paragraph margins. If you request embedded or external CSS and compare with Microsoft Word’s output you’ll see the reason. By default, HTML visual agents apply some built-in CSS rules including margins. To make output independent on them application have to output those margins. If styles in your document define non-zero paragraph margins you’ll see them in the output.
Best regards,