Skip to content

PDFCLOUD-5013: added snippets rotate-crop-resize #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2025
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
14 changes: 14 additions & 0 deletions Uses-Cases/ChangeLayout/ChangeLayout.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Aspose.Pdf.Cloud.Sdk\Aspose.Pdf.Cloud.Sdk.csproj" />
</ItemGroup>

</Project>
184 changes: 184 additions & 0 deletions Uses-Cases/ChangeLayout/ChangeLayoutHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Pdf.Cloud.Sdk.Model;
using Newtonsoft.Json;

namespace ChangeLayout
{
public class ConfigParams
{
public string CrdentialPath { get; } = "..\\credentials.json ";
public string LOCAL_FOLDER { get; } = "C:\\Samples";
public string REMOTE_TEMP_FOLDER { get; } = "TempPdfCloud";
public string PDF_DOCUMENT { get; } = "sample.pdf";
public string PDF_OUTPUT { get; } = "output_sample.pdf";

public Rotation ROTATE_PAGES_ANGLE = Rotation.On90;
public string ROTATE_PAGES { get; } = "1-3";


public string CROP_PAGE_TEMP_FILE { get; } = "sample_temp_file.png";
public string CROP_LOCAL_RESULT_DOCUMENT_NAME { get; } = "output_sample.pdf";
public int CROP_PAGE_NUMBER { get; } = 3;
public int CROP_HEIGHT { get; } = 400;
public int CROP_WIDTH { get; } = 300;
public int CROP_LLX { get; } = 100;
public int CROP_LLY { get; } = 200;

public string RESIZE_PDF_HTML_FILE { get; } = "sample_temp_file.html";
public string RESIZE_RESULT_DOCUMENT_NAME { get; } = "output_sample.pdf";
public int RESIZE_PAGE_NUMBER { get; } = 2;
public int RESIZE_NEW_PAGE_WIDTH { get; } = 1000;
public int RESIZE_NEW_PAGE_HEIGHT { get; } = 500;

public int CROP_PAGE_WIDTH { get; set; } = 0;
public int CROP_PAGE_HEIGHT { get; set; } = 0;
}

public class Credentials
{
public string Id { get; set; }
public string Key { get; set; }
}

public class ChangeLayoutHelper
{
public PdfApi pdfApi { get; private set; }
public ConfigParams config { get; private set; }

public ChangeLayoutHelper()
{
config = new ConfigParams();
string jsCredText = File.ReadAllText(config.CrdentialPath);
Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText);
pdfApi = new PdfApi(cred.Key, cred.Id);
}

public async Task UploadFile(string fileName)
{
using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName)))
{
FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file);
if (response == null)
Console.WriteLine("UploadFile(): Unexpected error - no response!");
else if (response.Errors != null && response.Errors.Count > 0)
foreach (var error in response.Errors)
Console.WriteLine("UploadFile(): {0} -> {1}", [ error.Code, error.Message]);
else
Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName);
}
}

public async Task DownloadFile(string fileName, string outputPrefix)
{
Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName));
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + fileName));
stream.Position = 0;
await stream.CopyToAsync(fileStream);
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + fileName);
}

public async Task GetPageInfo(string document, int pageNumber)
{
DocumentPageResponse response = await pdfApi.GetPageAsync(document, pageNumber, folder: config.REMOTE_TEMP_FOLDER);
if (response == null)
Console.WriteLine("GetPageInfo(): Unexpected error - no response!");
else if (response.Code != 200)
Console.WriteLine("GetPageInfo(): Code {0} -> Status '{1}'", [response.Code, response.Status]);
else
{
Page page = response.Page;
Rectangle rectangle = page.Rectangle;
Console.WriteLine("GetPageInfo(): page {0} => id: '{1}', lLx: '{2}', lLY: '{3}', uRX: '{4}', uRY: '{5}'",
[pageNumber, page.Id, rectangle.LLX, rectangle.LLY, rectangle.URX, rectangle.URY]);
config.CROP_PAGE_HEIGHT = (int)(Math.Round(rectangle.URY ?? 0) - Math.Round(rectangle.LLY ?? 0));
config.CROP_PAGE_WIDTH = (int)(Math.Round(rectangle.URX ?? 0) - Math.Round(rectangle.LLX ?? 0));
}
}

