Skip to content
Merged
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,109 @@
using Syncfusion.PdfToImageConverter;
using System.Collections.Generic;
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)
{
List<System.Drawing.Image> pages = ConvertPdfToImages("../../../testing.pdf");
PrintImage(pages);
}

private List<System.Drawing.Image> ConvertPdfToImages(string pdfPath)
{
PdfToImageConverter converter = new PdfToImageConverter();

FileStream fs = new FileStream(pdfPath, FileMode.Open, FileAccess.Read);
converter.Load(fs);

List<System.Drawing.Image> images = new List<System.Drawing.Image>();

for (int i = 0; i < converter.PageCount; i++)
{
Stream stream = converter.Convert(i, false, false);
images.Add(System.Drawing.Image.FromStream(stream));
}

return images;
}

private void PrintImage(List<System.Drawing.Image> images)
{

PrintDocument printDoc = new PrintDocument();
int pageIndex = 0;
printDoc.DefaultPageSettings.Color = true;
printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
printDoc.DefaultPageSettings.Margins = new Margins(100, 200, 150, 150);
printDoc.DefaultPageSettings.PaperSource.RawKind = (int)PaperSourceKind.LargeFormat;

if (printDoc.PrinterSettings.CanDuplex)
printDoc.PrinterSettings.Duplex = Duplex.Horizontal;

printDoc.PrinterSettings.DefaultPageSettings.PrinterResolution.Kind =
PrinterResolutionKind.High;

printDoc.PrintPage += (s, e) =>
{
System.Drawing.Image img = images[pageIndex];

if (!e.PageSettings.Color)
img = ToGrayscale(img);

e.Graphics.DrawImage(img, e.MarginBounds);

pageIndex++;
e.HasMorePages = pageIndex < images.Count;
};

printDoc.Print();
foreach (var img in images)
img.Dispose();

}
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.
Loading