0

Basically, I need to take a question from a text file and format it as a question would be formatted in a maths exam.

Right now, I'm using PDFsharp to do this but it always saves the alphanumerical symbols (for example, ) as boxes.

I tried copying from the sample on PDFsharp and have this

Dim document As New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = document.AddPage()
Dim gfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(page)
Dim tf As New PdfSharp.Drawing.Layout.XTextFormatter(gfx)
Dim options As New PdfSharp.Drawing.XPdfFontOptions(PdfSharp.Pdf.PdfFontEncoding.Unicode)
Dim font As New PdfSharp.Drawing.XFont("LastResort", 10, PdfSharp.Drawing.XFontStyle.Regular, options)
tf.Alignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Left
tf.DrawString(questionArray(i)), font, PdfSharp.Drawing.XBrushes.Black, New PdfSharp.Drawing.XRect(0, 0, page.Width.Point, page.Height.Point), PdfSharp.Drawing.XStringFormats.TopLeft)
Dim filename As String = "test" + Str(i).Trim + ".pdf"
document.Save(filename)
Process.Start(filename) 

I know I don't need to keep repeating the "PdfSharp.Pdf" stuff, my plan was to clean it all up when I get the characters saving properly. Last Resort is a font that contains unicode symbols and the mathematical alphanumerical block, according to https://www.fileformat.info/info/unicode/block/mathematical_alphanumeric_symbols/fontsupport.htm

My end goal is to take a basic .txt file like "f(x) = 5[^2] + (k+7) + k where k is a real constant." and save it to a PDF to resemble a real math exam question.

So, is there a better way to do this or a way to make PDFsharp do it?

ryan
  • 23
  • 4

1 Answers1

1

Unicode support in PDFsharp works fine for characters in the range 0x0000 to 0xffff as long as you use a font that supports these characters.

Mathematical Alphanumeric Symbols are in the range U+1D400..U+1D7FF. You have to patch PDFsharp to make use of them. They are not yet supported out of the box as of today (December 2019).

In your snippet you give "LastResort" as the name of the font. Do you have a font with that name installed in Windows? Can you use it with e.g. Word or WordPad?
Maybe try "Arial" or "Tahoma" or "Verdana" instead.

Do you see the correct strings in the debugger? Maybe the problem is with the formatting of the text file or the encoding used to open it.

Update:
All characters in LastResort look like boxes. No good choice for math exam sheets: enter image description here

Please try a different font.

  • Verdana, Tahoma, Arial all don't work because they don't include the mathematical alphanumerical unicode block. LastResort does and I do have it installed. I also just put in the unicode x character straight into the program rather than through the file to test that, and it still shows up as a box. – ryan Dec 17 '19 at 15:38
  • Then maybe try "FreeSerif" from the site you linked to in your question. – I liked the old Stack Overflow Dec 17 '19 at 15:46
  • Mathematical Alphanumeric Symbols are in the range U+1D400..U+1D7FF. You have to patch PDFsharp to make use of them. They are not yet supported out of the box as of today (December 2019). – I liked the old Stack Overflow Dec 17 '19 at 15:49
  • Ah, okay. I'll look into how to patch it then. Thanks. – ryan Dec 17 '19 at 15:51