public async Task<string> ExtractPdfPage(string document, int pageNumber, int width, int height)
{
string imageFile = document + ".png";
Stream stream = await pdfApi.GetPageConvertToPngAsync(document, pageNumber, width, height, config.REMOTE_TEMP_FOLDER);
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, imageFile));
stream.Position = 0;
await stream.CopyToAsync(fileStream);

UploadFile(imageFile);
Console.WriteLine("ExtractPdfPage(): Page #{0} extracted as image {1}.", [pageNumber, imageFile]);

return imageFile;
}

public async Task<DocumentResponse?> CreatePdfDocument(string newDocument, int width, int height)
{
DocumentProperties docProps = new DocumentProperties(
List: new List<DocumentProperty>() {
new DocumentProperty(Name: "prop1", Value: "Value1", BuiltIn: false)
}
);

DisplayProperties dispProps = new DisplayProperties()
{
CenterWindow = true,
HideMenuBar = true,
Direction = Direction.L2R,
DisplayDocTitle = true,
HideToolBar = true,
HideWindowUI = true,
NonFullScreenPageMode = PageMode.UseThumbs,
PageLayout = PageLayout.TwoPageLeft,
PageMode = PageMode.UseThumbs
};

DefaultPageConfig pageConfig = new DefaultPageConfig(height, width);

DocumentConfig document_config = new DocumentConfig(
DocumentProperties: docProps,
DisplayProperties: dispProps,
DefaultPageConfig: pageConfig,
PagesCount: 1
);

DocumentResponse response = await pdfApi.PostCreateDocumentAsync(newDocument, document_config, folder: config.REMOTE_TEMP_FOLDER);

if (response != null && response.Code == 200)
{
Console.WriteLine("CreatePdfDocument(): Document '{0}' created.", newDocument);
return response;
}
else
{
Console.WriteLine("CreatePdfDocument(): Unexpected error!!!");
return null;
}
}

public async Task<AsposeResponse?> InsertPageAsImage(string document, string imageFileValue, int llx, int lly)
{
ImageStamp imageStamp = new ImageStamp(
Background: true,
HorizontalAlignment: HorizontalAlignment.None,
VerticalAlignment: VerticalAlignment.None,
Rotate: Rotation.None,
RotateAngle: 0,
Opacity: 1,
XIndent: -llx,
YIndent: -lly,
Zoom: 1,
FileName: Path.Combine(config.REMOTE_TEMP_FOLDER, imageFileValue)
);

AsposeResponse response = await pdfApi.PostPageImageStampsAsync(document, 1, new List<ImageStamp>() { imageStamp}, folder: config.REMOTE_TEMP_FOLDER );
if (response != null && response.Code == 200)
{
Console.WriteLine("InsertPageAsImage(): Document '{0}' with stamp '{1}' created.", [document, imageFileValue]);
return response;
}
else
{
Console.WriteLine("CreatePdfDocument(): Unexpected error!!!");
return null;
}
}
}
}
39 changes: 39 additions & 0 deletions Uses-Cases/ChangeLayout/CropDocumentPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace ChangeLayout
{
public class CropDocumentPage
{
private ChangeLayoutHelper _helper;

public CropDocumentPage(ChangeLayoutHelper helper)
{
_helper = helper;
}

public async Task MakeCropDocumentPage(string document, string outputDocument, int pageNumber, int llx, int lly, int width, int height)
{
await _helper.UploadFile(document);
await _helper.GetPageInfo(document, pageNumber);
string imageFile = await _helper.ExtractPdfPage(document, pageNumber, _helper.config.CROP_PAGE_WIDTH, _helper.config.CROP_PAGE_HEIGHT);
DocumentResponse? newPdf = await _helper.CreatePdfDocument(outputDocument, width, height);
if (newPdf == null)
Console.WriteLine("MakeCropDocumentPage(): Unexpected error - new document is NULL");
else if (newPdf.Code != 200)
Console.WriteLine("MakeCropDocumentPage(): Failed to create new PDF document!");
else
{
AsposeResponse? response = await _helper.InsertPageAsImage(outputDocument, imageFile, llx, lly);
if (response == null)
Console.WriteLine("MakeCropDocumentPage(): Unexpected error - insert image return NULL");
else if (newPdf.Code != 200)
Console.WriteLine("MakeCropDocumentPage(): Failed to insert image to the new PDF document!");
else
{
Console.WriteLine("cropPage(): Page successfully cropped.");
await _helper.DownloadFile(outputDocument, "cropped_");
}
}
}
}
}
13 changes: 13 additions & 0 deletions Uses-Cases/ChangeLayout/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ChangeLayout;


