Skip to content

Commit e70e3d8

Browse files
committed
Sample updated
1 parent 9d645c0 commit e70e3d8

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35617.110 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "How_to_silent_print_a_PDF_document", "How_to_silent_print_a_PDF_document\How_to_silent_print_a_PDF_document.csproj", "{C39DF450-AD95-418F-8946-A0F0FEB51117}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {A4847A47-8765-447B-BE70-E32347BCCF00}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
13+
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
14+
<PackageReference Include="System.Drawing.Common" Version="*" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Syncfusion.PdfToImageConverter;
2+
using System.Drawing;
3+
using System.Drawing.Printing;
4+
5+
class Program
6+
{
7+
static Bitmap[] bitmaps;
8+
static int currentPageIndex = 0;
9+
static void Main()
10+
{
11+
// Initialize PDF to Image converter.
12+
PdfToImageConverter imageConverter = new PdfToImageConverter();
13+
// Load the PDF document as a stream
14+
using (FileStream inputStream = new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.ReadWrite))
15+
{
16+
imageConverter.Load(inputStream);
17+
// Convert PDF to Image.
18+
Stream[] outputStream = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
19+
// Convert streams to bitmaps.
20+
bitmaps = BitmapConverter.ConvertStreamsToBitmaps(outputStream);
21+
}
22+
// Initialize PrintDocument
23+
PrintDocument printDocument = new PrintDocument();
24+
// Attach the PrintPage event handler
25+
printDocument.PrintPage += PrintDocument_PrintPage;
26+
// Print the document
27+
printDocument.Print();
28+
}
29+
private static void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
30+
{
31+
Bitmap bitmap = bitmaps[currentPageIndex];
32+
Graphics graphics = e.Graphics;
33+
// Center the image on the page without scaling
34+
float offsetX = (e.PageBounds.Width - bitmap.Width) / 2;
35+
float offsetY = (e.PageBounds.Height - bitmap.Height) / 2;
36+
graphics.DrawImage(bitmap, offsetX, offsetY);
37+
currentPageIndex++;
38+
e.HasMorePages = currentPageIndex < bitmaps.Length;
39+
if (!e.HasMorePages)
40+
{
41+
currentPageIndex = 0;
42+
}
43+
}
44+
public static class BitmapConverter
45+
{
46+
public static Bitmap[] ConvertStreamsToBitmaps(Stream[] streams)
47+
{
48+
Bitmap[] bitmaps = new Bitmap[streams.Length];
49+
for (int i = 0; i < streams.Length; i++)
50+
{
51+
bitmaps[i] = new Bitmap(streams[i]);
52+
}
53+
return bitmaps;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)