简体   繁体   English

用C#绘制和打印复杂文档(改进/替换我的PrintDocument)

[英]Drawing and Printing complex document in C# (improving/replacing my PrintDocument)

I am trying to produce a document in C# mainly to print. 我正在尝试主要使用C#生成文档以进行打印。 Basically it is a form, where I will fill in the values from objects from a database. 基本上,这是一种表单,我将在其中填写数据库中对象的值。

The code I been using in the past is ugly, long, a pain to write and worse to maintain. 我过去使用的代码很丑陋,很长,编写起来很麻烦,维护起来也更糟。

public class MyDoc : PrintDocument
{
    private MyContainer _myObject; //Inherits from list.

    private int PageCount()
    {
        return (int)Math.Ceiling(_myObject.Count / 5f);
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        g.PageUnit = GraphicsUnit.Millimeter;

        Font bigFont = new Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Point);
        Font mediumFont = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point);
        Font smallFont = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point);
        Font smallNumberFont = new Font("Courier New", 7, FontStyle.Regular, GraphicsUnit.Point);
        Pen linePen = new Pen(Brushes.Black, 0.2F);

        int rowPosition = 5;
        int rowIncrement = 9;

        g.DrawString(string.Format("Page {0} of {1}", _pageNumber, PageCount()), smallFont, Brushes.Black, 175, rowPosition);
        rowPosition = 15;

        g.DrawImage(Image.FromFile("myfile.jpg"), 127, rowPosition);

        g.DrawString("Name:", smallFont, Brushes.Black, 3, rowPosition);
        g.DrawString(_myObject.Name, smallBoldFont, Brushes.Black, 50, rowPosition);
        rowPosition += rowIncrement;

        //Sub-Items Table Header
        g.DrawRectangle(linePen, 3, rowPosition, 35, 10);
        g.DrawRectangle(linePen, 38, rowPosition, 85, 10);
        g.DrawRectangle(linePen, 123, rowPosition, 25, 10);
        g.DrawRectangle(linePen, 148, rowPosition, 20, 10);
        g.DrawRectangle(linePen, 168, rowPosition, 25, 10);

        foreach (var i in _myObject)
        {
            //Draw a grid and write out all details of everything in the collection.
            //If there are 2 many, stop and we will create a page 2.
        }

        //Write Standard page footer
        //etc etc.
    }
}

Also a new requirement, I need to Save this to either a pdf or an image. 这也是一项新要求,我需要将其保存为pdf或图像。

Basically I've got 300-500 lines of code to write a document which looks somewhat decent. 基本上,我有300-500行代码来编写看起来不错的文档。

I've got some more to do which will be very similar and I need a better way! 我还有很多事情要做,这将非常相似,我需要更好的方法!


I've thought of: 我想到了:

XAML - then databind the properties I need to fill in. Not sure how to handle it spreading onto 2 or more pages though. XAML-然后将我需要填写的属性进行数据绑定。尽管不确定如何传播到2个或更多页面,但不确定。

Word Interop - I don't know if I can. Word Interop-我不知道是否可以。 It runs on a webserver where office is NOT installed. 它在未安装Office的Web服务器上运行。 (I can package a few dll's up if thats all I need, but I'm not sure.) (如果仅此而已,我可以打包一些dll,但是我不确定。)

So I'm looking for ideas from all you good folks. 所以我正在寻找大家的想法。

I'd prefer not to use external libraries, but if thats the best way, it needs to be free and have good licensing :) 我不希望使用外部库,但是如果那是最好的方法,则它需要免费并且具有良好的许可:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM