From 4d8d2a6f2b1be754094ed44c9b0742ac45071d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20Mart=C3=ADnez=20Pineda?= Date: Tue, 21 Oct 2025 13:37:51 +0200 Subject: [PATCH 1/5] Adding TryFunction when loading the PDF's preview image --- .../src/Document/Inbound/InboundEDocPicture.Page.al | 5 +++-- .../App/Pdf/src/PDFDocument.Codeunit.al | 5 +++-- .../App/Pdf/src/PDFDocumentImpl.Codeunit.al | 13 +++++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al b/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al index 1d2dc77f0e..59bf41f503 100644 --- a/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al +++ b/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al @@ -41,6 +41,7 @@ page 6111 "Inbound E-Doc. Picture" PdfStream, ImageStream : InStream; EDocDataStorageImageDescriptionLbl: Label 'Pdf Preview'; begin + Clear(TempMediaRepository); if Rec."Entry No." <> xRec."Entry No." then PdfLoaded := false; @@ -55,8 +56,8 @@ page 6111 "Inbound E-Doc. Picture" Rec."Data Storage".CreateInStream(PdfStream, TextEncoding::UTF8); if PdfDocument.Load(PdfStream) then begin TempBlob.CreateInStream(ImageStream, TextEncoding::UTF8); - PdfDocument.ConvertToImage(ImageStream, "Image Format"::Png, 1); - TempMediaRepository.Image.ImportStream(ImageStream, EDocDataStorageImageDescriptionLbl, 'image/png'); + if PdfDocument.ConvertToImage(ImageStream, "Image Format"::Png, 1) then + TempMediaRepository.Image.ImportStream(ImageStream, EDocDataStorageImageDescriptionLbl, 'image/png'); end; end; diff --git a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al index dc4d4e4120..863efa725d 100644 --- a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al +++ b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al @@ -44,9 +44,10 @@ codeunit 3110 "PDF Document" /// Stream of the image file. /// Image format to convert the PDF to. /// Page number to convert. - procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer) + /// Whether or not the conversion was successful. + procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer): Boolean begin - PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber); + exit(PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber)); end; /// diff --git a/src/System Application/App/Pdf/src/PDFDocumentImpl.Codeunit.al b/src/System Application/App/Pdf/src/PDFDocumentImpl.Codeunit.al index 45a957ecbd..16f6f7b701 100644 --- a/src/System Application/App/Pdf/src/PDFDocumentImpl.Codeunit.al +++ b/src/System Application/App/Pdf/src/PDFDocumentImpl.Codeunit.al @@ -53,7 +53,6 @@ codeunit 3109 "PDF Document Impl." procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer): Boolean var - PdfConverterInstance: DotNet PdfConverter; PdfTargetDevice: DotNet PdfTargetDevice; MemoryStream: DotNet MemoryStream; ImageMemoryStream: DotNet MemoryStream; @@ -72,7 +71,9 @@ codeunit 3109 "PDF Document Impl." CopyStream(MemoryStream, SharedDocumentStream); ConvertImageFormatToPdfTargetDevice(ImageFormat, PdfTargetDevice); - ImageMemoryStream := PdfConverterInstance.ConvertPage(PdfTargetDevice, MemoryStream, PageNumber, 0, 0, 0); // apply default height, width and resolution + if not TryToConvertPage(PdfTargetDevice, MemoryStream, PageNumber, ImageMemoryStream) then + exit(false); + // Copy data to the outgoing stream and make sure it is reset to the beginning of the stream. ImageMemoryStream.Seek(0, 0); ImageMemoryStream.CopyTo(ImageStream); @@ -80,6 +81,14 @@ codeunit 3109 "PDF Document Impl." exit(true) end; + [TryFunction] + local procedure TryToConvertPage(var PdfTargetDevice: DotNet PdfTargetDevice; var MemoryStream: DotNet MemoryStream; PageNumber: Integer; var ImageMemoryStream: DotNet MemoryStream) + var + PdfConverterInstance: DotNet PdfConverter; + begin + ImageMemoryStream := PdfConverterInstance.ConvertPage(PdfTargetDevice, MemoryStream, PageNumber, 0, 0, 0); // apply default height, width and resolution + end; + local procedure ConvertImageFormatToPdfTargetDevice(ImageFormat: Enum "Image Format"; var PdfTargetDevice: DotNet PdfTargetDevice) begin case ImageFormat of From 57ece244784e76ddf9ea8d7f1b00fd1c06bbaa52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20Mart=C3=ADnez=20Pineda?= Date: Tue, 21 Oct 2025 14:17:45 +0200 Subject: [PATCH 2/5] overprotective cop --- .../App/Pdf/src/PDFDocument.Codeunit.al | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al index 863efa725d..b336296e46 100644 --- a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al +++ b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al @@ -45,11 +45,25 @@ codeunit 3110 "PDF Document" /// Image format to convert the PDF to. /// Page number to convert. /// Whether or not the conversion was successful. - procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer): Boolean + procedure ConvertPdfToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer): Boolean begin exit(PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber)); end; +#if not CLEAN27 + + /// + /// This procedure is used to convert a PDF file to an image. + /// + /// Stream of the image file. + /// Image format to convert the PDF to. + /// Page number to convert. + [Obsolete('Use the ConvertPdfToImage procedure instead.', '27.1')] + procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer) + begin + PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber); + end; +#endif /// /// This procedure is used to get the invoice attachment stream from a PDF file. /// From d9f40a2ea393bf7e4ac554e1314d9c116bd4739c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20Mart=C3=ADnez=20Pineda?= Date: Tue, 21 Oct 2025 14:18:33 +0200 Subject: [PATCH 3/5] overprotective cop --- .../App/src/Document/Inbound/InboundEDocPicture.Page.al | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al b/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al index 59bf41f503..c319272d0b 100644 --- a/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al +++ b/src/Apps/W1/EDocument/App/src/Document/Inbound/InboundEDocPicture.Page.al @@ -56,7 +56,7 @@ page 6111 "Inbound E-Doc. Picture" Rec."Data Storage".CreateInStream(PdfStream, TextEncoding::UTF8); if PdfDocument.Load(PdfStream) then begin TempBlob.CreateInStream(ImageStream, TextEncoding::UTF8); - if PdfDocument.ConvertToImage(ImageStream, "Image Format"::Png, 1) then + if PdfDocument.ConvertPdfToImage(ImageStream, "Image Format"::Png, 1) then TempMediaRepository.Image.ImportStream(ImageStream, EDocDataStorageImageDescriptionLbl, 'image/png'); end; end; From 7448ff80e128c88fa0dd51238ee406e4105d8ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20Mart=C3=ADnez=20Pineda?= Date: Tue, 21 Oct 2025 14:31:21 +0200 Subject: [PATCH 4/5] as0072 disabled --- src/System Application/App/Pdf/src/PDFDocument.Codeunit.al | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al index b336296e46..b60dd2c898 100644 --- a/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al +++ b/src/System Application/App/Pdf/src/PDFDocument.Codeunit.al @@ -51,7 +51,7 @@ codeunit 3110 "PDF Document" end; #if not CLEAN27 - +#pragma warning disable AS0072 // this will be backported: #610559 /// /// This procedure is used to convert a PDF file to an image. /// @@ -63,6 +63,7 @@ codeunit 3110 "PDF Document" begin PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber); end; +#pragma warning restore AS0072 #endif /// /// This procedure is used to get the invoice attachment stream from a PDF file. From 731d5f8a53adff62cd29e76cff4ad3bc52e2544c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20Mart=C3=ADnez=20Pineda?= Date: Tue, 21 Oct 2025 15:56:04 +0200 Subject: [PATCH 5/5] missing usages of ConvertToImage --- src/System Application/App/Pdf/README.md | 2 +- src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System Application/App/Pdf/README.md b/src/System Application/App/Pdf/README.md index 94614a7019..c4c0aa971d 100644 --- a/src/System Application/App/Pdf/README.md +++ b/src/System Application/App/Pdf/README.md @@ -137,7 +137,7 @@ procedure Example(ImageStream: InStream) var PDFDocument: Codeunit "PDF Document"; begin - PDFDocument.ConvertToImage(ImageStream, Enum::"Image Format"::PNG, 1); + PDFDocument.ConvertPdfToImage(ImageStream, Enum::"Image Format"::PNG, 1); end; ``` diff --git a/src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al b/src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al index 05560a10f5..0e971f5f13 100644 --- a/src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al +++ b/src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al @@ -30,7 +30,7 @@ codeunit 132601 "PDF Document Test" NavApp.GetResource('test.pdf', PdfInstream, TextEncoding::UTF8); TempBlob.CreateInStream(ImageStream); PdfDocument.Load(PdfInstream); - PdfDocument.ConvertToImage(ImageStream, ImageFormat::Png, 1); + PdfDocument.ConvertPdfToImage(ImageStream, ImageFormat::Png, 1); Assert.AreNotEqual(0, TempBlob.Length(), LengthErr); end;