christiaanderidder / QuestPDF.Markdown

QuestPDF.Markdown allows rendering markdown into a QuestPDF document
MIT License
29 stars 7 forks source link

Justify text #83

Closed supersume closed 6 days ago

supersume commented 1 month ago

How can one render markdown text that is justified?

christiaanderidder commented 1 month ago

Hi @supersume

Markdown does not have a native way of configuring text alignment (For example, see https://stackoverflow.com/questions/14051715/markdown-native-text-alignment).

The most common workaround for this is using HTML + CSS.

QuestPDF.Markdown aims to convert pure markdown to PDF and not to become a HTML to PDF converter with all the issues that will bring.

Therefore, currently no syntax besides markdown is supported. Under the hood markdig is used, which does have some HTML support, but I have not looked into using it to support alignment styles. So unless someone is willing to create a PR with a PoC this will likely not be added.

nemec784 commented 1 week ago

Hi, @christiaanderidder! Thank you for your work on QuestPDF.Markdown!

Could we leverage QuestPDF’s existing capabilities for this? text.Justify() could be called if a flag is set in MarkdownRendererOptions.

Here’s a sample of how these changes could be applied:


--- a/src/QuestPDF.Markdown/MarkdownRenderer.cs
+++ b/src/QuestPDF.Markdown/MarkdownRenderer.cs
@@ -191,6 +191,10 @@ internal sealed class MarkdownRenderer : IComponent
 {
     pdf.Text(text =>
     {
+        if (_options.JustifyText)
+        {
+            text.Justify();
+        }

         // Process the block's inline elements
         foreach (var item in block.Inline)
--- a/src/QuestPDF.Markdown/MarkdownRendererOptions.cs
+++ b/src/QuestPDF.Markdown/MarkdownRendererOptions.cs
@@ -41,6 +41,8 @@ public class MarkdownRendererOptions
     public float ImageScalingFactor { get; set; } = 0.5f;

+    public bool JustifyText { get; set; }
+
     public float ParagraphSpacing { get; set; } = 10;
     public float ListItemSpacing { get; set; } = 5;
     public string UnorderedListGlyph { get; set; } = "•";
christiaanderidder commented 6 days ago

@nemec784 I've done some experimenting with this and this can indeed be done, but it would set the alignment for every single piece of text in the document. I would expect that in most cases more finely grained control is needed, which cannot really be done without having some sort of markdown syntax to indicate alignment (and without introducing HTML into the mix).

@supersume would setting the text align for the entire document, without any additional control, be workable for your use case?

supersume commented 6 days ago

Yes. Setting alignment for the entire document is an acceptable solution

christiaanderidder commented 6 days ago

Version 1.24.0 should become available shortly and will include a global paragraph alignment setting.

nemec784 commented 6 days ago

@christiaanderidder, wow! Thanks a lot!