Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PdfSharp: dynamic generating document

I got the following code for simply adding two textboxes-contents into a pdf-file:

using System;
using System.Windows.Forms;
using PdfSharp.Pdf;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;

namespace pdfDynamic
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Creating the document
            Document document = new Document();
            Section section = document.AddSection();

            //Adding the first paragraph
            section.AddParagraph(richTextBox1.Text);

            //Adding the second paragraph
            section.AddParagraph(richTextBox2.Text);

            //Creating the document
            PdfDocumentRenderer renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
            renderer.Document = document;
            renderer.RenderDocument();
            string pdfFilename = string.Format("Rekla-{0:dd.MM.yyyy_hh-mm-ss}.pdf", DateTime.Now);
            renderer.PdfDocument.Save(pdfFilename);

        }
    }
}

How can i detect if the second paragraph shows up from the first page to the second page? In this case i want to put the second paragraph on the second page only.

My english is not the best. Maybe my "paint-skills" are a better help to describe the problem: current and desired situation

like image 471
Basssprosse Avatar asked Jun 29 '26 15:06

Basssprosse


1 Answers

Try creating a Paragraph and use the KeepTogether

Paragraph p;
p.Format.KeepTogether = true;
p.AddFormattedText(richTextBox2.Text);
like image 189
Jack Morton Avatar answered Jul 02 '26 04:07

Jack Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!