PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Feb 16, 2019 9:19 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Jan 24, 2019 3:50 pm 
Offline

Joined: Thu Jan 24, 2019 3:06 pm
Posts: 2
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 24, 2019 4:19 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2908
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 28, 2019 9:57 am 
Offline

Joined: Thu Jan 24, 2019 3:06 pm
Posts: 2
Hi Thomas,

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

Many thanks


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 28, 2019 11:15 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2908
Location: Cologne, Germany
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/

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group