Pdfnet打印样本无效

时间:2017-06-09 11:52:01

标签: c# winforms printing pdftron pdfnet

在测试PDFPrintTest的示例时,我们注意到示例2与事件处理程序的示例1结合使用时表现不正常。

PrintPage事件处理程序的示例1:

void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Graphics gr = ev.Graphics;
        gr.PageUnit = GraphicsUnit.Inch;

        Rectangle rectPage = ev.PageBounds;         //print without margins
        //Rectangle rectPage = ev.MarginBounds;     //print using margins

        float dpi = gr.DpiX;
        if (dpi > 300) dpi = 300;

        int example = 1;
        bool use_hard_margins = false;

        // Example 1) Print the Bitmap.
        if (example == 1)
        {
            pdfdraw.SetDPI(dpi);
            Bitmap bmp = pdfdraw.GetBitmap(pageitr.Current());
            //bmp.Save("tiger.jpg");

            gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
        }

此处的完整示例代码:https://www.pdftron.com/pdfnet/samplecode/PDFPrintTest.cs.html

您会在评论中注意到bmp.Save("tiger.jpg");,这是出错的地方。如果我们运行代码并保存bmp,我们就可以获得jpg文件中所需的内容。但是,gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);打印一个纯空的pdf页面。那是为什么?

我们的目标:在某些情况下,我们需要强制使用40%灰度的printjob。 Winforms不支持此功能,我们只能设置灰度,而不是指定百分比,因此我们希望拦截打印并将输出更改为40%灰度,这将我们引导到PdfNet Print样本。从这些示例中,只有处理程序中的示例2具有Graphics gr,它接受​​一个颜色矩阵来将所需的灰度设置为页面。

欢迎使用任何非PdfNet解决方案,但示例代码尚未开箱即用仍然很奇怪。

2 个答案:

答案 0 :(得分:1)

感谢您指出这一点。就像你一样,我不清楚为什么bmp.Save工作正常,但Graphics.DrawImage(bmp,...只显示背景颜色。我怀疑它与传递到Graphics.DrawImage

的其他参数有关

由于Bitmap对象是正确的,那么这个特定问题实际上是.Net问题,而不是PDFNet问题,我目前无法回答。

示例的其他部分运行正常,使用PDFDraw.DrawInRect。这不适合你吗?

答案 1 :(得分:1)

我们得到了它的工作,显然它只是在打印到pdf时给出了白页。完全相同的代码渲染了太小的图像,但实际打印。 我们仍然不完全确定问题是什么,但制定了适当打印到pdf的新代码,并将全尺寸打印到打印机。

void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Graphics gr = ev.Graphics;
        gr.PageUnit = GraphicsUnit.Pixel; //this has been changed to Pixel, from Inch.

        float dpi = gr.DpiX;
        //if (dpi > 300) dpi = 300;


        Rectangle rectPage = ev.PageBounds;         //print without margins
        //Rectangle rectPage = ev.MarginBounds;     //print using margins

         float dpi = gr.DpiX;


        int example = 1;
        bool use_hard_margins = false;

        // Example 1) Print the Bitmap.
        if (example == 1)
        {
            pdfdraw.SetDPI(dpi);
            pdfdraw.SetDrawAnnotations(false);
            Bitmap bmp = pdfdraw.GetBitmap(pageitr.Current());



            gr.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
        }

`

if (dpi > 300) dpi = 300;这是将太小的图像发送到打印机的罪魁祸首。它还修复了白色pdf'问题。 其次,我们没有将rectPage传递给DrawImage,而是将其替换为:new Rectangle(0, 0, bmp.Width, bmp.Height)

我可以理解发送到打印机的较小尺寸,但为什么它没有拾取任何要打印到pdf的内容仍然不清楚。

虽然最终目标仍然是打印,但使用正确的“pdf”打印方式进行调试和测试会更加容易。上面的代码在2个独立的项目中工作,因此我将假设这确实解决了这个问题。