blob: 179b3ada06a5c7e91d5800578e8545d4dfdd6e9b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
using PdfSharp;
using PdfSharp.Drawing;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
namespace Tango.PDF
{
public class PdfWpfWriter
{
private List<FrameworkElement> _elements;
public PdfWpfWriter()
{
_elements = new List<FrameworkElement>();
}
public void AddElement(FrameworkElement element)
{
_elements.Add(element);
}
public void RemoveElement(FrameworkElement element)
{
_elements.Remove(element);
}
public Size GetA4Size()
{
XSize a4Size = PageSizeConverter.ToSize(PageSize.A4);
return new Size(a4Size.Width, a4Size.Height);
}
public void Save(String filePath)
{
using (MemoryStream lMemoryStream = new MemoryStream())
{
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
List<StackPanel> stacks = new List<StackPanel>();
StackPanel currentStack = new StackPanel();
stacks.Add(currentStack);
XSize a4Size = PageSizeConverter.ToSize(PageSize.A4);
foreach (var element in _elements.ToList())
{
//element.Width = a4Size.Width * 0.80;
currentStack.Children.Add(element);
currentStack.Measure(new Size(Double.MaxValue, Double.MaxValue));
Size visualSize = currentStack.DesiredSize;
currentStack.Arrange(new Rect(new Point(0, 0), visualSize));
currentStack.UpdateLayout();
if (visualSize.Height > a4Size.Height)
{
currentStack.Children.Remove(element);
currentStack.UpdateLayout();
currentStack.InvalidateVisual();
currentStack = new StackPanel();
currentStack.Children.Add(element);
stacks.Add(currentStack);
}
}
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => { })).Wait();
FixedDocument fixedDoc = new FixedDocument();
foreach (var stack in stacks)
{
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.Height = a4Size.Height;
fixedPage.Width = a4Size.Width;
fixedPage.Children.Add(stack);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
}
writer.Write(fixedDoc);
doc.Close();
package.Close();
MemoryStream outStream = new MemoryStream();
PdfSharp.Xps.XpsConverter.Convert(lMemoryStream, outStream, true);
File.WriteAllBytes(filePath, outStream.ToArray());
lMemoryStream.Dispose();
outStream.Dispose();
}
}
}
}
|