Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/System Application/App/DotNet Aliases/src/dotnet.al
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,21 @@ dotnet
type("Microsoft.BusinessCentral.DocumentProcessor.MailMerge"; "MailMerge")
{
}
type("Microsoft.BusinessCentral.DocumentProcessor.PdfDocumentInfo"; "PdfDocumentInfo")
{
}
type("Microsoft.BusinessCentral.DocumentProcessor.PdfConverter"; "PdfConverter")
{
}
type("Microsoft.BusinessCentral.DocumentProcessor.PdfTargetDevice"; "PdfTargetDevice")
{
}
type("Microsoft.BusinessCentral.DocumentProcessor.PdfAttachmentManager"; "PdfAttachmentManager")
{
}
type("Microsoft.BusinessCentral.DocumentProcessor.PdfAttachment"; "PdfAttachment")
{
}
}

assembly("Microsoft.Dynamics.Nav.PowerBIEmbedded")
Expand Down Expand Up @@ -2099,7 +2114,6 @@ dotnet
type("System.Collections.Generic.List`1"; "GenericList1")
{
}

type("System.Nullable`1"; "Nullable1")
{
}
Expand Down
37 changes: 37 additions & 0 deletions src/System Application/App/Pdf/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "2c7afcfb-9625-4ace-b972-5889d045ce8f",
"name": "Pdf",
"publisher": "Microsoft",
"brief": "Provides functionality for processing and extracting data from PDF documents.",
"description": "Provides functionality for processing and extracting data from PDF documents.",
"version": "26.3.0.0",
"privacyStatement": "https://go.microsoft.com/fwlink/?linkid=724009",
"EULA": "https://go.microsoft.com/fwlink/?linkid=2009120",
"help": "https://go.microsoft.com/fwlink/?linkid=2103698",
"url": "https://go.microsoft.com/fwlink/?linkid=724011",
"logo": "",
"dependencies": [
{
"id": "7e3b999e-1182-45d2-8b82-d5127ddba9b2",
"name": "DotNet Aliases",
"publisher": "Microsoft",
"version": "26.3.0.0"
},
{
"id": "b185fd4a-677b-48d3-a324-768de7563df0",
"name": "Image",
"publisher": "Microsoft",
"version": "26.3.0.0"
}
],
"screenshots": [],
"platform": "26.0.0.0",
"idRanges": [
{
"from": 3109,
"to": 3110
}
],
"target": "OnPrem",
"contextSensitiveHelpUrl": "https://learn.microsoft.com/dynamics365/business-central/"
}
42 changes: 42 additions & 0 deletions src/System Application/App/Pdf/src/PDFDocument.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.IO;

using System.Utilities;

/// <summary>
/// Codeunit that provides helper functions for PDF processing.
/// </summary>
codeunit 3110 "PDF Document"
{
Access = Public;
InherentEntitlements = X;
InherentPermissions = X;

var
PDFDocumentImpl: Codeunit "PDF Document Impl.";

/// <summary>
/// This procedure is used to load a PDF document from a stream.
/// </summary>
/// <param name="DocumentStream">Stream of the PDF document.</param>
/// <returns>Returns true if the document is loaded successfully, otherwise false.</returns>
procedure Load(DocumentStream: InStream): Boolean
begin
exit(PDFDocumentImpl.Load(DocumentStream));
end;

/// <summary>
/// This procedure is used to convert a PDF file to an image.
/// </summary>
/// <param name="ImageStream">Stream of the image file.</param>
/// <param name="ImageFormat">Image format to convert the PDF to.</param>
/// <param name="PageNumber">Page number to convert.</param>
procedure ConvertToImage(var ImageStream: InStream; ImageFormat: Enum "Image Format"; PageNumber: Integer)
begin
PDFDocumentImpl.ConvertToImage(ImageStream, ImageFormat, PageNumber);
end;
}
82 changes: 82 additions & 0 deletions src/System Application/App/Pdf/src/PDFDocumentImpl.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.IO;

using System.Utilities;
using System;

/// <summary>
/// Codeunit that provides helper functions for PDF processing.
/// </summary>
codeunit 3109 "PDF Document Impl."
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

var
DocumentStream: InStream;
Loaded: Boolean;
UnsupportedImageFormatErr: Label 'Unsupported image format: %1', Comment = '%1 is the image format that is not supported.';
NotLoadedErr: Label 'PDF document is not loaded. Please load the document before performing this operation.';

procedure Load(DocStream: InStream): Boolean
begin
// Empty stream, no actions possible on the stream so return immediately
if DocStream.Length() < 1 then
exit(false);

DocumentStream := DocStream;
Loaded := true;
exit(true);
end;

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;
SharedDocumentStream: InStream;
begin
// Check if the document is loaded
if not Loaded then
Error(NotLoadedErr);

