38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using System;
|
|
using PdfSharp.Pdf;
|
|
using PdfSharp.Pdf.IO;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
namespace LCU.Api.Internal.FileOperations.Validations;
|
|
|
|
public static class PdfValidations
|
|
{
|
|
private const string PdfPageDoesntExist = "PDF Page Doesn't Exist.";
|
|
private const string PdfPageCantBeLessThanOne = "PDF Page Can't Be Less Than 1.";
|
|
|
|
public static RequestResponse ValidatePdfPageExists(RequestResponse requestResponse, string pdfName, int pageNumber)
|
|
{
|
|
try
|
|
{
|
|
PdfDocument inputDocument = PdfReader.Open(pdfName, PdfDocumentOpenMode.Import);
|
|
|
|
if(pageNumber <= 0)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add(PdfPageCantBeLessThanOne);
|
|
}
|
|
|
|
if (pageNumber > inputDocument.PageCount)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add(PdfPageDoesntExist);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
requestResponse.IsSuccessful = false;
|
|
requestResponse.MessageStack.Add(exception.Message);
|
|
}
|
|
|
|
return requestResponse;
|
|
}
|
|
} |