Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Printersettings/Printersettings.csproj" />
</Solution>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Printersettings.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Printersettings"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;

namespace Printersettings
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window x:Class="Printersettings.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Printersettings"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="PrintButton" HorizontalAlignment="Left" Margin="266,143,0,0" VerticalAlignment="Top" Click="Button_Click"/>

</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
</PropertyGroup>

<Import Project="targets\MultiTargeting.targets" />

<ItemGroup>
<PackageReference Include="Syncfusion.PdfToImageConverter.WPF" Version="*" />
<PackageReference Include="System.Drawing.Common" Version="11.0.0-preview.1.26104.118" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net462;net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnableDefaultItems>True</EnableDefaultItems>
<EnableDefaultEmbeddedResourceItems>True</EnableDefaultEmbeddedResourceItems>
</PropertyGroup>
</Project>
Binary file not shown.