Skip to content

Commit 11ee8e0

Browse files
committed
Poc Multipage Tiff to Pdf
1 parent 2b5fdfb commit 11ee8e0

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

GithubActionsHelloWorld/GithubActionsHelloWorld.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23+
<PackageReference Include="PdfSharpCore" Version="1.3.40" />
2324
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
24-
<PackageReference Include="ZXing.Net" Version="0.16.8" />
2525
</ItemGroup>
2626

2727
</Project>

GithubActionsHelloWorld/Program.cs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
1-
using SixLabors.ImageSharp;
2-
using SixLabors.ImageSharp.PixelFormats;
1+
using PdfSharpCore.Drawing;
2+
using PdfSharpCore.Fonts;
3+
using PdfSharpCore.Pdf;
4+
using PdfSharpCore.Utils;
5+
using SixLabors.ImageSharp;
6+
using System.IO;
37
using System.Threading.Tasks;
4-
using ZXing.Common;
5-
using ZXing.Rendering;
68

79
namespace GithubActionsHelloWorld
810
{
911
public class Program
1012
{
1113
public static async Task Main(string[] args)
1214
{
13-
var barcodeWriter = new ZXing.BarcodeWriterPixelData
14-
{
15-
Format = ZXing.BarcodeFormat.CODE_128,
16-
Options = new EncodingOptions
17-
{
18-
Height = 300,
19-
Width = 300,
20-
},
21-
Renderer = new PixelDataRenderer
15+
GlobalFontSettings.FontResolver = new FontResolver();
16+
17+
var document = new PdfDocument();
18+
19+
using (var tiff = Image.Load(args[0]))
20+
21+
for (var pageIndex = 0; pageIndex < tiff.Frames.Count; pageIndex++)
2222
{
23+
var pageImage = await ConvertToXImageAsync(tiff.Frames.CloneFrame(pageIndex));
24+
var pageWithImage = new PdfPage();
25+
document.Pages.Add(pageWithImage);
26+
var xgr = XGraphics.FromPdfPage(document.Pages[pageIndex]);
27+
xgr.DrawImage(pageImage, 0, 0);
2328
}
24-
};
2529

26-
var pixelData = barcodeWriter.Write("Hallo Barcode");
30+
var page = document.AddPage();
31+
var gfx = XGraphics.FromPdfPage(page);
32+
var font = new XFont("Arial", 20, XFontStyle.Bold);
33+
34+
var textColor = XBrushes.Black;
35+
var layout = new XRect(20, 20, page.Width, page.Height);
36+
var format = XStringFormats.Center;
2737

28-
using (var image = Image.LoadPixelData<Rgba32>(pixelData.Pixels, 300, 300))
29-
await image.SaveAsBmpAsync("barcode.bmp");
38+
gfx.DrawString("Hello World!", font, textColor, layout, format);
39+
40+
document.Save("helloworld.pdf");
41+
}
42+
43+
private static async Task<XImage> ConvertToXImageAsync(Image imageFrame)
44+
{
45+
using var memoryStream = new MemoryStream();
46+
await imageFrame.SaveAsPngAsync(memoryStream);
47+
memoryStream.Position = 0;
48+
var image = XImage.FromStream(() => memoryStream);
49+
return image;
3050
}
3151
}
3252
}
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
using GithubActionsHelloWorld;
2+
using System;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Reflection;
26
using System.Threading.Tasks;
37
using Xunit;
48

59
namespace GithubActionsHelloWorldTests
610
{
711
public class GithubActionsHelloWorldTest
812
{
13+
private const string expectedCreatedFilepath = "helloworld.pdf";
14+
915
[Fact]
1016
public async Task Main_ShouldRunWithoutError()
1117
{
12-
var args = new string[] { "SomeParameter" };
18+
var codeBaseUrl = new Uri(Assembly.GetExecutingAssembly().Location);
19+
var codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
20+
var dirPath = Path.GetDirectoryName(codeBasePath);
21+
var multipageTiff = Path.GetFullPath(Path.Combine(dirPath, "../../../MultiPage.tiff"));
22+
23+
if (File.Exists(expectedCreatedFilepath))
24+
{
25+
File.Delete(expectedCreatedFilepath);
26+
}
27+
28+
var args = new string[] { multipageTiff };
1329
await Program.Main(args);
30+
31+
Assert.True(File.Exists(expectedCreatedFilepath));
32+
33+
using var process = new Process();
34+
process.StartInfo.FileName = expectedCreatedFilepath;
35+
process.StartInfo.UseShellExecute = true;
36+
process.Start();
1437
}
1538
}
1639
}

GithubActionsHelloWorldTests/GithubActionsHelloWorldTests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
11-
<PackageReference Include="xunit" Version="2.4.1" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
11+
<PackageReference Include="xunit" Version="2.4.2" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
20.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)