-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
95 lines (92 loc) · 3.79 KB
/
MainWindow.xaml.cs
File metadata and controls
95 lines (92 loc) · 3.79 KB
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
using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Text;
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;
namespace Printersettings
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("../../../testing.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream outputStream = imageConverter.Convert(0, false, false);
Bitmap image = new Bitmap(outputStream);
image.Save("tmp_without.png");
PrintImage("tmp_without.png");
}
private void PrintImage(string imagePath)
{
using (System.Drawing.Image original = System.Drawing.Image.FromFile(imagePath))
{
PrintDocument printDoc = new PrintDocument();
var img = original;
printDoc.PrinterSettings.DefaultPageSettings.Color = true;
//customize printer settings
printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
// Set margins
printDoc.DefaultPageSettings.Margins = new Margins(100, 200, 150, 150);
//Set paper source
printDoc.DefaultPageSettings.PaperSource.RawKind = (int)PaperSourceKind.LargeFormat;
//// Set paper kind
//printDoc.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = (int)PaperKind.A4;
// Set duplex printing
if (printDoc.PrinterSettings.CanDuplex)
printDoc.PrinterSettings.Duplex = Duplex.Horizontal;
// Set printer resolution
printDoc.PrinterSettings.DefaultPageSettings.PrinterResolution.Kind = PrinterResolutionKind.High;
// Check if the printer supports color printing
if (!printDoc.PrinterSettings.DefaultPageSettings.Color)
{
img = ToGrayscale(original);
}
printDoc.PrintPage += (s, e) =>
{
e.Graphics.DrawImage(img, e.MarginBounds);
};
printDoc.Print();
}
}
private static System.Drawing.Image ToGrayscale(System.Drawing.Image source)
{
var bmp = new Bitmap(source.Width, source.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (var g = Graphics.FromImage(bmp))
{
var matrix = new ColorMatrix(new float[][]
{
new float[] {.299f, .299f, .299f, 0, 0},
new float[] {.587f, .587f, .587f, 0, 0},
new float[] {.114f, .114f, .114f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
var ia = new ImageAttributes();
ia.SetColorMatrix(matrix);
g.DrawImage(source, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia);
}
return bmp;
}
}
}