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?