Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35617.110 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project file and folder name are mentioned wrongly in the sln file. How it will be working on your end?

EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C39DF450-AD95-418F-8946-A0F0FEB51117}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A4847A47-8765-447B-BE70-E32347BCCF00}
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Syncfusion.PdfToImageConverter.Net" Version="*" />
<PackageReference Include="System.Drawing.Common" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.Drawing.Printing;

class Program
{
static Bitmap[] bitmaps;
static int currentPageIndex = 0;
static void Main()
{
// Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
// Load the PDF document as a stream
using (FileStream inputStream = new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.ReadWrite))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify the code like below.

using Syncfusion.PdfToImageConverter;
using System.Drawing;
using System.Drawing.Printing;

// Initialize the PdfToImageConverter with the path to the PDF file
using (PdfToImageConverter imageConverter = new PdfToImageConverter())
{
    // Load the PDF document
    imageConverter.Load(new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.Read));

    //Convert all the PDF pages to images
    Stream[] images = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);

    //Create a print document object to print the images
    using (PrintDocument printDocument = new PrintDocument())
    {
        int currentPageIndex = 0;
        //Handle the PrintPage event to print each image
        printDocument.PrintPage += (sender, e) =>
        {
            Stream imageStream = images[currentPageIndex];
            imageStream.Position = 0; // Reset stream position
            Image currentImage = Image.FromStream(imageStream);
            //Draw the current image on the print page
            e.Graphics.DrawImage(currentImage, e.PageBounds);
            //Move to the next page
            currentPageIndex++;
            //Check if there are more pages to print
            e.HasMorePages = currentPageIndex < images.Length;
        };
        //Print the document
        printDocument.Print();
    }
}

{
imageConverter.Load(inputStream);
// Convert PDF to Image.
Stream[] outputStream = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
// Convert streams to bitmaps.
bitmaps = BitmapConverter.ConvertStreamsToBitmaps(outputStream);
}
// Initialize PrintDocument
PrintDocument printDocument = new PrintDocument();
// Attach the PrintPage event handler
printDocument.PrintPage += PrintDocument_PrintPage;
// Print the document
printDocument.Print();
}
private static void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bitmap = bitmaps[currentPageIndex];
Graphics graphics = e.Graphics;
// Center the image on the page without scaling
float offsetX = (e.PageBounds.Width - bitmap.Width) / 2;
float offsetY = (e.PageBounds.Height - bitmap.Height) / 2;
graphics.DrawImage(bitmap, offsetX, offsetY);
currentPageIndex++;
e.HasMorePages = currentPageIndex < bitmaps.Length;
if (!e.HasMorePages)
{
currentPageIndex = 0;
}
}
public static class BitmapConverter
{
public static Bitmap[] ConvertStreamsToBitmaps(Stream[] streams)
{
Bitmap[] bitmaps = new Bitmap[streams.Length];
for (int i = 0; i < streams.Length; i++)
{
bitmaps[i] = new Bitmap(streams[i]);
}
return bitmaps;
}
}
}
Loading