// Use a shared stream and reset the read pointer to beginning of stream.
SharedDocumentStream := DocumentStream;
if SharedDocumentStream.Position > 1 then
SharedDocumentStream.ResetPosition();

MemoryStream := MemoryStream.MemoryStream();
CopyStream(MemoryStream, SharedDocumentStream);

ConvertImageFormatToPdfTargetDevice(ImageFormat, PdfTargetDevice);
ImageMemoryStream := PdfConverterInstance.ConvertPage(PdfTargetDevice, MemoryStream, PageNumber, 0, 0, 0); // apply default height, width and resolution
// 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);
ImageStream.Position(1);
exit(true)
end;

local procedure ConvertImageFormatToPdfTargetDevice(ImageFormat: Enum "Image Format"; var PdfTargetDevice: DotNet PdfTargetDevice)
begin
case ImageFormat of
ImageFormat::Png:
PdfTargetDevice := PdfTargetDevice.PngDevice;
ImageFormat::Jpeg:
PdfTargetDevice := PdfTargetDevice.JpegDevice;
ImageFormat::Tiff:
PdfTargetDevice := PdfTargetDevice.TiffDevice;
ImageFormat::Bmp:
PdfTargetDevice := PdfTargetDevice.BmpDevice;
ImageFormat::Gif:
PdfTargetDevice := PdfTargetDevice.GifDevice;
else
Error(UnsupportedImageFormatErr, ImageFormat);
end;
end;
}
Binary file not shown.
Binary file added src/System Application/Test/.resources/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/System Application/Test/Document Sharing/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
}
],
"target": "OnPrem"
}
}
49 changes: 49 additions & 0 deletions src/System Application/Test/Pdf/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"id": "2c7afcfb-9625-4ace-b972-5889d045ce8c",
"name": "Pdf Tests",
"publisher": "Microsoft",
"brief": "Tests for the Pdf module.",
"description": "Provides functionality for processing and extracting data from PDF documents.",
"version": "26.3.0.0",
"privacyStatement": "https://go.microsoft.com/fwlink/?linkid=724009",
"EULA": "https://go.microsoft.com/fwlink/?linkid=2009120",
"help": "https://go.microsoft.com/fwlink/?linkid=2103698",
"url": "https://go.microsoft.com/fwlink/?linkid=724011",
"logo": "",
"dependencies": [
{
"id": "2c7afcfb-9625-4ace-b972-5889d045ce8f",
"name": "Pdf",
"publisher": "Microsoft",
"version": "26.3.0.0"
},
{
"id": "b185fd4a-677b-48d3-a324-768de7563df0",
"name": "Image",
"publisher": "Microsoft",
"version": "26.3.0.0"
},
{
"id": "e31ad830-3d46-472e-afeb-1d3d35247943",
"name": "BLOB Storage",
"publisher": "Microsoft",
"version": "26.3.0.0"
},
{
"id": "dd0be2ea-f733-4d65-bb34-a28f4624fb14",
"name": "Library Assert",
"publisher": "Microsoft",
"version": "26.3.0.0"
}
],
"screenshots": [],
"platform": "26.0.0.0",
"idRanges": [
{
"from": 132601,
"to": 132602
}
],
"target": "OnPrem",
"contextSensitiveHelpUrl": "https://learn.microsoft.com/dynamics365/business-central/"
}
38 changes: 38 additions & 0 deletions src/System Application/Test/Pdf/src/PDFDocumentTest.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.IO;

using System.Utilities;
using System.TestLibraries.Utilities;


codeunit 132601 "PDF Document Test"
{
Access = Internal;
Subtype = Test;
TestPermissions = Disabled;

var
Assert: Codeunit "Library Assert";
LengthErr: Label 'Outstream length should have length 0';

[Test]
procedure ValidPdfToPngImage()
var
PdfDocument: Codeunit "PDF Document";
TempBlob: Codeunit "Temp Blob";
ImageFormat: Enum "Image Format";
PdfInstream, ImageStream, ResultImageStream : InStream;
begin
// Setup
NavApp.GetResource('test.pdf', PdfInstream, TextEncoding::UTF8);
NavApp.GetResource('test.png', ResultImageStream, TextEncoding::UTF8);
TempBlob.CreateInStream(ImageStream);
PdfDocument.Load(PdfInstream);
PdfDocument.ConvertToImage(ImageStream, ImageFormat::Png, 1);
Assert.AreNotEqual(0, TempBlob.Length(), LengthErr);
end;

}
3 changes: 3 additions & 0 deletions src/System Application/Test/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,8 @@
"from": 130000,
"to": 139999
}
],
"resourceFolders": [
".resources"
]
}
Loading