C
C#2mo ago
apnxmammoth

✅ PdfPig

I don't know what I am doing wrong. I am trying to generate a PDF document and attach an image (PNG) to the PDF Document. The problem is that I keep getting an index out of range exception. I did the math, and the points I use for the image coordinates are correct (At least, I am 98% sure). This is what I have:
private const double POINT_VALUE = 2.83465f;
private const double MARGIN = 10 * POINT_VALUE;
private const double PAGE_WIDTH = 60 * POINT_VALUE; // 170.079 points
private const double PAGE_HEIGHT = 108 * POINT_VALUE; // 306.142 points
private const double IMAGE_WIDTH = 40 * POINT_VALUE; // 113.386 points
private const double IMAGE_HEIGHT = 40 * POINT_VALUE; // 113.386 points
private const double IMAGE_X1 = MARGIN;
private const double IMAGE_Y1 = PAGE_HEIGHT - MARGIN - IMAGE_HEIGHT;
private const double IMAGE_X2 = IMAGE_X1 + IMAGE_WIDTH;
private const double IMAGE_Y2 = PAGE_HEIGHT - MARGIN;

public byte[] GeneratePrintableLabel(LogisticsViewModel model)
{
PdfDocumentBuilder builder = new PdfDocumentBuilder();
var fontBytes = File.ReadAllBytes("wwwroot/assets/fonts/la-regular-400.ttf");
var font = builder.AddTrueTypeFont(fontBytes);

var page = builder.AddPage(PAGE_WIDTH, PAGE_HEIGHT);

var qrCodeBytes = GenerateQrCode($"{model.Description}|{model.PalletNumber}|{model.Batch}|{model.Date:dd/MM/yyyy}|{model.Quantity}");

var qrPlacement = new PdfRectangle(IMAGE_X1, IMAGE_Y1, IMAGE_X2, IMAGE_Y2);
page.AddPng(qrCodeBytes, qrPlacement);

page.AddText($"Description: {model.Description}", 12, new PdfPoint(IMAGE_X2 + MARGIN, PAGE_HEIGHT - MARGIN - 20), font);
page.AddText($"Quantity: {model.Quantity}", 12, new PdfPoint(IMAGE_X2 + MARGIN, PAGE_HEIGHT - MARGIN - 40), font);

var pdfDocument = builder.Build();
using (var stream = new MemoryStream())
{
return stream.ToArray();
}
}
private const double POINT_VALUE = 2.83465f;
private const double MARGIN = 10 * POINT_VALUE;
private const double PAGE_WIDTH = 60 * POINT_VALUE; // 170.079 points
private const double PAGE_HEIGHT = 108 * POINT_VALUE; // 306.142 points
private const double IMAGE_WIDTH = 40 * POINT_VALUE; // 113.386 points
private const double IMAGE_HEIGHT = 40 * POINT_VALUE; // 113.386 points
private const double IMAGE_X1 = MARGIN;
private const double IMAGE_Y1 = PAGE_HEIGHT - MARGIN - IMAGE_HEIGHT;
private const double IMAGE_X2 = IMAGE_X1 + IMAGE_WIDTH;
private const double IMAGE_Y2 = PAGE_HEIGHT - MARGIN;

public byte[] GeneratePrintableLabel(LogisticsViewModel model)
{
PdfDocumentBuilder builder = new PdfDocumentBuilder();
var fontBytes = File.ReadAllBytes("wwwroot/assets/fonts/la-regular-400.ttf");
var font = builder.AddTrueTypeFont(fontBytes);

var page = builder.AddPage(PAGE_WIDTH, PAGE_HEIGHT);

var qrCodeBytes = GenerateQrCode($"{model.Description}|{model.PalletNumber}|{model.Batch}|{model.Date:dd/MM/yyyy}|{model.Quantity}");

var qrPlacement = new PdfRectangle(IMAGE_X1, IMAGE_Y1, IMAGE_X2, IMAGE_Y2);
page.AddPng(qrCodeBytes, qrPlacement);

page.AddText($"Description: {model.Description}", 12, new PdfPoint(IMAGE_X2 + MARGIN, PAGE_HEIGHT - MARGIN - 20), font);
page.AddText($"Quantity: {model.Quantity}", 12, new PdfPoint(IMAGE_X2 + MARGIN, PAGE_HEIGHT - MARGIN - 40), font);

var pdfDocument = builder.Build();
using (var stream = new MemoryStream())
{
return stream.ToArray();
}
}
For context: X1 and Y1 = Bottom Left coordinates and X2 and Y2 is top right.
7 Replies
Anton
Anton2mo ago
which line are you getting that at?
jcotton42
jcotton422mo ago
@apnxmammoth share the exact line and error message you get that on
apnxmammoth
apnxmammothOP2mo ago
page.AddPng(qrCodeBytes, qrPlacement); Sorry Anton. I didn't get notified when you messaged, but at page.AddPng()
jcotton42
jcotton422mo ago
and the full exception?
apnxmammoth
apnxmammothOP2mo ago
System.IndexOutOfRangeException: Index was outside the bounds of the array. at UglyToad.PdfPig.Images.Png.RawPngData.GetPixel(Int32 x, Int32 y) at UglyToad.PdfPig.Writer.PdfPageBuilder.AddPng(Stream pngStream, PdfRectangle placementRectangle) at UglyToad.PdfPig.Writer.PdfPageBuilder.AddPng(Byte[] pngBytes, PdfRectangle placementRectangle) at PrintHelper.GeneratePrintableLabel(LogisticsViewModel model) in Helper\PrintHelper.cs:line 23 at LogisticsController.Create(LogisticsViewModel model) in Controllers\LogisticsController.cs:line 51 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker... Unable to send full exception, character limit.
Joreyk ( IXLLEGACYIXL )
put it into a gist https://gist.github.com/
Gist
Discover gists
GitHub Gist: instantly share code, notes, and snippets.
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server