Skip to content

Task-972104-How to silent print PDF document using Syncfusion.Pdf.Net.Core Package? #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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,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}"
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))
{
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