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
99 changes: 98 additions & 1 deletion src/System Application/App/Pdf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ You can use this module to:

- Retrieve PDF metadata (author, title, page size, etc.)

- Number of pages
- Count number of pages

- Add attachments and append files to the rendered PDF

- Protect PDF documents with user/admin codes


### Extract an embedded invoice from a PDF
```
Expand Down Expand Up @@ -77,4 +82,96 @@ begin
    PageCount := PDFDocument.GetPdfPageCount(PdfStream);
    Message('PDF has %1 pages', PageCount);
end;
```

### Add an attachment to the PDF
```
procedure Example()
var
PDFDocument: Codeunit "PDF Document";
begin
PDFDocument.AddAttachment(
'factur-x.xml',
Enum::"PDF Attach. Data Relationship"::Data,
'application/xml',
'factur-x.xml',
'Embedded e-invoice',
false);
end;
```

### Append a file to the rendered PDF
```
procedure Example()
var
PDFDocument: Codeunit "PDF Document";
begin
PDFDocument.AddFileToAppend('c:\temp\appendix.pdf');
end;

```

### Append a stream to the rendered PDF
```
procedure Example(FileInStream: InStream)
var
PDFDocument: Codeunit "PDF Document";
begin
PDFDocument.AddStreamToAppend(FileInStream);
end;
```

### Protect the PDF with user and admin codes
```
procedure Example()
var
PDFDocument: Codeunit "PDF Document";
begin
PDFDocument.ProtectDocument('user123', 'admin456');
end;
```

### Convert PDF to image
```
procedure Example(ImageStream: InStream)
var
PDFDocument: Codeunit "PDF Document";
begin
PDFDocument.ConvertToImage(ImageStream, Enum::"Image Format"::PNG, 1);
end;
```

### Generate JSON rendering payload
```
procedure Example(RenderingPayload: JsonObject)
var
PDFDocument: Codeunit "PDF Document";
FinalPayload: JsonObject;
begin
FinalPayload := PDFDocument.ToJson(RenderingPayload);
end;
```

### Count configured attachments
```
procedure Example()
var
    PDFDocument: Codeunit "PDF Document";
    Count: Integer;
begin
    Count := PDFDocument.AttachmentCount();
    Message('There are %1 attachments.', Count);
end;
```

### Count appended documents
```
procedure Example()
var
PDFDocument: Codeunit "PDF Document";
Count: Integer;
begin
Count := PDFDocument.AppendedDocumentCount();
Message('There are %1 appended documents.', Count);
end;
```
103 changes: 103 additions & 0 deletions src/System Application/App/Pdf/src/PDFDocument.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ codeunit 3110 "PDF Document"
var
PDFDocumentImpl: Codeunit "PDF Document Impl.";


/// <summary>
/// This procedure initializes the internal state of the object by resetting attachment lists
/// and clearing user and admin codes, as well as any additional document names.
/// </summary>
procedure Initialize()
begin
PDFDocumentImpl.Initialize();
end;
/// <summary>
/// This procedure is used to load a PDF document from a stream.
/// </summary>
Expand Down Expand Up @@ -115,4 +124,98 @@ codeunit 3110 "PDF Document"
begin
exit(PDFDocumentImpl.GetAttachmentNames(PdfStream));
end;

/// <summary>
/// Configure the attachment lists. An empty name will reset the list.
/// This procedure adds a new attachment to the PDF document with the specified metadata and relationship type.
/// </summary>
/// <param name="AttachmentName">Attachment name. If empty, the list will be reset.</param>
/// <param name="PDFAttachmentDataType">Defines the relationship of the attachment to the PDF (e.g. supplementary, source, data, alternative).</param>
/// <param name="MimeType">MIME type of the attachment (e.g., application/pdf, image/png).</param>
/// <param name="FileName">The file name of the attachment as it should appear in the PDF.</param>
/// <param name="Description">A textual description of the attachment.</param>
/// <param name="PrimaryDocument">Indicates whether this attachment is the primary document.</param>

procedure AddAttachment(AttachmentName: Text; PDFAttachmentDataType: Enum "PDF Attach. Data Relationship"; MimeType: Text; FileName: Text; Description: Text; PrimaryDocument: Boolean)
begin
PDFDocumentImpl.AddAttachment(AttachmentName, PDFAttachmentDataType, MimeType, FileName, Description, PrimaryDocument);
end;

/// <summary>
/// Add a file to the list of files to append to the rendered document. Empty name will reset the list.
/// </summary>
/// <param name="FileName">Path to the file to append. Platform will remove the file when rendering has been completed.</param>
[Scope('OnPrem')]
procedure AddFileToAppend(FileName: Text)
begin
PDFDocumentImpl.AddFileToAppend(FileName);
end;

/// <summary>
/// Add a stream to the list of files to append to the rendered document using a temporary file name.
/// </summary>
/// <param name="FileInStream">Stream with file content. Platform will remove the temporary file when rendering has been completed.</param>
procedure AddStreamToAppend(FileInStream: InStream)
begin
PDFDocumentImpl.AddStreamToAppend(FileInStream);
end;

/// <summary>
/// Protect the document with a user and admin code using text data type.
/// </summary>
/// <param name="User">User code.</param>
/// <param name="Admin">Admin code.</param>
[NonDebuggable]
procedure ProtectDocument(User: Text; Admin: Text)
begin
PDFDocumentImpl.ProtectDocument(User, Admin);
end;

/// <summary>
/// Protect the document with a user and admin code using secrettext data type.
/// </summary>
/// <param name="User">User code.</param>
/// <param name="Admin">Admin code.</param>
procedure ProtectDocument(User: SecretText; Admin: SecretText)
begin
PDFDocumentImpl.ProtectDocument(User, Admin);
end;

/// <summary>
/// Returns the number of configured attachments.
/// Validates that all attachment-related lists (names, MIME types, data types, filenames, and descriptions) are synchronized in length.
/// Throws an error if any of the lists are out of sync.
/// </summary>
/// <returns>The total number of attachments configured.</returns>

procedure AttachmentCount(): Integer
begin
exit(PDFDocumentImpl.AttachmentCount());
end;

/// <summary>
/// Returns the number of additional document names that have been appended.
/// This count reflects how many supplementary documents are currently tracked.
/// </summary>
/// <returns>The total number of additional document names.</returns>
procedure AppendedDocumentCount(): Integer
begin
exit(PDFDocumentImpl.AppendedDocumentCount());
end;

/// <summary>
/// Converts the internal state of the PDF document configuration into a structured JSON payload.
/// This includes metadata such as version, primary document, attachments, additional documents, and protection settings.
/// </summary>
/// <param name="RenderingPayload">The base JSON object to which the PDF configuration will be applied.</param>
/// <returns>A JsonObject representing the complete rendering payload with all configured properties.</returns>
/// <remarks>
/// Throws an error if the payload already contains a primary document or protection block, as these cannot be overwritten.
/// </remarks>

[NonDebuggable]
procedure ToJson(RenderingPayload: JsonObject): JsonObject
begin
exit(PDFDocumentImpl.ToJson(RenderingPayload));
end;
}
Loading
Loading