- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在我的 UWP 应用程序中用作打印根的 XAML 页面上有一个 Canvas 元素。我正在使用 PrintManager.PrintTaskRequested 和 PrintDocument.Paginate 等事件来准备我的报告并将其发送到打印机。
我需要以编程方式将报告导出为 PDF 文件。理想的解决方案是以某种方式利用现有的打印根( Canvas ),打印到它,然后将结果转换为 PDF。之后,我就可以将 PDF 保存到文件或将其附加到电子邮件中。
一段时间以来,我一直在寻找合适的解决方案,但在 UWP 中没有任何效果。例如,这篇文章提供了一个完美的解决方案,它似乎在 UWP 中不起作用:
如有任何帮助,我将不胜感激。
最佳答案
我为这个问题苦苦挣扎了很长时间 - 在 UWP 中自动、程序化 XAML 到 PDF 的转换 - 最后找到了一个很好的解决方案。
有几个库可用于在 UWP 中以编程方式创建 PDF。诀窍是 XAML 转换。我采取了以下方法:
A) 遍历 XAML 树并生成要转换的控件列表。在我的例子中,文本 block 和边框,但这可以扩展。
B) 声明与 XAML 的实际大小相匹配的 PDF 页面大小。
C) 遍历列表,获取控件坐标。使用 C1PDF 中的适当函数在 PDF 中创建相同的元素。此代码还会检查任何 RotateTransforms 并将旋转角度也应用于文本。
使用此解决方案,我能够生成与 XAML UI 完全相似的文件(它本身代表打印文档)作为 PDF,完全可扩展且具有完美的打印渲染。
这里有一些代码可以帮助您使用我编写的 ComponentOne PDF 控件将我的 XAML 控件转换为 PDF:
Async Function XAMLtoPDF(myXAMLcontrol As Control) As Task(Of Boolean)
Dim pdf As C1PdfDocument
pdf = New C1PdfDocument(PaperKind.Letter)
Dim lTB As New List(Of Object)
pdf.PageSize = New Size(myXAMLcontrol.ActualWidth, myXAMLcontrol.ActualHeight)
FindTextBlocks(myXAMLcontrol, lTB)
For x = 0 To lTB.Count - 1
If TypeOf lTB(x) Is TextBlock Then
Dim TB As TextBlock = lTB(x)
Dim obj As FrameworkElement = TB
Dim angle As Double = 0
Do While obj IsNot Nothing
Dim renderxform As Transform = obj.RenderTransform
If TypeOf renderxform Is TransformGroup Then
Dim tg As TransformGroup = CType(renderxform, TransformGroup)
For Each t As Transform In tg.Children
If TypeOf t Is RotateTransform Then
angle -= CType(t, RotateTransform).Angle
End If
Next
ElseIf TypeOf renderxform Is RotateTransform Then
angle -= CType(renderxform, RotateTransform).Angle
End If
obj = obj.Parent
Loop
Dim myfont As Font
Select Case TB.FontStyle
Case FontStyle.Normal
If TB.FontWeight.Weight = FontWeights.Bold.Weight Then
myfont = New Font(TB.FontFamily.Source, TB.FontSize, PdfFontStyle.Bold)
Else
myfont = New Font(TB.FontFamily.Source, TB.FontSize, PdfFontStyle.Regular)
End If
Case Else 'FontStyle.Oblique, FontStyle.Italic '
myfont = New Font(TB.FontFamily.Source, TB.FontSize, PdfFontStyle.Italic)
End Select
Dim ttv As GeneralTransform = TB.TransformToVisual(myXAMLcontrol)
Dim ScreenCoords As Point = ttv.TransformPoint(New Point(0, 0))
Dim myWidth As Double, myHeight As Double
If TB.TextWrapping = TextWrapping.NoWrap Then
myWidth = pdf.MeasureString(TB.Text, myfont).Width
myHeight = pdf.MeasureString(TB.Text, myfont).Height
Else
myWidth = TB.ActualWidth + 10 'Admittedly, 10 is a kluge factor to make wrapping match'
myHeight = pdf.MeasureString(TB.Text, myfont, myWidth).Height
End If
Dim rc As New Rect(ScreenCoords.X, ScreenCoords.Y, myWidth, myHeight)
If angle Then
Dim fmt As New StringFormat()
fmt.Angle = angle
pdf.DrawString(TB.Text, myfont, CType(TB.Foreground, SolidColorBrush).Color, rc, fmt)
Else
pdf.DrawString(TB.Text, myfont, CType(TB.Foreground, SolidColorBrush).Color, rc)
End If
ElseIf TypeOf lTB(x) Is Border Then
Dim BDR As Border = lTB(x)
Dim ttv As GeneralTransform = BDR.TransformToVisual(myXAMLcontrol)
Dim ScreenCoords As Point = ttv.TransformPoint(New Point(0, 0))
Dim pts() As Point = {
New Point(ScreenCoords.X, ScreenCoords.Y),
New Point(ScreenCoords.X + BDR.ActualWidth, ScreenCoords.Y),
New Point(ScreenCoords.X + BDR.ActualWidth, ScreenCoords.Y + BDR.ActualHeight),
New Point(ScreenCoords.X, ScreenCoords.Y + BDR.ActualHeight)}
Dim Clr As Color = CType(BDR.BorderBrush, SolidColorBrush).Color
If BDR.BorderThickness.Top Then pdf.DrawLine(New Pen(Clr, BDR.BorderThickness.Top), pts(0), pts(1))
If BDR.BorderThickness.Right Then pdf.DrawLine(New Pen(Clr, BDR.BorderThickness.Right), pts(1), pts(2))
If BDR.BorderThickness.Bottom Then pdf.DrawLine(New Pen(Clr, BDR.BorderThickness.Bottom), pts(2), pts(3))
If BDR.BorderThickness.Left Then pdf.DrawLine(New Pen(Clr, BDR.BorderThickness.Left), pts(3), pts(0))
ElseIf TypeOf lTB(x) Is Rectangle Then
Dim Rect As Rectangle = lTB(x)
Dim ttv As GeneralTransform = Rect.TransformToVisual(myXAMLcontrol)
Dim ScreenCoords As Point = ttv.TransformPoint(New Point(0, 0))
Dim pts() As Point = {
New Point(ScreenCoords.X + Rect.Margin.Left, ScreenCoords.Y + Rect.Margin.Top),
New Point(ScreenCoords.X + Rect.ActualWidth - Rect.Margin.Right, ScreenCoords.Y + Rect.Margin.Top),
New Point(ScreenCoords.X + Rect.ActualWidth - Rect.Margin.Right, ScreenCoords.Y + Rect.ActualHeight - Rect.Margin.Bottom),
New Point(ScreenCoords.X + Rect.Margin.Left, ScreenCoords.Y + Rect.ActualHeight - Rect.Margin.Bottom)}
Dim MyPen1 As New Pen(CType(Rect.Stroke, SolidColorBrush).Color, Rect.StrokeThickness)
MyPen1.DashStyle = DashStyle.Custom
MyPen1.DashPattern = Rect.StrokeDashArray.ToArray
Dim MyPen2 As New Pen(CType(Rect.Stroke, SolidColorBrush).Color, Rect.StrokeThickness)
MyPen2.DashStyle = DashStyle.Custom
MyPen2.DashPattern = Rect.StrokeDashArray.ToArray
pdf.DrawLine(MyPen2, pts(0), pts(1))
pdf.DrawLine(MyPen1, pts(1), pts(2))
pdf.DrawLine(MyPen2, pts(2), pts(3))
pdf.DrawLine(MyPen1, pts(3), pts(0))
End If
Next
Dim file As StorageFile = Await ThisApp.AppStorageFolder.CreateFileAsync("Temp.PDF", Windows.Storage.CreationCollisionOption.ReplaceExisting)
Await pdf.SaveAsync(file)
Return True
End Function
Private Sub FindTextBlocks(uiElement As Object, foundOnes As IList(Of Object))
If TypeOf uiElement Is TextBlock Then
Dim uiElementAsTextBlock = DirectCast(uiElement, TextBlock)
If uiElementAsTextBlock.Visibility = Visibility.Visible Then
foundOnes.Add(uiElementAsTextBlock)
End If
ElseIf TypeOf uiElement Is Panel Then
Dim uiElementAsCollection = DirectCast(uiElement, Panel)
If uiElementAsCollection.Visibility = Visibility.Visible Then
For Each element In uiElementAsCollection.Children
FindTextBlocks(element, foundOnes)
Next
End If
ElseIf TypeOf uiElement Is UserControl Then
Dim uiElementAsUserControl = DirectCast(uiElement, UserControl)
If uiElementAsUserControl.Visibility = Visibility.Visible Then
FindTextBlocks(uiElementAsUserControl.Content, foundOnes)
End If
ElseIf TypeOf uiElement Is ContentControl Then
Dim uiElementAsContentControl = DirectCast(uiElement, ContentControl)
If uiElementAsContentControl.Visibility = Visibility.Visible Then
FindTextBlocks(uiElementAsContentControl.Content, foundOnes)
End If
ElseIf TypeOf uiElement Is Border Then
Dim uiElementAsBorder = DirectCast(uiElement, Border)
If uiElementAsBorder.Visibility = Visibility.Visible Then
foundOnes.Add(uiElementAsBorder)
FindTextBlocks(uiElementAsBorder.Child, foundOnes)
End If
ElseIf TypeOf uiElement Is Rectangle Then
Dim uiElementAsRectangle = DirectCast(uiElement, Rectangle)
foundOnes.Add(uiElementAsRectangle)
End If
End Sub
实际结果:
关于c# - 在 UWP 应用程序中将 XAML 转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44688156/
我想创建一个包含一堆其他 XAML 文件的主 XAML 文件。这可能吗? 最佳答案 在您的主 Xaml 中,使用 项目控件 balise 和 Xaml 组合器,您必须使用 定义您的项目用户控件 代替
如何在 xaml 中更改 ToggleSwitch 中实际开关的大小?我可以更改 OnContent 和 OffContent 的文本大小,但滑动开关永远不会更改大小。 这些都不会改变开关本身的大小,
我有一个文本 block 列表,其中可能包括里面的 url,比如: 构建失败,请在此处查看更多信息:http://... 构建成功 应用 http://myapp/无法启动,请在此处查看更多信息:ht
我想做的事 我最近一直在探索 XAML 资源字典。它们非常强大,但为了减少(甚至进一步)为适应任何修改而需要进行的更改,我想使用一些基本的算术运算来更改 HeightRequest Entry 的属性
我使用以下代码创建了一个弹出窗口,但我不知道如何将它居中 我试图在运行时自动更改边距,但我不知道该怎么做,但是有人知道如何将弹出窗口居中吗? 它没有标准维度,因为我需要全局化我的程序
哪种类型的命名更适合在 XAML 中使用: xmlns:inventoryControls="clr-namespace:Inventory.Components.Controls;assembly=
我正在尝试格式化我的 string每 3 位有逗号,如果不是整数,则为小数。我检查了大约 20 个示例,这是我最接近的示例: 但我得到一个 The property 'StringFormat' w
在 Windows Phone 8 中,我想将一个图像放在一个圆圈中。是否有类似网格的圆形容器?我知道有椭圆位它不是容器 最佳答案 这是我如何做到的。
有没有办法在 XAML 中检查逻辑非逻辑。我试图绑定(bind)到广泛使用的属性,但是对于这些新的绑定(bind),它的方式是错误的。我不想修改属性,那么有没有办法在 xaml 中反转 bool 结果
在 html 中可以说 width="20%"。这在 XAML 中当然是不允许的。有谁知道为什么会这样或者有没有办法在 XAML 中获得百分比值支持? 最佳答案 网格列定义和行定义允许比例单位(除了固
我可以动态创建 XAML 并将其弹出到我的应用程序中吗?该怎么做呢? 最佳答案 是的,这很简单: XmlReader tXml = XmlReader.Create(myXamlString); UI
简短版: 如果不使用 xamlc 预编译 xaml,应用程序中捆绑了什么以在运行时编译 xaml?它是否取决于 xamarin.forms nugets 之外的应用构建时的当前工具集? 长版: 我们有
我知道如何显示内容页面,但如何根据方向选择正确的布局。我想编写 xaml 文件:一个用于垂直方向,另一个用于水平方向。方向改变时是否可以切换xaml文件 最佳答案 Xamarin.Forms does
我是一名 Android 开发者,我是 Windows Phone 8 的新手 最近我做了一个 android 应用程序..所以我想在 Windows Phone 8 中开发具有相同 View 和相同
在库中编译 XAML 时出现以下错误: Themes\Generic.xaml(35,12): XamlCompiler error WMC0610: XBF generation error cod
我需要在几个不同的 XAML 布局文件中包含一个通用的 XAML。 我不想在其他 XAML 文件中对重复的 XAML 部分进行硬编码。 怎么做包含? 最佳答案 你能把共享代码变成UserControl
我想像这样添加一个符号: 这行不通,所以我尝试用\分隔,但也行不通。有人对我如何做到这一点有任何建议吗? 最佳答案 其实这个问题不是关于XAML的,它只是一个纯XML的问题。 XML中有些字符需要转
我在 Xaml 中定义了这样的列表。 #3599B8 #374649 #FD625E #F2C80F
在向 Microsoft 报告之前,我想在这里问一下。我有一个问题,我无法看到我的 WinUI 3 应用程序的实时可视化树。我什至看不到应用程序内工具栏。我可以在 WPF 和 UWP 应用程序中看到,
我已经定义了一个动画作为一个资源,当通过这样的 EventTrigger 调用时它可以工作: 然后
我是一名优秀的程序员,十分优秀!