Search code examples
c#pdfsharppdfsharpcore

Cannot see XForm drawing in the document using PdfSharpCore


I made a project using PdfSharp. However, when I wanted to port my project from PdfSharp to PdfsharpCore, my XForm objects were not drawn.

Here is a sample code:

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;

PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

XForm form = new XForm(document, XUnit.FromMillimeter(30), XUnit.FromMillimeter(45));

XFont font = new XFont("Verdana", 10, XFontStyle.Regular);

using (XGraphics formGfx = XGraphics.FromForm(form))
{
    formGfx.DrawString("Cannot see this text in the document", font, XBrushes.Black, 3, 0, XStringFormats.TopLeft);

    // Cannot see this either
    XPen pen = XPens.LightBlue.Clone();
    pen.Width = 2.5;
    formGfx.DrawBeziers(pen, XPoint.ParsePoints("30,120 80,20 100,140 175,33.3"));
}    

using(XGraphics pageGfx = XGraphics.FromPdfPage(page))
{
    // Drawing form
    pageGfx.DrawImage(form, 0, 0);

    pageGfx.DrawString("This text is visible", font, XBrushes.Black, new XPoint(0, 50));
}

string savePath = Path.Join(Directory.GetCurrentDirectory(), "XFormTest.pdf");
document.Save(savePath);
document.Close();

I was not able to find any solution for this problem on the Internet, only this unanswered issue.

I tried this on Windows 10 and Ubuntu 22.04 LTS. Neither worked.


Solution

  • There was a bug in PdfSharpCore that was fixed in the meantime.

    https://github.com/ststeiger/PdfSharpCore/pull/295