samad montazeri
samad montazeri

Reputation: 1273

Using PDFsharp and MigraDoc only for printing(not creating PDF)

I want to print some formatted text using C#. the text is something like this:

Hi i am a Multi-line partially formatted text. I want to be printed using C#(winforms). I might contain some unicode text like مرا به هیچ بدادی و من هنوز بر آنم/ که از وجود تو مویی به عالمی نفروشم and so on....

I tried C# System.Drawing printing, but it was very hard and very messy, so I searched and found PDFsharp which can draw multi style text and create a PDF from it. It says in the first page that:

PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer

but I don’t see how? I don’t want to create a PDF file and print it. also I don’t want to make a pagePreview that I don’t use.

Is there a way to print directly from XGraphics or whatever? How?

Is there a better alternative(and free, because I am broke :( ) to PDFsharp?

(a simple "helloworld" sample would be very nice)

Upvotes: 0

Views: 3547

Answers (2)

samad montazeri
samad montazeri

Reputation: 1273

user-241.007 answer is correct(and I accepted it as the right answer). But I post this answer, just to provide an example(as I asked for it in the question)

In the code below, the same text in the question, is drawn on a form(in OnPaint event of Form).

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Document document = new Document();
    // Add a section to the document
    Section section = document.AddSection();

    // Add a paragraph to the section
    Paragraph paragraph = section.AddParagraph();
    paragraph.Format.Font.Size = 14;
    // Add some text to the paragraph
    paragraph.AddFormattedText("Hi i am a");
    paragraph.AddFormattedText(" Multi-line ",TextFormat.Bold);
    FormattedText ft = paragraph.AddFormattedText("partially formatted");
    ft.Italic = true;
    paragraph.AddFormattedText(" text. I want to be printed using C#(winforms). I might contain some unicode text like مرا به هیچ بدادی و من هنوز بر آنم/ که از وجود تو مویی به عالمی نفروشم and so on.... ");
    paragraph = section.AddParagraph(); 
    //here is the important part, linking Graphics to XGraphics. Graphics can be used in drawing on form, or in printing
    XGraphics xgf = XGraphics.FromGraphics(e.Graphics, new XSize(0, 1000));
    DocumentRenderer docRenderer = new DocumentRenderer(document);
    docRenderer.PrepareDocument();
    //rendering first page to Xgraphics
    docRenderer.RenderPage(xgf, 1);
}

output of code

Upvotes: 0

You can create an XGraphics object from a Graphics object:

XGraphics gfx = XGraphics.FromGraphics(graphics, size);

So if you have a Graphics object for a printer, you can use PDFsharp code for printing.

Not sure if it can be of help for you, as the Graphics object can be used for printing directly.
Using XGraphics makes sense if you need PDF and printing or PDF and screen preview.

Upvotes: 1

Related Questions