Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Peugnet committed Sep 16, 2024
2 parents a084e52 + 880c69d commit c55d4fa
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 42 deletions.
5 changes: 5 additions & 0 deletions Blazor.Server/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<br />
<br />

<button class="btn btn-primary mb-3"
@onclick="HelloWordCombine">
Combine 2 Hello World!
</button>

<button class="btn btn-danger mb-3"
@onclick="OrderClick">
generates an order
Expand Down
38 changes: 29 additions & 9 deletions Blazor.Server/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Blazor.Server.Pages;
using Microsoft.JSInterop;

using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf;
using PdfSharpCore.Utils;

public partial class Index
Expand All @@ -16,22 +18,23 @@ public partial class Index

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
JsModule ??= await JS.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE);
if (!firstRender)
return;

JsModule ??= await JS.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE);

if (PdfSharpCore.Fonts.GlobalFontSettings.FontResolver is not FontResolver)
{
GlobalFontSettings.FontResolver = new FontResolver();
}
}
if (PdfSharpCore.Fonts.GlobalFontSettings.FontResolver is not FontResolver)
{
GlobalFontSettings.FontResolver = new FontResolver();
}

}

async Task HelloWord()
{
byte[] pdf = Share.PDF.Editions.HelloWord();

await JsModule.InvokeVoidAsync("BlazorDownloadFile", "sample.pdf", pdf);
await JsModule.InvokeVoidAsync("BlazorDownloadFile", "sample.pdf", pdf);
}

async Task DrawGraphics()
Expand Down Expand Up @@ -74,4 +77,21 @@ async Task OrderClick()

await JsModule.InvokeVoidAsync("BlazorDownloadFile", "Order.pdf", pdf);
}

async Task HelloWordCombine()
{
MemoryStream pdf1 = Share.PDF.Editions.HelloWordStream();
MemoryStream pdf2 = Share.PDF.Editions.HelloWordStream();

// Open the output document
PdfDocument combineDocument = new();

combineDocument = Share.PDF.Tools.Combine(pdf1, combineDocument);
combineDocument = Share.PDF.Tools.Combine(pdf2, combineDocument);

MemoryStream PdfStream = new();
combineDocument.Save(PdfStream);

await JsModule.InvokeVoidAsync("BlazorDownloadFile", "CombineDoc.pdf", PdfStream.ToArray());
}
}
6 changes: 6 additions & 0 deletions Blazor.Wasm/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
⛑️Unicode Sample
</button>
</p>

<br />

<button class="btn btn-info mb-3"
Expand All @@ -57,6 +58,11 @@
<br />
<br />

<button class="btn btn-primary mb-3"
@onclick="HelloWordCombine">
Combine 2 Hello World!
</button>

<button class="btn btn-danger mb-3"
@onclick="OrderClick">
generates an order
Expand Down
57 changes: 38 additions & 19 deletions Blazor.Wasm/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace Blazor.Wasm.Pages;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf;
using Share.PDF.Models;
using static System.Net.WebRequestMethods;

public partial class Index
{
Expand All @@ -20,25 +21,25 @@ public partial class Index

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
if (!firstRender)
return;

JsModule ??= await JS.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE);

Fonts font = await FontService.LoadFonts();

try
{
//if (GlobalFontSettings.FontResolver is not CustomFontResolver)
//{
//if (GlobalFontSettings.FontResolver == null)
//{
GlobalFontSettings.FontResolver = new CustomFontResolver(font);
//}
}
catch (Exception e)
{
JsModule ??= await JS.InvokeAsync<IJSObjectReference>("import", JAVASCRIPT_FILE);

Fonts font = await FontService.LoadFonts();

try
{
//if (GlobalFontSettings.FontResolver is not CustomFontResolver)
//{
//if (GlobalFontSettings.FontResolver == null)
//{
GlobalFontSettings.FontResolver = new CustomFontResolver(font);
//}
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
Console.WriteLine(e.Message.ToString());
}
}

Expand Down Expand Up @@ -92,6 +93,24 @@ async Task HelloMigraDocCoreClick()
await JsModule.InvokeVoidAsync("BlazorDownloadFile", "HelloMigraDocCore.pdf", pdf);
}

async Task HelloWordCombine()
{
MemoryStream pdf1 = Share.PDF.Editions.HelloWordStream();
MemoryStream pdf2 = Share.PDF.Editions.HelloWordStream();

// Open the output document
PdfDocument combineDocument = new();

combineDocument = Share.PDF.Tools.Combine(pdf1, combineDocument);
combineDocument = Share.PDF.Tools.Combine(pdf2, combineDocument);

MemoryStream PdfStream = new();
combineDocument.Save(PdfStream);

await JsModule.InvokeVoidAsync("BlazorDownloadFile", "CombineDoc.pdf", PdfStream.ToArray());
}


async Task OrderClick()
{
byte[] imageFile = await GetImage("images/logo-fake.png");
Expand Down
33 changes: 19 additions & 14 deletions Share.PDF/Editions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,44 @@ public static class Editions

public static byte[] HelloWord()
{
// Create Document with info
document = new();
return HelloWordStream().ToArray();
}

public static MemoryStream HelloWordStream()
{
// Create Document with info
document = new();
Common.DocumentInfo(document, "Hello world");

// Create new page
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
// Create new page
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
//XFont font = new("OpenSans-Regular", 20, XFontStyle.Regular);
XFont font = new("Arial", 20, XFontStyle.Regular);

var textColor = XBrushes.Black;
var layout = new XRect(0, 0, page.Width, page.Height);
var format = XStringFormats.Center;
var layout = new XRect(0, 0, page.Width, page.Height);
var format = XStringFormats.Center;

gfx.DrawString("Hello World!", font, textColor, layout, format);
gfx.DrawString("Hello World!", font, textColor, layout, format);

SamplePage1();

SamplePage2();
SamplePage2();

MemoryStream PdfStream = new();
document.Save(PdfStream);
document.Save(PdfStream);

return PdfStream;
}

return PdfStream.ToArray();
}







private static void DefineStyles(Document doc)
private static void DefineStyles(Document doc)
{
// Get the predefined style Normal.
Style style = doc.Styles["Normal"];
Expand Down
25 changes: 25 additions & 0 deletions Share.PDF/Tools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Share.PDF;

using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf;
public class Tools
{

public static PdfDocument Combine(MemoryStream pdf, PdfDocument combineDocument)
{
// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open(pdf, PdfDocumentOpenMode.Import);

// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
combineDocument.AddPage(page);
}

return combineDocument;
}
}

0 comments on commit c55d4fa

Please sign in to comment.