PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDFSharp for Mono
https://forum.pdfsharp.net/viewtopic.php?f=4&t=1206
Page 1 of 1

Author:  mscott161 [ Fri Jun 04, 2010 3:14 am ]
Post subject:  PDFSharp for Mono

Hello,

I would like to gain permission and share my code with the PDFSharp Team, so I may code the PDFSharp Project in Mono. I have a few application we are coding to work with both Microsoft and Mono. Please let me know. I will be happy to post the code as I work on it to your forum or codeplex

Michael Scott
eBiz Software/Agilisent
Director of Research & Development

Author:  mscott161 [ Thu Jun 10, 2010 6:43 pm ]
Post subject:  Re: PDFSharp for Mono

I have made a couple of small changes to the PDFSharp source to compile under Mono on linux and windows. I basically provide a property to set a font directory or multiple font directories. As I am no familiar with Font File Formats I have set it to read on ttf files. This has passed all test of creating PDF. There program will still through an error if the font is not on the system. I can attach or email the source code for your review. I have not test PDF Forms or WPF. The tar.gz file is 1.6MB is size, please let me know if you would like a copy.

Michael Scott
Director of Research & Development
eBiz Software/Agilisent

Author:  DaleStan [ Mon Jun 21, 2010 5:56 pm ]
Post subject:  Re: PDFSharp for Mono

I would recommend posting the source diff here (unified, not ed!)

Author:  mscott161 [ Mon Jun 21, 2010 7:27 pm ]
Post subject:  Re: PDFSharp for Mono

what do you mean by unified not ed? I can provide just the code changes, do you want me to zip it up the changed files or post the code that changed in a post reply to this forum.
Michael

Author:  DavidSchmitt [ Mon Jun 28, 2010 11:58 am ]
Post subject:  Re: PDFSharp for Mono

Hi Michael,

"unified" is a specific patch-format. You can generate it easily by running
Code:
diff -r -N -u original_dir/ modified_dir/


The advantage of this format is that it allows easy comparison of before/after state and tools exist to automatically apply the changes to another source tree, even if the new target was changed since you downloaded your tree.

Thanks for your time and work! I want to run PDF# on mono too, and would be very interested in seeing and testing your changes!

Best Regards, David Schmitt

Author:  mscott161 [ Mon Jun 28, 2010 4:34 pm ]
Post subject:  Re: PDFSharp for Mono

This is the output from diff.

If this will be easier for you I have put the mono solution up to be downloaded from
http://www.ebizsoft.com/MonoPdfSharp.tar.gz

Michael

