From 56e222adc848ccc223e6b1065d311ced5c82f34c Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Wed, 18 May 2022 19:49:41 +0300 Subject: Vector Fine Tuning. --- Software/Visual_Studio/Tango.PDF/PdfWpfWriter.cs | 118 +++++++++++++++++++++ .../Tango.PDF/Properties/AssemblyInfo.cs | 36 +++++++ Software/Visual_Studio/Tango.PDF/Tango.PDF.csproj | 67 ++++++++++++ Software/Visual_Studio/Tango.PDF/packages.config | 5 + 4 files changed, 226 insertions(+) create mode 100644 Software/Visual_Studio/Tango.PDF/PdfWpfWriter.cs create mode 100644 Software/Visual_Studio/Tango.PDF/Properties/AssemblyInfo.cs create mode 100644 Software/Visual_Studio/Tango.PDF/Tango.PDF.csproj create mode 100644 Software/Visual_Studio/Tango.PDF/packages.config (limited to 'Software/Visual_Studio/Tango.PDF') diff --git a/Software/Visual_Studio/Tango.PDF/PdfWpfWriter.cs b/Software/Visual_Studio/Tango.PDF/PdfWpfWriter.cs new file mode 100644 index 000000000..179b3ada0 --- /dev/null +++ b/Software/Visual_Studio/Tango.PDF/PdfWpfWriter.cs @@ -0,0 +1,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 _elements; + + public PdfWpfWriter() + { + _elements = new List(); + } + + 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 stacks = new List(); + + 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(); + } + } + } +} diff --git a/Software/Visual_Studio/Tango.PDF/Properties/AssemblyInfo.cs b/Software/Visual_Studio/Tango.PDF/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..941147f2e --- /dev/null +++ b/Software/Visual_Studio/Tango.PDF/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tango.PDF")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Tango.PDF")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("84fb2b51-213e-4602-a5db-fa97d8ae907a")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Software/Visual_Studio/Tango.PDF/Tango.PDF.csproj b/Software/Visual_Studio/Tango.PDF/Tango.PDF.csproj new file mode 100644 index 000000000..47ddba495 --- /dev/null +++ b/Software/Visual_Studio/Tango.PDF/Tango.PDF.csproj @@ -0,0 +1,67 @@ + + + + + Debug + AnyCPU + {84FB2B51-213E-4602-A5DB-FA97D8AE907A} + Library + Properties + Tango.PDF + Tango.PDF + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + + + + ..\packages\kenjiuno.PdfSharp-WPF.1.31.6209\lib\net40\HiraokaHyperTools.PdfSharp-WPF.dll + + + ..\packages\kenjiuno.PdfSharp.Xps.1.1.10\lib\net40\HiraokaHyperTools.PdfSharp.Xps.dll + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Software/Visual_Studio/Tango.PDF/packages.config b/Software/Visual_Studio/Tango.PDF/packages.config new file mode 100644 index 000000000..77755e1a5 --- /dev/null +++ b/Software/Visual_Studio/Tango.PDF/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file -- cgit v1.3.1