Skip to content

Commit 6efe8e8

Browse files
committed
Resolved issues in the KB
1 parent e70e3d8 commit 6efe8e8

File tree

3 files changed

+24
-71
lines changed

3 files changed

+24
-71
lines changed

Document conversion/PDF-to-Image/How-to-silent-print-a-PDF-document/How-to-silent-print-a-PDF-document.sln

Lines changed: 0 additions & 25 deletions
This file was deleted.

Document conversion/PDF-to-Image/How-to-silent-print-a-PDF-document/How-to-silent-print-a-PDF-document/Program.cs

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,33 @@
22
using System.Drawing;
33
using System.Drawing.Printing;
44

5-
class Program
5+
// Initialize the PdfToImageConverter with the path to the PDF file
6+
using (PdfToImageConverter imageConverter = new PdfToImageConverter())
67
{
7-
static Bitmap[] bitmaps;
8-
static int currentPageIndex = 0;
9-
static void Main()
8+
// Load the PDF document
9+
imageConverter.Load(new FileStream("Data/Input.pdf", FileMode.Open, FileAccess.Read));
10+
11+
//Convert all the PDF pages to images
12+
Stream[] images = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
13+
14+
//Create a print document object to print the images
15+
using (PrintDocument printDocument = new PrintDocument())
1016
{
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))
17+
int currentPageIndex = 0;
18+
//Handle the PrintPage event to print each image
19+
printDocument.PrintPage += (sender, e) =>
1520
{
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
21+
Stream imageStream = images[currentPageIndex];
22+
imageStream.Position = 0; // Reset stream position
23+
Image currentImage = Image.FromStream(imageStream);
24+
//Draw the current image on the print page
25+
e.Graphics.DrawImage(currentImage, e.PageBounds);
26+
//Move to the next page
27+
currentPageIndex++;
28+
//Check if there are more pages to print
29+
e.HasMorePages = currentPageIndex < images.Length;
30+
};
31+
//Print the document
2732
printDocument.Print();
2833
}
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-
}
5634
}

0 commit comments

Comments
 (0)