Author:  DaleStan [ Mon Jun 28, 2010 6:50 pm ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
This is the output from diff.

I see no output.

The "ed" reference, by the way, is to one of the other output formats that diff can create; an ed script. These are, unlike context and unified diffs, incredibly fragile.

Author:  mscott161 [ Mon Jun 28, 2010 9:19 pm ]
Post subject:  Re: PDFSharp for Mono

Sorry about that I thought it attached the file I will just paste it here.

Michael

/PdfSharp/PdfSharp.Fonts.OpenType/FontData.cs

void CreateGdiFontImage(XFont font, XPdfFontOptions options/*, XPrivateFontCollection privateFontCollection*/)
{
System.Drawing.Font gdiFont = font.RealizeGdiFont();

this.data = null;

foreach (string fontFile in NativeMethods._FontFiles)
{
try
{
BinaryReader br = new BinaryReader(File.OpenRead(fontFile));
byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
br.Close();

FontData fd = new FontData(buffer);

System.Console.WriteLine(fd.name.Name);

if (fd.name.Name.Trim().ToLower() == gdiFont.Name.Trim().ToLower())
{
this.data = buffer;
break;
}
}
catch
{
//System.Console.WriteLine(exc.ToString());
}
}

}

/PdfSharp/PdfSharp.Internal/NativeMethods.cs
public static class NativeMethods

{

static string _FontDirectory = "";

static List<string> _FontDirectories = new List<string>();

public static List<string> _FontFiles = new List<string>();



public static string FontDirectory

{

set

{

_FontDirectory = value;

_FontDirectories.Add(value);

RecursiveFontLookup(value);

}

}



public static List<string> FontDirectories

{

get { return _FontDirectories; }

}



static void RecursiveFontLookup(string dir)

{

string[] files = Directory.GetFiles(dir);

foreach (string file in files)

if (file.EndsWith(".ttf"))

_FontFiles.Add(file);



string[] directories = Directory.GetDirectories(dir);



foreach (string directory in directories)

{

_FontDirectories.Add(directory);

RecursiveFontLookup(directory);

}

}


//[DllImport("user32.dll")]

//public static extern IntPtr GetDC(IntPtr hwnd);



//[DllImport("user32.dll")]

//public static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);



//[DllImport("gdi32.dll", SetLastError = true)]

//public static extern int GetFontData(

// IntPtr hdc, // handle to DC

// uint dwTable, // metric table name

// uint dwOffset, // offset into table

// byte[] lpvBuffer, // buffer for returned data

// int cbData // length of data

// );



//[DllImport("gdi32.dll", EntryPoint = "CreateFontIndirectW")]

//public static extern IntPtr CreateFontIndirect(LOGFONT lpLogFont);



//[DllImport("gdi32.dll")]

//public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);



//[DllImport("gdi32.dll")]

//public static extern bool DeleteObject(IntPtr hgdiobj);

Author:  mscott161 [ Mon Jun 28, 2010 9:20 pm ]
Post subject:  Re: PDFSharp for Mono

Here is an example of using the PdfSharp in Mono on a Linux (Ubuntu)

using System;
using System.Diagnostics;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Internal;

namespace HelloWorld
{
class MainClass
{
// /usr/share/fotns/truetype/*

public static void Main (string[] args)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";

// Create an empty page
PdfPage page = document.AddPage();

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

// Create a font
NativeMethods.FontDirectory = "/usr/share/fonts/";

XFont font = new XFont("FreeMono", 20, XFontStyle.BoldItalic);

// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);

// Save the document...
const string filename = "HelloWorld_tempfile.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
}
}

Author:  DaleStan [ Tue Jun 29, 2010 1:24 pm ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
Sorry about that I thought it attached the file I will just paste it here.

That is not the output of diff.

Code:
$ diff -r -N -u original_dir/ modified_dir/ > PDFSharp_on_Mono.diff
and post PDFSharp_on_Mono.diff somewhere. If this forum doesn't accept .diff files, other alternatives are to fudge the extension or to host it on the server where you posted the whole source archive.

Author:  mscott161 [ Tue Jun 29, 2010 3:18 pm ]
Post subject:  Re: PDFSharp for Mono

I have put the output PDFSharp_on_Mono.diff (http://www.ebizsoft.com/PDFSharp_on_Mono.diff) The file is to big to post to this forum. I have a modified version of diff on the machine I used yesterday so I copied the directories to a clean Ubuntu machine with an unmodified diff and got the file exported per your instructions.
Let me know if I need to do anything else.

Michael Scott
eBiz Software

Author:  DavidSchmitt [ Tue Jun 29, 2010 4:05 pm ]
Post subject:  Re: PDFSharp for Mono

Hi Michael,

thanks for going the whole mile and uploading the diff to your site! Sadly, there is quite some cruft (seems like build output and skewed line-endings to me) in it which diminishes its value. Could you execute the diff on a cleaned source tree (and perhaps also use -w to suppress whitespace-only changes)?

In other news, I tried your approach of reading the TTF files directly under Linux and it worked great for me. So now we only need one of the maintainers to apply your changes (or something similar).


Best Regards, David Schmitt

Author:  mscott161 [ Tue Jun 29, 2010 5:42 pm ]
Post subject:  Re: PDFSharp for Mono

David Schmitt,

I have re ran the diff with the -w and uploaded it to the previous URL. I am having problems getting the latest source tree. I will try again and re run the diff for you.

Michael Scott
eBiz Software

Author:  mscott161 [ Tue Jun 29, 2010 5:44 pm ]
Post subject:  Re: PDFSharp for Mono

David,

I do have the latest already from your sourceforge page PDFSharp 1.31

Michael

Author:  mscott161 [ Wed Jun 30, 2010 7:37 pm ]
Post subject:  Re: PDFSharp for Mono

David,

I hope all is going well with PdfSharp. I was wonder if you had time to answer a few business questions for me. I know the PdfSharp is all open source but how do you make money to pay yourself? If this is too personal that is fine, I was think of provide an application in the Open Source market but have no idea how to make money enough to cover the costs.

Thank you,
Michael

Author:  DavidSchmitt [ Fri Jul 02, 2010 10:31 am ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
I hope all is going well with PdfSharp. I was wonder if you had time to answer a few business questions for me. I know the PdfSharp is all open source but how do you make money to pay yourself? If this is too personal that is fine, I was think of provide an application in the Open Source market but have no idea how to make money enough to cover the costs.


I'm not the maintainer of PDFSharp, if you're thinking that. I'm just a regular user of it.

Author:  GeertVc [ Mon Oct 04, 2010 5:33 pm ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
I have put the output PDFSharp_on_Mono.diff (http://www.ebizsoft.com/PDFSharp_on_Mono.diff) The file is to big to post to this forum. I have a modified version of diff on the machine I used yesterday so I copied the directories to a clean Ubuntu machine with an unmodified diff and got the file exported per your instructions.
Let me know if I need to do anything else.

Michael Scott
eBiz Software


Hi Michael,

The above mentioned link does not seem to exist any more. Can you check pls?

I'm anyhow interested in your changes (I assume I have to apply the patch on top of PdfSharp 1.31 to get the "mono" version of PdfSharp, right?), since we want to apply PdfSharp in a Linux environment running Mono.

If it's not too much of a trouble for you, you can send it directly to geert dot vancompernolle at gmail dot com.

Thanks in advance for your help,

Author:  mscott161 [ Mon Oct 04, 2010 7:14 pm ]
Post subject:  Re: PDFSharp for Mono

Gerrt,

I put the file up in a zip file for you.

http://www.ebizsoft.com/PDFSharp_on_Mono.zip

Let me know if you have problems.

Michael

Author:  GeertVc [ Tue Oct 05, 2010 11:11 am ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
Gerrt,

I put the file up in a zip file for you.

http://www.ebizsoft.com/PDFSharp_on_Mono.zip

Let me know if you have problems.

Michael

Thanks for the file, Michael.

I've compiled PdfSharp (which was apparently already patched) on Windows, next to the HelloWorld application. Then I moved it to the "Linux realm" and put both the exe and dll in one and the same directory.

I've also checked if there were fonts in the directory /usr/share/fonts and there were.

But when loading the application Helloworld.exe on Linux, I get the following error messages:

Code:
beq00819@lxdevenv:~/monotest$ mono HelloWorld.exe
WARNING: The runtime version supported by this application is unavailable.
Using default runtime: v1.1.4322

** (HelloWorld.exe:6760): WARNING **: The class System.Collections.Generic.IDictionary`2 could not be loaded, used in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
** (HelloWorld.exe:6760): WARNING **: The class System.Collections.Generic.List`1 could not be loaded, used in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
** (HelloWorld.exe:6760): WARNING **: The class System.Collections.Generic.IEnumerable`1 could not be loaded, used in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
** (HelloWorld.exe:6760): WARNING **: The class System.Collections.Generic.Dictionary`2 could not be loaded, used in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Unhandled Exception: System.TypeLoadException: A type load exception has occurred.
  at PdfSharp.Pdf.PdfDocument..ctor () [0x00000]
  at HelloWorld.MainClass.Main (System.String[] args) [0x00000]


I have this mono version installed (on a Linux Ubuntu 8.04 LTS distro):

Code:
beq00819@lxdevenv:~/monotest$ mono --version
Mono JIT compiler version 2.4.4 (Debian 2.4.4~svn151842-1ubuntu3~dhx3)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        GC:            Included Boehm (with typed GC)
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  x86
        Disabled:      none


Any idea what could cause this issue?

Author:  mscott161 [ Tue Oct 05, 2010 4:26 pm ]
Post subject:  Re: PDFSharp for Mono

Geert,

I looks like a problem with different framework versions. I see when you run the application that it cannot find the framework and defaults to version 1.1 which does not have generics. If you have MonoDevelop on the Ubuntu box I would copy the code to it and compile it there. You could also try compiling the Helloworld and PDFSharp with framework 2 specifically.

Michael

Author:  RobWalker [ Sat Oct 23, 2010 11:39 pm ]
Post subject:  Re: PDFSharp for Mono

I have PDFSharp running under mono on a Fedora system using Michael's tar file from above.

There were a couple of issues:
[*] I needed to export an environment variable LANGUAGE=en_US otherwise there was an exception in PdfPage.Initialize due to no default region being set
[*] The path to gdiplus was not configured correct as per http://www.mono-project.com/DllNotFoundException

However, now when I call PdfSharp.Drawing.XGraphics.FromPdfPage I get a System.NotSupportedException error:
Could not open display (X-Server required. Check you DISPLAY environment variable)
from System.Drawing.Graphics.FromHwnd(0)

This is a server image running under EC2 ... there is no display (and simply trying to pretend there was didn't work) and no X libraries are installed.

Is there a way around this? Has anyone used PdfSharp under mono in a web stack?

Author:  GeertVc [ Sun Oct 24, 2010 6:18 am ]
Post subject:  Re: PDFSharp for Mono

mscott161 wrote:
It looks like a problem with different framework versions.... ...You could also try compiling the Helloworld and PDFSharp with framework 2 specifically.


Hi Michael,

Sorry for the late reply... Your guess was the correct one. Under VS 2010 EE, I recompiled everything with framework 2 settings and now it works on Linux too.

Thanks again for your suggestions!

Author:  timotheus [ Wed Jan 25, 2012 11:07 am ]
Post subject:  Re: PDFSharp for Mono

RobWalker wrote:
Is there a way around this? Has anyone used PdfSharp under mono in a web stack?


I never got around to post here, but for about a year, I am now using PdfSharp on a Linux server without a screen, and it works fine. This is being used for our project called OpenPetra (http://www.openpetra.org), see also http://tpokorra.blogspot.com/2011/09/us ... -2011.html
We are producing PDF files on the fly, for registering people for a conference.

I have modified the patch from Michael posted in this thread.

This is the link to the diff file: http://bazaar.launchpad.net/~openpetrac ... f-mono.txt

Please see the details here: http://bazaar.launchpad.net/~openpetrac ... /PDFSharp/

You can also download a dll compiled for mono, including the patch, from our code repository:
http://bazaar.launchpad.net/~openpetrac ... fSharp.dll

Thanks for this useful tool!

All the best,
Timotheus

Author:  khairuddinniam [ Sat Jan 28, 2012 3:11 pm ]
Post subject:  Re: PDFSharp for Mono

Hi, Timotheus. Does your compiled dll also include patch (Fast table rendering) from topic http://forum.pdfsharp.net/viewtopic.php?f=2&t=679&start=0 ?

Author:  timotheus [ Tue Feb 14, 2012 4:58 pm ]
Post subject:  Re: PDFSharp for Mono

Sorry, I forgot to subscribe to this thread. Now I am subscribed.
I have now compiled a new version of the PDF, including the patch for the table rendering.

The latest dll is at http://bazaar.launchpad.net/~tpokorra/o ... fSharp.dll

Author:  Bob Jones [ Wed Jul 17, 2013 3:27 pm ]
Post subject:  Re: PDFSharp for Mono

Hi,

This thread seems to have died but I would love to get a working copy of PDFSharp on Linux.

I tried to embed the fonts returned by the User32.dll file in to the assembly, which "sort of" works, but the rendering is slightly out of place. I also get the error message that Debug Assertion Failed... "Value differs from information retrieved from font issue."

I would love to get a working copy up. I have tried to download the dlls attached but they fail worse than the version I made. I gather there have been some attempts to use pre-loaded fonts by looking at the code. Does anyone know what the status of this is? If mono seems like it might work?


-Nigel

Author:  timotheus [ Thu Jul 18, 2013 6:17 am ]
Post subject:  Re: PDFSharp for Mono

yes, my version works with Mono on Linux, but the fonts do not look the same on Linux as they do on Windows.

The code and compiled dll is now placed here (but actually no change from last year), and includes a Readme.txt:
https://github.com/tpokorra/openpetragi ... y/PDFSharp

Author:  Bob Jones [ Fri Jul 19, 2013 5:39 pm ]
Post subject:  Re: PDFSharp for Mono

Hmm.. I just gave it a whirl and it still threw an error related to not finding the user32.dll function.

It looks like your font library collection has to have the path to the font files set, do you set this before calling it manually? If so which directory do you usually point to?

Author:  timotheus [ Mon Jul 22, 2013 8:39 pm ]
Post subject:  Re: PDFSharp for Mono

I had to look for the printing functionality, have not worked on that for a while.
Yes, you are right: I am setting the path here:
https://github.com/tpokorra/openpetragi ... er.cs#L680
Code:
if (Directory.Exists("/usr/share/fonts/"))
{
    PdfSharp.Internal.NativeMethods.FontDirectory = "/usr/share/fonts/";
}


I am also installing the package liberation-mono-fonts (Redhat/CentOS), on Debian/Ubuntu it might be ttf-liberation.

Author:  Bob Jones [ Thu Jul 25, 2013 3:19 pm ]
Post subject:  Re: PDFSharp for Mono

Great, thanks for the information! This does indeed work.

The solution of course seems a bit hacky. The issue of course is that the font directory might change, I found that one could list all the available fonts using the X11 command:

xset q

which works if X11 is installed. I gather mono is using the libgdiplus.so library to find the font files, it would be nice to integrate with that more, though am not sure if this is possible.

In the meantime, this works well, thanks!

Author:  martin.kupec [ Wed Dec 10, 2014 5:46 pm ]
Post subject:  Re: PDFSharp for Mono

Hi the patch works, but have a slite problem. It doesn't honor font style.

The diff is here:

130c130,131
< // Console.WriteLine("looking for " + gdiFont.Name);
---
> // Console.WriteLine("looking for " + gdiFont.Name + " " + gdiFont.Style);
> var suitable = new List<KeyValuePair<FontData, byte[]>> ();
141,142d141
< //System.Console.WriteLine(fd.name.Name);
<
144,146c143,144
< {
< this.data = buffer;
< break;
---
> {
> suitable.Add(new KeyValuePair<FontData, byte[]>(fd, buffer));
153a152,165
> foreach(var fd in suitable) {
> if(gdiFont.Bold != fd.Key.name.Style.Contains("Bold")) {
> continue;
> }
> if(gdiFont.Italic != (fd.Key.name.Style.Contains("Italic") || fd.Key.name.Style.Contains("Oblique"))) {
> continue;
> }
> //Console.WriteLine("{0} {1}", fd.Key.name.Name, fd.Key.name.Style);
> this.data = fd.Value;
> break;
> }
> if(this.data == null && suitable.Count > 0) {
> this.data = suitable[0].Value;
> }

Author:  xmb [ Thu Dec 18, 2014 2:03 pm ]
Post subject:  Re: PDFSharp for Mono

Hello everybody!
Is there a collection of patches to run MigraDoc with mono?
I'm not sure where I can get the latest patches.
Thanks, Markus

Author:  CodingBlitz [ Sat Sep 19, 2015 3:51 am ]
Post subject:  Re: PDFSharp for Mono

I cannot seem to get MonoDevelop C# (CentOS Linux) and PdfSharp to work together when I execute the resulting .exe file.

I keep getting a user32.dll reference whenever I start specifying font stuff:
Attachment:
problem.jpg
problem.jpg [ 74.51 KiB | Viewed 3985173 times ]


I get no compile errors just run-time errors involving font dll stuff.

Comments?

Author:  daniellor75 [ Wed Mar 16, 2016 5:56 pm ]
Post subject:  Re: PDFSharp for Mono

Hi

I've changed PDFsharp 1.5 to work on MONO, still tested on ubuntu ttf-mscorefonts-installer package fonts, look at travis.yml file.

https://github.com/daniellor/PDFsharp

this sample working on Mono

https://github.com/daniellor/PDFsharp/b ... Samples.cs

Daniel

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/