|
4 | 4 | using Syncfusion.Pdf;
|
5 | 5 | using Syncfusion.Pdf.Graphics;
|
6 | 6 | using Syncfusion.Drawing;
|
| 7 | +using SkiaSharp; |
| 8 | +using Microsoft.AspNetCore.Mvc.RazorPages; |
7 | 9 |
|
8 | 10 | namespace DifferentFormatsImageintoPDF.Controllers
|
9 | 11 | {
|
@@ -32,38 +34,29 @@ public IActionResult DifferentFormatsImagetoPDF()
|
32 | 34 | foreach (var file in files)
|
33 | 35 | {
|
34 | 36 | string extension = Path.GetExtension(file);
|
35 |
| - |
| 37 | + //Load the image from the disk |
| 38 | + FileStream imageStream = new FileStream(file, FileMode.Open, FileAccess.Read); |
| 39 | + PdfImage image; |
36 | 40 | if (extension == ".jpg" || extension == ".png" || extension == ".jpeg")
|
37 | 41 | {
|
38 |
| - //Add a page to the document |
39 |
| - PdfPage page = doc.Pages.Add(); |
40 |
| - |
41 |
| - //Create PDF graphics for the page |
42 |
| - PdfGraphics graphics = page.Graphics; |
43 |
| - |
44 |
| - //Load the image from the disk |
45 |
| - FileStream imageStream = new FileStream(file, FileMode.Open, FileAccess.Read); |
46 |
| - PdfBitmap image = new PdfBitmap(imageStream); |
47 |
| - |
48 |
| - //Draw the image |
49 |
| - graphics.DrawImage(image, new PointF(0, 0), new SizeF(400, 600)); |
| 42 | + image = new PdfBitmap(imageStream); |
50 | 43 | }
|
51 |
| - |
52 |
| - else if (extension == ".tiff" || extension == ".tif" || extension == ".bmp" || extension == ".gif") |
| 44 | + else |
53 | 45 | {
|
54 |
| - //Add a page to the document |
55 |
| - PdfPage page = doc.Pages.Add(); |
56 |
| - |
57 |
| - //Create PDF graphics for the page |
58 |
| - PdfGraphics graphics = page.Graphics; |
59 |
| - |
60 |
| - //Load the TIFF image |
61 |
| - FileStream imageStream = new FileStream(file, FileMode.Open, FileAccess.Read); |
62 |
| - PdfTiffImage image = new PdfTiffImage(imageStream); |
63 |
| - |
64 |
| - //Draw the image |
65 |
| - graphics.DrawImage(image, new PointF(0, 0), new SizeF(400, 600)); |
| 46 | + image = new PdfTiffImage(imageStream); |
66 | 47 | }
|
| 48 | + |
| 49 | + //Add section |
| 50 | + PdfSection section = doc.Sections.Add(); |
| 51 | + //Set margin |
| 52 | + section.PageSettings.Margins.All = 0; |
| 53 | + //Set page size as image size. |
| 54 | + section.PageSettings.Size = new SizeF(image.Width, image.Height); |
| 55 | + //Add page to the section and initialize graphics for the page |
| 56 | + PdfPage page = section.Pages.Add(); |
| 57 | + //Create PDF graphics for the page |
| 58 | + PdfGraphics graphics = page.Graphics; |
| 59 | + graphics.DrawImage(image, new PointF(0, 0), new SizeF(image.Width, image.Height)); |
67 | 60 | }
|
68 | 61 |
|
69 | 62 | //Saving the PDF to the MemoryStream
|
|
0 commit comments