PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Line Break not working with paragraphs
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3905
Page 1 of 1

Author:  paulalton_utw [ Thu Jan 24, 2019 3:50 pm ]
Post subject:  Line Break not working with paragraphs

I'm new to PDFSharp / MigraDoc and have only just got the document generation working with some text.

However the text is on top of each other which I suspect is due to my line breaks not working properly.

This is the C# code:

Code:
public void InsertText(Didstopia.PDFSharp.Pdf.PdfDocument pdfDoc, string text, string fontName, MigraDoc.DocumentObjectModel.Color textColour,
            XStringFormat format, int fontSize = 10, bool fontBold = false, bool fontItalic = false)
        {
            Didstopia.PDFSharp.Pdf.PdfPage page = pdfDoc.Pages[0];
            Didstopia.PDFSharp.Drawing.XGraphics gfx = Didstopia.PDFSharp.Drawing.XGraphics.FromPdfPage(page);
           
            Document doc = new Document();  // (always need a MigraDoc document for rendering)

            InsertLineBreak(doc);

            Section sec = doc.AddSection();

            Paragraph para = sec.AddParagraph();
            para.Format.Alignment = ParagraphAlignment.Left;
            para.Format.Font.Name = fontName;
            para.Format.Font.Color = textColour;
            para.Format.Font.Bold = fontBold;
            para.Format.Font.Italic = fontItalic;
            para.Format.Font.Size = fontSize;
                       
            var lines = text.Split(new[] { "&%" }, StringSplitOptions.None);

            // Add line(s) of text
            for (var i = 0; i < lines.Length; i++)
            {
                para.AddText(lines[i].ToString());
            }
           
            // Create a renderer for document paragraph
            MigraDoc.Rendering.DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(doc);
            docRenderer.PrepareDocument();
            docRenderer.RenderObject(gfx, Didstopia.PDFSharp.Drawing.XUnit.FromCentimeter(2), Didstopia.PDFSharp.Drawing.XUnit.FromCentimeter(2), "12cm", para);

            gfx.Dispose();
        }

        private void InsertLineBreak(Document doc)
        {
            Section sec = doc.AddSection(); Paragraph p = sec.AddParagraph();
            p.Format.LineSpacingRule = LineSpacingRule.Exactly;
            p.Format.LineSpacing = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(12);
            p.Format.SpaceBefore = new Unit(5);
        }


I've also tried .AddLineBreak() to the paragraph but that also didn't work.

Am I missing some setting on the paragraph(s) or is this approach not correct?

Author:  Thomas Hoevel [ Thu Jan 24, 2019 4:19 pm ]
Post subject:  Re: Line Break not working with paragraphs

Hi!

I don't see any obvious mistakes.

You are using RenderObject.
The recommended (and well tested) approaches are using RenderPage or RenderDocument.

And your are not using the supported official version, you are using an unsupported port of our libraries. We do not know what that port does.

So maybe ask at the source:
https://github.com/Didstopia/PDFSharp/issues

Author:  paulalton_utw [ Mon Jan 28, 2019 9:57 am ]
Post subject:  Re: Line Break not working with paragraphs

Hi Thomas,

Do you have any sample code using the correct (supported) libraries that would exemplify what I'm trying to achieve?

Many thanks

Author:  Thomas Hoevel [ Mon Jan 28, 2019 11:15 am ]
Post subject:  Re: Line Break not working with paragraphs

Well, what are you trying to achieve?

Maybe start reading here:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

NuGet:
https://www.nuget.org/profiles/PDFsharp-Team

GitHub:
https://github.com/empira

Website:
http://www.pdfsharp.net/

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/