Hi
I put my hope to anyone experienced in the PdfSharp class library or perhaps can find an answer anyway. Please provide some code example, I have tried many variations and have already seen most of the existing errors.

I have some problems adding new pages. I want to add new pages only if needed and measured by
the XParagraph (text) length. If the length doesnt fit on one page, a new page must be added.
Also the header and footer need to be added to every new page.
It is probably easy to solve if I know how to do that, but I dont.

I only use the PdfSharp library, not MigraDoc.

Here is some sample code I have been working with....

protected void btnCreate_Click(object sender, EventArgs e)
{
PdfPage pdfPage = new PdfPage();
pdfPage.Orientation = PdfSharp.PageOrientation.Portrait;
pdfPage.Size = PdfSharp.PageSize.A4;

PdfDocument pdfDocument = new PdfDocument();
pdfDocument.Pages.Add(pdfPage);
pdfDocument.PageLayout = PdfPageLayout.SinglePage;
pdfDocument.PageMode = PdfPageMode.UseOutlines;

// Strängvärden från textboxar
header = TextBox_rubrik.Text.ToString();
text = TextBox_text.Text;
footer = TextBox_fot.Text;
fileName = TextBox_filename.Text.ToString();
linkText = TextBox_linktext.Text;

XFont fontHeader = new XFont("Arial", 16, XFontStyle.Bold);
XFont fontText = new XFont("Arial", 11, XFontStyle.Regular);
XFont fontFot = new XFont("Arial", 10, XFontStyle.Regular);

XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
XTextFormatter tf = new XTextFormatter(gfx);
// Position av ruta XRect: indrag vänstermarginal, indrag toppmarginal, bredd, höjd
XRect rect = new XRect();

int pageCount = pdfDocument.PageCount;
for (int idx = 0; idx < pageCount; idx++)
{
PdfPage page = pdfDocument.AddPage();
}

rect = new XRect(40, 40, 505, 60);
tf.Alignment = XParagraphAlignment.Center;
tf.DrawString(header, fontHeader, XBrushes.Black, rect, XStringFormats.TopLeft);
tf.DrawString(header, fontHeader, XBrushes.Black, rect, XStringFormats.TopLeft);

rect = new XRect(40, 80, 505, 682);
tf.Alignment = XParagraphAlignment.Left;
tf.DrawString(text, fontText, XBrushes.Black, rect, XStringFormats.TopLeft);

rect = new XRect(40, 802, 505, 40);
tf.Alignment = XParagraphAlignment.Center;
tf.DrawString(footer, fontFot, XBrushes.Black, rect, XStringFormats.TopLeft);

string fileFolder = Server.MapPath("~/document_folder/");

fileName = fileName + ".pdf";
pdfDocument.Save(fileFolder + fileName);

// print link on ASP.NET page
fileLink.InnerHtml += "<a href='document_folder/" + fileName + "'>" + linkText + "</a><br />";

pdfDocument.Close();
}

thanks in advance

If I have understood correctly, you are wanting to perform a paragraph split when it exceeds the page length? I don't have a direct answer for you and these pdf libraries/utilities generally have a learning curve associated with their lack of documentation, etc. However, you might want to consider alternatives to PdfSharp, depending on what you are doing. If you haven't checked out iTextSharp yet, you might want too. With the following code snippet, I produced an 8 page pdf (horizontal layout) as a result of a long paragraph. There are also header and footer options.

Code snippet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using PdfSharp.Pdf;
//using PdfSharp.Drawing;
//using PdfSharp.Drawing.Layout;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace ForumSolutions
{
    public class AcrobatReaderStuff
    {
        public static void Test_iTextSharp()
        {
            Document myDocument;

            myDocument = new Document(PageSize.A4.Rotate());

            PdfWriter.GetInstance(myDocument, new FileStream("c:\\Test_iTextSharp.pdf", FileMode.Create));

            myDocument.Open();

            string tmp = "";
            for (int i = 0; i < 1000; i++)
                tmp += " Here is some more text (" + i + ")... ";

            myDocument.Add(new Paragraph(tmp));

            myDocument.Close();
        }
    }
}

And, here is the Document class definition:

public class Document : IDocListener, IElementListener
    {
        protected int chapternumber;
        protected bool close;
        public static bool Compress;
        protected HeaderFooter footer;
        protected HeaderFooter header;
        protected string htmlStyleClass;
        protected string javaScript_onLoad;
        protected string javaScript_onUnLoad;
        protected float marginBottom;
        protected float marginLeft;
        protected bool marginMirroring;
        protected bool marginMirroringTopBottom;
        protected float marginRight;
        protected float marginTop;
        protected bool open;
        protected int pageN;
        protected Rectangle pageSize;
        public static float WmfFontCorrection;

        public Document();
        public Document(Rectangle pageSize);
        public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom);

        public float Bottom { get; }
        public float BottomMargin { get; }
        public virtual HeaderFooter Footer { set; }
        public virtual HeaderFooter Header { set; }
        public string HtmlStyleClass { get; set; }
        public string JavaScript_onLoad { get; set; }
        public string JavaScript_onUnLoad { get; set; }
        public float Left { get; }
        public float LeftMargin { get; }
        public virtual int PageCount { set; }
        public int PageNumber { get; }
        public Rectangle PageSize { get; }
        public static string Product { get; }
        public static string Release { get; }
        public float Right { get; }
        public float RightMargin { get; }
        public float Top { get; }
        public float TopMargin { get; }
        public static string Version { get; }

        public virtual bool Add(IElement element);
        public bool AddAuthor(string author);
        public bool AddCreationDate();
        public bool AddCreator(string creator);
        public void AddDocListener(IDocListener listener);
        public bool AddHeader(string name, string content);
        public bool AddKeywords(string keywords);
        public bool AddProducer();
        public bool AddSubject(string subject);
        public bool AddTitle(string title);
        public virtual void Close();
        public float GetBottom(float margin);
        public float GetLeft(float margin);
        public float GetRight(float margin);
        public float GetTop(float margin);
        public bool IsMarginMirroring();
        public bool IsOpen();
        public virtual bool NewPage();
        public virtual void Open();
        public void RemoveIDocListener(IDocListener listener);
        public virtual void ResetFooter();
        public virtual void ResetHeader();
        public virtual void ResetPageCount();
        public virtual bool SetMarginMirroring(bool marginMirroring);
        public virtual bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom);
        public virtual bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom);
        public virtual bool SetPageSize(Rectangle pageSize);
    }

Link to iTextSharp library and tutorial on SourceForge.net...

Though I did not look into it, there is a tutorial/project for this library's usage at: iTextSharp tutorial project...


I don't know if this interests you or not, and I hope that someone will provide a solution to your particular usage of pdfsharp. In any case, I hope that when you find a final solution that you will post the relevant code here so others can benefit from it.

Cheers!

commented: Very kind, clear and helpful in his comments +2

Hi and thanks for your answer.

Yes you are correct, I want to split the paragraph if needed and append the text on a new page, also create new pages while necessary.

Yes I have looked into the iTextSharp library as well, similar to PdfSharp at first look. And yes there is a severe lack of documentation;-).

I will check out and test the code you provided and get back here soon

best regards

Hi

Your example was very useful. I just replaced the static text, and the loop with a textbox input and it all works well, new pages are added while needed. Also with less code than the Pdfsharp example. Probably it is possible to make similar operations with the PdfSharp class, but I just can't figure it out.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.