ChangeLayoutHelper helper = new ChangeLayoutHelper();

RotateDocumentsPages rotate = new RotateDocumentsPages(helper);
await rotate.MakeRotateDocumentsPages(helper.config.PDF_DOCUMENT, helper.config.ROTATE_PAGES_ANGLE.ToString(), helper.config.ROTATE_PAGES);

ResizeDocumentAllPages resize = new ResizeDocumentAllPages(helper);
await resize.MakeResizeDocumentAllPages(helper.config.PDF_DOCUMENT, helper.config.RESIZE_PDF_HTML_FILE, helper.config.RESIZE_NEW_PAGE_WIDTH, helper.config.RESIZE_NEW_PAGE_HEIGHT);

CropDocumentPage crop = new CropDocumentPage(helper);
await crop.MakeCropDocumentPage(helper.config.PDF_DOCUMENT, helper.config.CROP_LOCAL_RESULT_DOCUMENT_NAME, helper.config.CROP_PAGE_NUMBER, helper.config.CROP_LLX, helper.config.CROP_LLY, helper.config.CROP_WIDTH, helper.config.CROP_HEIGHT);
54 changes: 54 additions & 0 deletions Uses-Cases/ChangeLayout/ResizeDocumentAllPages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Aspose.Pdf.Cloud.Sdk.Model;


namespace ChangeLayout
{
public class ResizeDocumentAllPages
{
private ChangeLayoutHelper _helper;

public ResizeDocumentAllPages(ChangeLayoutHelper helper)
{
_helper = helper;
}

public async Task MakeResizeDocumentAllPages(string document, string htmlTempDoc, int pageWidth, int pageHeight)
{
await _helper.UploadFile(document);
string htmlTempPath = Path.Combine(_helper.config.REMOTE_TEMP_FOLDER, htmlTempDoc);
AsposeResponse response = await _helper.pdfApi.PutPdfInStorageToHtmlAsync(
document, htmlTempPath,
documentType: HtmlDocumentType.Xhtml.ToString(),
outputFormat: OutputFormat.Folder.ToString(),
folder: _helper.config.REMOTE_TEMP_FOLDER
);
if (response == null)
Console.WriteLine("MakeResizeDocumentAllPages(): Unexpected error - no response in Pdf to Html convert!");
else if (response.Code != 200)
Console.WriteLine("MakeResizeDocumentAllPages(): Error -> Code {0} -> Status '{1}'", [response.Code, response.Status]);
else
{
Console.WriteLine("MakeResizeDocumentAllPages(): temporary file '{0}' successfully created.", htmlTempDoc);
string outputDocument = "resized_" + document;
await _helper.pdfApi.PutHtmlInStorageToPdfAsync(
outputDocument, htmlTempPath,
dstFolder: _helper.config.REMOTE_TEMP_FOLDER,
htmlFileName: htmlTempDoc,
height: pageHeight,
width: pageWidth
);

if (response == null)
Console.WriteLine("MakeResizeDocumentAllPages(): Unexpected error - no response in html to Pdf convert!");
else if (response.Code != 200)
Console.WriteLine("MakeResizeDocumentAllPages(): Error -> Code {0} -> Status '{1}'", [response.Code, response.Status]);
else
{
Console.WriteLine("resizePages(): Pages successfully resized.");
await _helper.DownloadFile(outputDocument, "resized_doc_");
}
}

}
}
}
28 changes: 28 additions & 0 deletions Uses-Cases/ChangeLayout/RotateDocumentsPages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Pdf.Cloud.Sdk.Model;

namespace ChangeLayout
{
public class RotateDocumentsPages
{
private ChangeLayoutHelper _helper;

public RotateDocumentsPages(ChangeLayoutHelper helper)
{
_helper = helper;
}

public async Task MakeRotateDocumentsPages(string document, string rotateAngle, string pages)
{
await _helper.UploadFile(document);
AsposeResponse response = await _helper.pdfApi.PostDocumentPagesRotateAsync(document, rotateAngle, pages, folder: _helper.config.REMOTE_TEMP_FOLDER);
if (response.Code != 200)
Console.WriteLine("MakeRotateDocumentsPages(): Unexpected error!");
else {
Console.WriteLine("MakeRotateDocumentsPages(): Pages '{0}' successfully rotated!");

await _helper.DownloadFile(document, "rotated_");
}
}
}
}