Guest User

PdfSharpTransformTest

a guest
Nov 24th, 2016
212
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Documents;
  5. using System.Windows.Markup;
  6. using System.Windows.Media;
  7. using System.Windows.Xps;
  8. using System.Windows.Xps.Packaging;
  9. using PdfSharp.Xps;
  10. using Size = System.Drawing.Size;
  11.  
  12. namespace TransformTest
  13. {
  14.    public class Program
  15.    {
  16.       [STAThread]
  17.       public static void Main(string[] args)
  18.       {
  19.          PdfSharpTransformTest();
  20.       }
  21.  
  22.       private static void PdfSharpTransformTest()
  23.       {
  24.          var visual = new DrawingVisual();
  25.          DrawingContext dc = visual.RenderOpen();
  26.  
  27.          // Setup transformations.
  28.          dc.PushTransform(new TranslateTransform(0, 1122.0));
  29.          dc.PushTransform(new ScaleTransform(3.77857136726379, -3.77857136726379));
  30.          dc.PushTransform(new TranslateTransform(-1719.41186523438, -1410.32360839844));
  31.          dc.PushTransform(new ScaleTransform(0.0117647061124444, 0.0117647061124444));
  32.  
  33.          // Draw red rectangle.
  34.          var redPen = new Pen(Brushes.Red, 1);
  35.          var rectGeo1 = new RectangleGeometry(new Rect(160000, 130000, 8000, 5000));
  36.          dc.DrawGeometry(Brushes.Transparent, redPen, rectGeo1);
  37.  
  38.          // Pop two transformations.
  39.          dc.Pop();
  40.          dc.Pop();
  41.          
  42.          // Draw blue rectangle.
  43.          var bluePen = new Pen(Brushes.Blue, 0.5);
  44.          var rectGeo2 = new RectangleGeometry(new Rect(12, 12, 150.9408, 107.088539));
  45.          dc.DrawGeometry(Brushes.Transparent, bluePen, rectGeo2);
  46.          dc.Close();
  47.  
  48.          // Generate XPS from DocumentSequence.
  49.          var a3Size = new Size(1587, 1123);
  50.          var docSeq = CreateDocumentSequence(a3Size, visual);
  51.          string dirPath = @"C:\Temp\";
  52.          string xpsFilePath = dirPath + "test.xps";
  53.          string pdfFilePath = dirPath + "test.pdf";
  54.          WriteXpsFile(docSeq, xpsFilePath);
  55.  
  56.          // Generate PDF from XPS (PdfSharp)
  57.          XpsConverter.Convert(xpsFilePath, pdfFilePath, 0);
  58.          Console.WriteLine("Done.");
  59.          Console.ReadLine();
  60.       }
  61.  
  62.       private static void WriteXpsFile(FixedDocumentSequence docSeq, string xpsFilePath)
  63.       {
  64.          if (File.Exists(xpsFilePath))
  65.             File.Delete(xpsFilePath);
  66.          var xpsd = new XpsDocument(xpsFilePath, FileAccess.ReadWrite);
  67.          XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
  68.          xw.Write(docSeq);
  69.          xpsd.Close();
  70.       }
  71.  
  72.       private static FixedDocumentSequence CreateDocumentSequence(Size fdSize, DrawingVisual visual)
  73.       {
  74.          var docSeq = new FixedDocumentSequence();
  75.          var doc = CreateFixedDocument(fdSize, new DrawingVisualPresenter(visual));
  76.          var reference = new DocumentReference();
  77.          reference.SetDocument(doc);
  78.          docSeq.References.Add(reference);
  79.          return docSeq;
  80.       }
  81.  
  82.       private static FixedDocument CreateFixedDocument(Size paperSize, UIElement elementToAdd)
  83.       {
  84.          var doc = new FixedDocument();
  85.          var page = new FixedPage();
  86.          page.Width = paperSize.Width;
  87.          page.Height = paperSize.Height;
  88.  
  89.          var pageContent = new PageContent();
  90.          ((IAddChild)pageContent).AddChild(page);
  91.          doc.Pages.Add(pageContent);
  92.  
  93.          page.Children.Add(elementToAdd);
  94.          return doc;
  95.       }
  96.    }
  97.    
  98.    public class DrawingVisualPresenter : FrameworkElement
  99.    {
  100.       private readonly DrawingVisual visual;
  101.  
  102.       public DrawingVisualPresenter(DrawingVisual visual)
  103.       {
  104.          this.visual = visual;
  105.       }
  106.  
  107.       protected override int VisualChildrenCount
  108.       {
  109.          get { return 1; }
  110.       }
  111.  
  112.       protected override Visual GetVisualChild(int index)
  113.       {
  114.          if (index != 0)
  115.             throw new ArgumentOutOfRangeException("index");
  116.          return visual;
  117.       }
  118.    }
  119. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×