另存为PDF时WPF DataGrid网格线不可见 [英] WPF DataGrid GridLines not visible when saved as PDF

查看:123
本文介绍了另存为PDF时WPF DataGrid网格线不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataGrid表示WPF应用程序中的某些数据.在我要使用PDFSharp将具有DataGrid的特定WPF窗口保存到PDF的功能中,我面临的一个问题是,当以较小的查看百分比查看保存的PDF时,DataGrid GridLines不可见. (请参阅附加的图像,仅当PDF视图设置为139%时,网格线才可见.但是,在较小的查看百分比中,一些网格线将被省略.)

I'm using a DataGrid to represent some data in a WPF application. In a feature where I'm saving a particular WPF Window which has the DataGrid into a PDF using PDFSharp, I'm facing an issue that the DataGrid GridLines are not visible when the saved PDF is viewed in smaller viewing percentages. (Refer attached images, only when the PDF view is set at 139%, the GridLines are visible. However, in smaller viewing %, some grid lines get omitted.)

这是PDF保存代码:-

Here's the PDF Saving Code:-

MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
var doc = new System.Windows.Xps.Packaging.XpsDocument(package);
XpsDocumentWriter writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(doc);

VisualBrush sourceBrush = new VisualBrush(this);
DrawingVisual drawingVisual = new DrawingVisual();

using (var drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(this.ActualWidth, this.ActualHeight)));
}

writer.Write(drawingVisual);
doc.Close();
package.Close();

var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
XpsConverter.Convert(pdfXpsDoc, sFileName, 0);

我认为这与绘制视觉效果的质量有关.然后,我尝试使用DrawImage在更高的分辨率下制作视觉效果的代码段.这是代码段:-

I believe it has to do with the quality with which the visual is drawn. Then I tried this snippet where I'm using DrawImage to make the visual at a higher resolution. Here's the snippet:-

MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
var doc = new System.Windows.Xps.Packaging.XpsDocument(package);
XpsDocumentWriter writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(doc);

double dpiScale = 600.0 / 96.0;
var renderBitmap = new RenderTargetBitmap(Convert.ToInt32(this.Width * dpiScale),
                       Convert.ToInt32(this.Height * dpiScale),
                       600.0,
                       600.0,
                       PixelFormats.Pbgra32);
renderBitmap.Render(this);
var visual = new DrawingVisual();
using (var dc = visual.RenderOpen())
{
    dc.DrawImage(renderBitmap, new Rect(0, 0, this.Width, this.Height));
}                

writer.Write(visual);
doc.Close();
package.Close();

var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
XpsConverter.Convert(pdfXpsDoc, _pdfFileName, 0);

此代码段可以正常工作,因为即使以较小的查看百分比也可以看到网格线,但是它使我的应用程序陷入了PDF保存操作,并且引发System.OutofMemoryException,并显示消息内存不足,无法继续执行程序. "但是,该应用程序不会崩溃.

This snippet is working as in the grid lines are visible even in smaller viewing percentages but it makes my application stuck at the PDF save operation and also it throws System.OutofMemoryException with message "Insufficient memory to continue the execution of the program." However, the application doesn't crash.

要检查PDF查看器的行为,我在MS Word中生成了一个包含多行和多列的表,并将其另存为PDF.在这种情况下,即使以很小的查看百分比,表格网格线也清晰可见.

To check the behavior of PDF viewer, I generated a table with multiple rows and columns in MS Word and saved it as a PDF. In that case, the table grid lines are clearly visible even at small viewing percentages.

有人可以帮我吗?

推荐答案

我假设第一个代码段以矢量格式创建了一个表(您没有提供可用来验证此格式的PDF).

I assume the first code snippet creates a table in vector format (you do not supply a PDF that allows to verify this).

第二个代码段尝试创建位图图像(栅格格式).

The second code snippet attempts to create a bitmap image (raster format).

无论哪种方式:对于矢量图像和光栅图像,都取决于PDF查看器,是否可见细线. Adobe Reader具有许多选项(例如增强细线",线条流畅",图像平滑"),这些选项将对实际显示产生影响-在客户端计算机上进行设置,而在PDF中则未进行任何设置.

Either way: with both vector and raster images it depends on the PDF viewer whether thin lines are visible. Adobe Reader has many options (like "Enhance thin lines", "Smooth line art", "Smooth images") that will have an effect on the actual display - to be set on the client computer, nothing to be set in the PDF.

我假设您使用MS Word进行的测试还创建了矢量格式的表格,但可能会有更粗的线.因此,此测试无法证明任何事情.

I assume your test with MS Word also created a table in vector format, but maybe with thicker lines. So this test proofs nothing.

这篇关于另存为PDF时WPF DataGrid网格线不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