I've begun to study MigraDoc combined with PdfSharp, and the results are better. Not exactly what I expected yet, since it becomes very unfunctional to create two different instance of a PDF document in order to be able to create one. One for creating pages, saving, etc. and another for rendering. Not quite friendly, if I am allowed to say so.
What I expect and tend to bring to my programmers are ways to get more and more functional. Functional programming is according to me the best way to get more productive. For instance:
Code:
public static class FacadePdfHelper {
private static readonly PdfHelperFactory _pdfHelperFactory = new PdfHelperFactory();
public static PdfDocument WorkingDocument {
get {
return _pdfHelperFactory.WorkingDocument;
}
}
public static PdfPage AddPage() {
return AddPage(PageSize.Letter, PageOrientation.Portrait);
}
public static PdfPage AddPage(PageSize size, PageOrientation orientation) {
return AppPage(WorkingDocument, size, orientation);
}
public static PdfPage AddPage(PdfDocument pdfDoc, PageSize size, PageOrientation orientation) {
return _pdfHelperFactory.AddPage(pdfDoc, size, orientation);
}
public static void Write(string text) {
Write(text, 0, 0);
}
public static void Write(string text, double xPos, double yPos) {
Write(text, xPos, yPos, new XFont("Helvetica", 12));
}
public static void Write(string text, double xPos, double yPos, XFont font) {
Write(text, xPos, yPos, font, XFontStyle.Regular);
}
public static void Write(string text, double xPos, double yPos, XFont font, XFontStyle fontStyle, params int[] pages) {
_pdfHelperFactory.Write(text, xPos, yPos, font, fontStyle, pages);
}
} // End of class FacadePdfHelper.
internal class PdfHelperFactory {
internal PdfHelperFactory() {
WorkingDocument = new PdfDocument();
}
internal PdfDocument WokringDocument { get; private set; }
internal PdfPage AddPage(PdfDocument pdfDoc, PageSize size, PageOrientation orientation) {
if(pdfDoc == null)
pdfDoc = new PdfDocument();
var newPdfPage = new PdfPage();
newPdfPage.Size = size;
newPdfPage.Orientation = orientation;
pdfDoc.Pages.Add(newPdfPage);
return newPdfPage;
}
internal void Write(PdfDocument pdfDoc, string text, double xPos, double yPos, XFont font, XBrush brush, params int[] pages) {
if (pdfDoc == null)
pdfDoc = new PdfDocument();
if(pdfDoc.Pages.Count < 1) {
pdfPage = FacadePdfHelper.AddPage();
pdfDoc.Pages.Add(pdfPage);
}
if (pages == null)
pages = new int[1] { 0 };
pages.ToList().ForEach((p) =>
using(var pdfGfx = XGraphics.FromPdfPage(p))
pdfGfx.DrawString(text, font, brush, xPos, yPos, XStringFormats.Default);
);
}
}
So when it comes to begin writing to a PDF file, one can fully concentrate on the positions, font and font style of the text to print to the PDF without having to care about PDF complexity as shown below.
Ithink the code below becomes rather interesting and functional, synonyms of productivity.
Code:
FacadePdfHelper.Write("Hello World!", 200, 200);
string tempFilepath = Path.GetTempFileName();
FileStream fs = new FileStream(tempFilepath, FileMode.Create, FileAccess.Write);
FacadePdfHelper.WorkingDocument.Save(fs);
fs.Close();
Process.Start(tempFilepath);
Anyway, after all this typing, all I wish is to share my perception about functional programming after all. Perhaps it could get interesting to have someday a FPDFSharp for Functional PDF Sharp library!
All I have to say humbly is that the work performed into any PDF library such as PDF Sharp is real hard labour. And after all, perhaps just a little more summary comments could help other programmers get acquainted faster with the library, since we always have to ask for questions and wait for answers. At least, with summary, we could get some more faster explanations.
I do hope I did not offend anyone with my comments.
But I still do have my positionning problem! lol