Skip to content

Commit fc97ffa

Browse files
1004655
1 parent 6d0e821 commit fc97ffa

9 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Printersettings/Printersettings.csproj" />
3+
</Solution>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="Printersettings.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Printersettings"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace Printersettings
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Window x:Class="Printersettings.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Printersettings"
7+
mc:Ignorable="d"
8+
Title="MainWindow" Height="450" Width="800">
9+
<Grid>
10+
<Button Content="PrintButton" HorizontalAlignment="Left" Margin="266,143,0,0" VerticalAlignment="Top" Click="Button_Click"/>
11+
12+
</Grid>
13+
</Window>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Syncfusion.PdfToImageConverter;
2+
using System.Drawing;
3+
using System.Drawing.Imaging;
4+
using System.Drawing.Printing;
5+
using System.IO;
6+
using System.Text;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Data;
10+
using System.Windows.Documents;
11+
using System.Windows.Input;
12+
using System.Windows.Media;
13+
using System.Windows.Media.Imaging;
14+
using System.Windows.Navigation;
15+
using System.Windows.Shapes;
16+
17+
namespace Printersettings
18+
{
19+
/// <summary>
20+
/// Interaction logic for MainWindow.xaml
21+
/// </summary>
22+
public partial class MainWindow : Window
23+
{
24+
public MainWindow()
25+
{
26+
InitializeComponent();
27+
}
28+
29+
private void Button_Click(object sender, RoutedEventArgs e)
30+
{
31+
PdfToImageConverter imageConverter = new PdfToImageConverter();
32+
//Load the PDF document as a stream
33+
FileStream inputStream = new FileStream("../../../testing.pdf", FileMode.Open, FileAccess.ReadWrite);
34+
imageConverter.Load(inputStream);
35+
//Convert PDF to Image.
36+
Stream outputStream = imageConverter.Convert(0, false, true);
37+
Bitmap image = new Bitmap(outputStream);
38+
image.Save("tmp_without.png");
39+
PrintImage("tmp_without.png");
40+
}
41+
42+
private void PrintImage(string imagePath)
43+
{
44+
using (System.Drawing.Image original = System.Drawing.Image.FromFile(imagePath))
45+
{
46+
PrintDocument printDoc = new PrintDocument();
47+
var img = original;
48+
printDoc.PrinterSettings.DefaultPageSettings.Color = true;
49+
//customize printer settings
50+
printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
51+
// Set margins
52+
printDoc.DefaultPageSettings.Margins = new Margins(100, 200, 150, 150);
53+
//Set paper source
54+
printDoc.DefaultPageSettings.PaperSource.RawKind = (int)PaperSourceKind.LargeFormat;
55+
//// Set paper kind
56+
//printDoc.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = (int)PaperKind.A4;
57+
// Set duplex printing
58+
if (printDoc.PrinterSettings.CanDuplex)
59+
printDoc.PrinterSettings.Duplex = Duplex.Horizontal;
60+
// Set printer resolution
61+
printDoc.PrinterSettings.DefaultPageSettings.PrinterResolution.Kind = PrinterResolutionKind.High;
62+
// Check if the printer supports color printing
63+
if (!printDoc.PrinterSettings.DefaultPageSettings.Color)
64+
{
65+
img = ToGrayscale(original);
66+
}
67+
printDoc.PrintPage += (s, e) =>
68+
{
69+
e.Graphics.DrawImage(img, e.MarginBounds);
70+
};
71+
printDoc.Print();
72+
}
73+
}
74+
private static System.Drawing.Image ToGrayscale(System.Drawing.Image source)
75+
{
76+
var bmp = new Bitmap(source.Width, source.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
77+
using (var g = Graphics.FromImage(bmp))
78+
{
79+
var matrix = new ColorMatrix(new float[][]
80+
{
81+
new float[] {.299f, .299f, .299f, 0, 0},
82+
new float[] {.587f, .587f, .587f, 0, 0},
83+
new float[] {.114f, .114f, .114f, 0, 0},
84+
new float[] {0, 0, 0, 1, 0},
85+
new float[] {0, 0, 0, 0, 1}
86+
});
87+
var ia = new ImageAttributes();
88+
ia.SetColorMatrix(matrix);
89+
g.DrawImage(source, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
90+
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, ia);
91+
}
92+
return bmp;
93+
}
94+
}
95+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
</PropertyGroup>
6+
7+
<Import Project="targets\MultiTargeting.targets" />
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.PdfToImageConverter.WPF" Version="*" />
11+
<PackageReference Include="System.Drawing.Common" Version="11.0.0-preview.1.26104.118" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<PropertyGroup>
3+
<TargetFrameworks>net462;net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
4+
<UseWPF>true</UseWPF>
5+
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
6+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
7+
<EnableDefaultItems>True</EnableDefaultItems>
8+
<EnableDefaultEmbeddedResourceItems>True</EnableDefaultEmbeddedResourceItems>
9+
</PropertyGroup>
10+
</Project>
Binary file not shown.

0 commit comments

Comments
 (0)