gpt4 book ai didi

c# - 使用 .NET 将可滚动面板保存为 PDF

转载 作者:行者123 更新时间:2023-11-30 12:16:15 25 4
gpt4 key购买 nike

我有一个面板,里面有很多控件供用户填写。这些包括文本框、复选框、单选按钮等。这是一个需要填写的长表单,因此控件位于可滚动的面板中。我需要的是将整个面板保存为 pdf。我认为 PDFsharp 是一个很好的库,可以将任何文本或图像保存为 pdf 文件,但我不想为面板内的每个控件编写代码。我曾经写过一个类来从 Control 对象创建一个 pdf 文件。它正在迭代给定控件的所有内部控件(以及它们的内部控件,直到没有内部控件留下),并使用它们的 Location 和 Size 属性将它们的 Text 属性(对于 chekable 控件是/否)写入 pdf。我现在找不到它,但我记得它与我使用的一些 DevExpress 控件有问题,所以我没有费心再写一次。 (编辑:我必须这样做,您可以在下面找到它。)我认为拍摄屏幕截图并将该图像另存为 pdf 会很好,但我找不到如何实现它。 This question似乎是这样,但没有令人满意的答案。

所以,无论是否截图,我都愿意听取任何建议。应该有很多场合用户必须填写长表格并且能够将其保存为pdf。同样,我们将不胜感激任何建议或解决方法。 (我考虑使用 html 创建表单,在 WebBrowser 控件中显示它并使用 html 到 pdf 库,但我真的更喜欢使用我现有的表单)

非常感谢。


编辑:我不得不写一些东西来迭代容器控件(如面板)的内部控件,并使用它们的位置、大小和字体属性将每个内部控件写入 pdf,但我不建议使用它(至少是这样)因为这些:

  1. 它将页面大小设置为给定控件的大小并且仅使用一个(通常是巨大的)pdf 页面。如果需要,您可以添加逻辑将其拆分为页面。 (我没有,但我猜你可能需要你的 pdf 打印机更友好)。
  2. Cheeso 的方法(使用 FlowDocument)是完成此类任务的更“合法”的方法。我更喜欢使用它而不是这个,但在这种情况下我别无选择。

我在此代码中使用了 PDFsharp。您可以在 it's hompage 中找到它或 it's CodePlex page .

PdfReport 类:

    private PdfDocument Document;
public Control Control { get; private set; }

public PdfReport(Control control) { Control = control; }

public PdfDocument CreatePdf(PdfDocument document = null)
{
Document = document != null ? document : new PdfDocument();
PdfPage page = Document.AddPage();
page.Height = Control.Height;
page.Width = Control.Width;

XGraphics gfx = XGraphics.FromPdfPage(page);
foreach (PdfItem item in CreatePdf(new Point(0, 0), Control.Controls))
{
XStringFormat format = item.IsContainer ? XStringFormats.TopLeft : item.TextAlign == ContentAlignment.BottomCenter ? XStringFormats.BottomCenter : item.TextAlign == ContentAlignment.TopLeft ? XStringFormats.TopLeft : item.TextAlign == ContentAlignment.TopCenter ? XStringFormats.TopCenter : XStringFormats.Center;
gfx.DrawString(item.Text, item.Font, item.Brush, new XRect(item.Location, item.Size), format);
}
return Document;
}
private IEnumerable<PdfItem> CreatePdf(Point location, Control.ControlCollection controls)
{
List<PdfItem> items = new List<PdfItem>();
foreach (Control control in controls)
{
if (control.Controls.Count > 0)
items.AddRange(CreatePdf(control.Location, control.Controls));
items.Add(new PdfItem(control, location));
}
return items;
}

public void SaveAsPdf(string path, bool open = false)
{
CreatePdf().Save(path);
if (open)
Process.Start(path);
}

PdfItem 类:

    public string Text { get; set; }
public Point Location { get; set; }
public Size Size { get; set; }
public Font Font { get; set; }
public bool IsContainer { get; set; }
public ContentAlignment TextAlign { get; set; }
public Color ForeColor { get; set; }
public XBrush Brush { get { return new SolidBrush(ForeColor); } }

public PdfItem() { }
public PdfItem(string text, Point location, Font font, Color foreColor, Size size, bool isContainer = false, ContentAlignment alignment = ContentAlignment.MiddleCenter)
{
Text = text;
Location = location;
Size = size;
Font = new Font(font.FontFamily, font.Size, font.Style, GraphicsUnit.World);
TextAlign = alignment;
ForeColor = foreColor;
IsContainer = isContainer;
}
public PdfItem(string text, Point location, Size size)
: this(text, location, new Font("Calibri", 12), Color.Black, size) { }
public PdfItem(Control control, Point parentLocation)
: this(control.Text, control.Location, control.Font, control.ForeColor, control.Size, control.Controls.Count > 0)
{
Location = new Point(Location.X + parentLocation.X, Location.Y + parentLocation.Y);
IEnumerable<PropertyInfo> properties = control.GetType().GetProperties();
if (properties.FirstOrDefault(p => p.Name == "TextAlign" && p.PropertyType == typeof(ContentAlignment)) != null)
TextAlign = (control as dynamic).TextAlign;
if (properties.FirstOrDefault(p => p.Name == "Checked" && p.PropertyType == typeof(bool)) != null)
{
string title = control.Text != null && control.Text.Length > 0 ? string.Format("{0}: ", control.Text) : string.Empty;
Text = string.Format("{0}{1}", title, (control as dynamic).Checked ? "Yes" : "No");
}
}

最佳答案

关于

. I think taking a screenshot and save that image as pdf would be nice but I couldn't find out how to achieve it.

在 codeplex.com 上有一个名为“cropper”的工具。它旨在用作可以截取屏幕截图的用户工具。它是托管代码,开源。

我可以想象在您的应用程序中嵌入一些裁剪魔法,这样您就可以截取该屏幕截图。我还可以想象这对于在出现问题时收集屏幕的诊断图像很有用。

另一方面...如果您有兴趣生成可在屏幕上重现内容的打印表单,那么我认为您应该使用 WPF,在这种情况下,做您想做的事情非常容易。例如,this question描述如何为 FlowDocument 进行打印预览。从那时起,您的用户可以打印为 PDF(如果他安装了 PDF 打印机)或打印为 XPS,或打印到物理输出设备,等等。

关于c# - 使用 .NET 将可滚动面板保存为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5732954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com