Skip to content

Commit 31a6292

Browse files
authored
Update Program.cs
1 parent 51ef380 commit 31a6292

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

GithubActionsHelloWorld/Program.cs

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BitMiracle.LibTiff.Classic;
1+
using BitMiracle.LibTiff.Classic;
22
using PdfSharpCore.Drawing;
33
using PdfSharpCore.Fonts;
44
using PdfSharpCore.Pdf;
@@ -18,75 +18,61 @@ public static async Task Main(string[] args)
1818
GlobalFontSettings.FontResolver = new FontResolver();
1919

2020
var document = new PdfDocument();
21-
var fileStream = File.OpenRead(args[0]); ;
22-
23-
var pageImages = tiffToBitmap(args[0]);
24-
for (int pageIndex = 0; pageIndex < pageImages.Count; pageIndex++)
21+
using var fileStream = File.OpenRead(args[0]);
2522

23+
var tempImageFiles = TiffToBitmap(args[0]);
24+
try
2625
{
27-
var pageWithImage = new PdfPage();
28-
document.Pages.Add(pageWithImage);
29-
var xgr = XGraphics.FromPdfPage(document.Pages[pageIndex]);
30-
31-
var xImage = XImage.FromStream(() => File.OpenRead(pageImages[pageIndex]));
32-
xgr.DrawImage(xImage, 0, 0);
33-
}
34-
35-
var page = document.AddPage();
36-
var gfx = XGraphics.FromPdfPage(page);
37-
var font = new XFont("Arial", 20, XFontStyle.Bold);
38-
39-
var textColor = XBrushes.Black;
40-
var layout = new XRect(20, 20, page.Width, page.Height);
41-
var format = XStringFormats.Center;
26+
for (int pageIndex = 0; pageIndex < tempImageFiles.Count; pageIndex++)
27+
{
28+
var pageWithImage = new PdfPage();
29+
document.Pages.Add(pageWithImage);
30+
var xgr = XGraphics.FromPdfPage(document.Pages[pageIndex]);
4231

43-
gfx.DrawString("Hello World!", font, textColor, layout, format);
32+
var xImage = XImage.FromStream(() => File.OpenRead(tempImageFiles[pageIndex]));
33+
xgr.DrawImage(xImage, 0, 0);
34+
}
4435

45-
document.Save("helloworld.pdf");
36+
document.Save("helloworld.pdf");
37+
}
38+
finally
39+
{
40+
foreach (var tempImageFile in tempImageFiles)
41+
{
42+
File.Delete(tempImageFile);
43+
}
44+
}
4645
}
4746

48-
private static List<string> tiffToBitmap(string fileName)
47+
private static List<string> TiffToBitmap(string tiffFilePath)
4948
{
5049
var tempPaths = new List<string>();
51-
52-
using var tiff = Tiff.Open(fileName, "r");
53-
54-
var numberIfTiffPages = getNumberofTiffPages(tiff);
50+
using var tiff = Tiff.Open(tiffFilePath, "r");
51+
var numberIfTiffPages = GetNumberofTiffPages(tiff);
5552

5653
for (short i = 0; i < numberIfTiffPages; i++)
5754
{
5855
tiff.SetDirectory(i);
59-
60-
// read the dimensions
6156
var width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
6257
var height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
63-
64-
// create the bitmap
6558
var bitmap = new SKBitmap();
6659
var info = new SKImageInfo(width, height);
67-
68-
// create the buffer that will hold the pixels
6960
var raster = new int[width * height];
70-
71-
// get a pointer to the buffer, and give it to the bitmap
7261
var ptr = GCHandle.Alloc(raster, GCHandleType.Pinned);
73-
bitmap.InstallPixels(info, ptr.AddrOfPinnedObject(), info.RowBytes, null, (addr, ctx) => ptr.Free(), null);
62+
bitmap.InstallPixels(info, ptr.AddrOfPinnedObject(), info.RowBytes, (addr, ctx) => ptr.Free(), null);
7463

75-
// read the image into the memory buffer
7664
if (!tiff.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT))
7765
{
7866
// not a valid TIF image.
7967
return null;
8068
}
8169

82-
// swap the red and blue because SkiaSharp may differ from the tiff
8370
if (SKImageInfo.PlatformColorType == SKColorType.Bgra8888)
8471
{
8572
SKSwizzle.SwapRedBlue(ptr.AddrOfPinnedObject(), raster.Length);
8673
}
8774

8875
var encodedData = bitmap.Encode(SKEncodedImageFormat.Png, 100);
89-
9076
var tempPath = Path.Combine(Path.GetTempFileName() + ".png");
9177
using var bitmapImageStream = File.Open(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
9278
encodedData.SaveTo(bitmapImageStream);
@@ -95,7 +81,7 @@ private static List<string> tiffToBitmap(string fileName)
9581
return tempPaths;
9682
}
9783

98-
public static int getNumberofTiffPages(Tiff image)
84+
public static int GetNumberofTiffPages(Tiff image)
9985
{
10086
int pageCount = 0;
10187
do
@@ -106,4 +92,4 @@ public static int getNumberofTiffPages(Tiff image)
10692
return pageCount;
10793
}
10894
}
109-
}
95+
}

0 commit comments

Comments
 (0)