Skip to content

Commit 2eba0e9

Browse files
Add 116 example(s) for Facades - Pages
1 parent 2306918 commit 2eba0e9

118 files changed

Lines changed: 7611 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf";
10+
const string outputPath = "output.pdf";
11+
12+
if (!File.Exists(inputPath))
13+
{
14+
Console.Error.WriteLine($"Input file not found: {inputPath}");
15+
return;
16+
}
17+
18+
PdfFileEditor fileEditor = new PdfFileEditor();
19+
// null pages array processes all pages; add 5% margin on each side
20+
bool success = fileEditor.AddMarginsPct(inputPath, outputPath, null, 5, 5, 5, 5);
21+
if (success)
22+
{
23+
Console.WriteLine($"Margins added successfully. Saved to '{outputPath}'.");
24+
}
25+
else
26+
{
27+
Console.Error.WriteLine("Failed to add margins.");
28+
}
29+
}
30+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Text;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string outputPath = "output.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine("Input file not found: " + inputPath);
17+
return;
18+
}
19+
20+
using (Document sourceDoc = new Document(inputPath))
21+
{
22+
using (Document resultDoc = new Document())
23+
{
24+
// Remove the default blank page created by the constructor
25+
if (resultDoc.Pages.Count > 0)
26+
{
27+
resultDoc.Pages.Delete(1);
28+
}
29+
30+
for (int i = 1; i <= sourceDoc.Pages.Count; i++)
31+
{
32+
Page sourcePage = sourceDoc.Pages[i];
33+
34+
// Extract text from the current page
35+
TextAbsorber absorber = new TextAbsorber();
36+
sourcePage.Accept(absorber);
37+
string pageText = absorber.Text ?? string.Empty;
38+
int wordCount = 0;
39+
if (pageText.Length > 0)
40+
{
41+
string[] words = pageText.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
42+
wordCount = words.Length;
43+
}
44+
45+
// Determine zoom factor based on word count
46+
float zoomFactor = 1.0f;
47+
if (wordCount < 100)
48+
{
49+
zoomFactor = 1.5f;
50+
}
51+
else if (wordCount < 300)
52+
{
53+
zoomFactor = 1.2f;
54+
}
55+
else
56+
{
57+
zoomFactor = 1.0f;
58+
}
59+
60+
// Create a temporary single‑page PDF
61+
string tempSinglePath = Path.GetTempFileName();
62+
using (Document singleDoc = new Document())
63+
{
64+
singleDoc.Pages.Add(sourcePage);
65+
singleDoc.Save(tempSinglePath);
66+
}
67+
68+
// Apply zoom using PdfPageEditor
69+
string tempZoomedPath = Path.GetTempFileName();
70+
PdfPageEditor editor = new PdfPageEditor();
71+
editor.BindPdf(tempSinglePath);
72+
editor.Zoom = zoomFactor;
73+
editor.Save(tempZoomedPath);
74+
editor.Close();
75+
76+
// Load the zoomed page and add it to the result document
77+
using (Document zoomedDoc = new Document(tempZoomedPath))
78+
{
79+
resultDoc.Pages.Add(zoomedDoc.Pages[1]);
80+
}
81+
82+
// Clean up temporary files
83+
File.Delete(tempSinglePath);
84+
File.Delete(tempZoomedPath);
85+
}
86+
87+
resultDoc.Save(outputPath);
88+
}
89+
}
90+
91+
Console.WriteLine("Zoom‑adjusted PDF saved to '" + outputPath + "'.");
92+
}
93+
}

facades-pages/agents.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
name: Facades - Pages
3+
description: C# examples for Facades - Pages using Aspose.PDF for .NET
4+
language: csharp
5+
framework: net10.0
6+
parent: ../agents.md
7+
---
8+
9+
# AGENTS - Facades - Pages
10+
11+
## Persona
12+
13+
You are a C# developer specializing in PDF processing using Aspose.PDF for .NET,
14+
working within the **Facades - Pages** category.
15+
This folder contains standalone C# examples for Facades - Pages operations.
16+
See the root [agents.md](../agents.md) for repository-wide conventions and boundaries.
17+
18+
## Scope
19+
- This folder contains examples for **Facades - Pages**.
20+
- Files are standalone `.cs` examples stored directly in this folder.
21+
22+
## Required Namespaces
23+
24+
- `using Aspose.Pdf;` (103/116 files) ← category-specific
25+
- `using Aspose.Pdf.Facades;` (76/116 files) ← category-specific
26+
- `using Aspose.Pdf.Text;` (12/116 files)
27+
- `using Aspose.Pdf.Annotations;` (3/116 files)
28+
- `using Aspose.Pdf.Drawing;` (1/116 files)
29+
- `using Aspose.Pdf.Printing;` (1/116 files)
30+
- `using System;` (116/116 files)
31+
- `using System.IO;` (105/116 files)
32+
- `using System.Runtime.InteropServices;` (4/116 files)
33+
- `using System.Collections.Generic;` (3/116 files)
34+
- `using System.Linq;` (3/116 files)
35+
- `using System.Text.Json;` (1/116 files)
36+
37+
## Common Code Pattern
38+
39+
Most files in this category use `PdfPageEditor` from `Aspose.Pdf.Facades`:
40+
41+
```csharp
42+
PdfPageEditor tool = new PdfPageEditor();
43+
tool.BindPdf("input.pdf");
44+
// ... PdfPageEditor operations ...
45+
tool.Save("output.pdf");
46+
```
47+
48+
## Files in this folder
49+
50+
| File | Title | Key APIs | Description |
51+
|------|-------|----------|-------------|
52+
| [add-5-percent-margins](./add-5-percent-margins.cs) | Add 5% Margins to All PDF Pages | `PdfFileEditor`, `AddMarginsPct` | Demonstrates how to add a uniform 5% margin to every page of a PDF using Aspose.Pdf's PdfFileEditor. |
53+
| [adjust-zoom-by-word-count](./adjust-zoom-by-word-count.cs) | Adjust Page Zoom Based on Word Count | `Document`, `Page`, `TextAbsorber` | Demonstrates how to increase the zoom level on PDF pages that contain fewer words, improving read... |
54+
| [align-page-two-left](./align-page-two-left.cs) | Align Page Two Content Left in PDF | `PdfPageEditor`, `BindPdf`, `HorizontalAlignment` | Demonstrates how to left‑justify the content of the second page of a PDF using PdfPageEditor. |
55+
| [align-page-vertical-middle](./align-page-vertical-middle.cs) | Align Page Content Vertically to Middle on Page 3 | `PdfPageEditor`, `VerticalAlignment`, `HorizontalAlignment` | Demonstrates how to vertically center the content of the third page of a PDF using PdfPageEditor. |
56+
| [align-page-vertical-top](./align-page-vertical-top.cs) | Align Page Content Vertically to Top | `PdfPageEditor`, `VerticalAlignmentType`, `VerticalAlignment` | Demonstrates aligning the content of the third page of a PDF to the top using PdfPageEditor.Verti... |
57+
| [apply-different-zoom-levels](./apply-different-zoom-levels.cs) | Apply Different Zoom Levels to PDF Pages | `PdfPageEditor`, `BindPdf`, `GetPages` | Demonstrates how to set a distinct zoom factor for each page of a PDF using PdfPageEditor. |
58+
| [apply-dissolve-transition](./apply-dissolve-transition.cs) | Apply Dissolve Transition to PDF Page | `Document`, `PdfPageEditor`, `ProcessPages` | Demonstrates how to set a dissolve page transition with a three‑second duration on page five of a... |
59+
| [apply-double-zoom-stamp](./apply-double-zoom-stamp.cs) | Apply Double-Precision Zoom to a Stamp in PDF | `Document`, `AddStamp`, `BindImage` | Demonstrates how to set a double‑precision zoom factor on a stamp and apply it to a PDF page. |
60+
| [apply-image-stamp-zoom](./apply-image-stamp-zoom.cs) | Apply Image Stamp to Selected Non-Consecutive Pages with Zoo... | `PdfFileStamp`, `ImageStamp`, `Zoom` | Demonstrates how to stamp an image on specific non-consecutive PDF pages and set a common zoom fa... |
61+
| [apply-left-horizontal-alignment](./apply-left-horizontal-alignment.cs) | Apply Left Horizontal Alignment to All PDF Pages | `PdfPageEditor`, `BindPdf`, `HorizontalAlignment` | Demonstrates how to set a uniform left‑justified horizontal alignment for all pages of a PDF usin... |
62+
| [apply-page-settings-from-json](./apply-page-settings-from-json.cs) | Apply Page Settings from JSON Configuration to PDF | `Document`, `Page`, `PageInfo` | Loads a PDF, reads page size and background color settings from a JSON file, and applies them to ... |
63+
| [apply-page-transitions](./apply-page-transitions.cs) | Apply Custom Page Transitions Based on Index | `PdfPageEditor`, `Document`, `TransitionType` | Demonstrates how to set different transition effects for each PDF page using PdfPageEditor. |
64+
| [apply-page-transitions__v2](./apply-page-transitions__v2.cs) | Apply Page Transition Effects Based on Page Index | `PdfPageEditor`, `Document`, `TransitionType` | Demonstrates how to set different transition effects for each PDF page using PdfPageEditor, cycli... |
65+
| [apply-sequential-page-transitions](./apply-sequential-page-transitions.cs) | Apply Sequential Page Transitions in PDF | `PdfPageEditor`, `Document`, `BindPdf` | Demonstrates how to set different transition effects for each page of a PDF using PdfPageEditor. |
66+
| [apply-transition-odd-pages](./apply-transition-odd-pages.cs) | Apply Transition Effect to Odd Pages in PDF | `PdfPageEditor`, `Document`, `ProcessPages` | Demonstrates how to set a page transition effect only on odd‑numbered pages of a PDF using Aspose... |
67+
| [apply-vertical-alignment](./apply-vertical-alignment.cs) | Apply Top Vertical Alignment to Selected PDF Pages | `PdfPageEditor`, `BindPdf`, `PageNumbers` | Demonstrates how to set the vertical alignment of specific pages in a PDF to the top using PdfPag... |
68+
| [apply-zoom-even-pages](./apply-zoom-even-pages.cs) | Apply Zoom to Even-Numbered PDF Pages | `Document`, `PdfPageEditor`, `File` | Demonstrates how to set a 1.2 zoom level on all even-numbered pages of a PDF using Aspose.Pdf. |
69+
| [apply-zoom-to-image-pages](./apply-zoom-to-image-pages.cs) | Apply Zoom to Pages Containing Images | `Document`, `ImagePlacementAbsorber`, `PdfPageEditor` | Detects pages that contain images and applies a zoom factor only to those pages, leaving other pa... |
70+
| [assign-page-transitions](./assign-page-transitions.cs) | Assign Page Transition Effects Based on Content Type | `Document`, `PdfPageEditor`, `BindPdf` | Demonstrates how to set different transition effects for individual PDF pages using PdfPageEditor. |
71+
| [batch-convert-pdfs-a4](./batch-convert-pdfs-a4.cs) | Batch Convert PDFs to A4 Page Size | `Document`, `SetPageSize`, `A4` | Converts multiple PDF files to A4 page size for uniform printing across all documents. |
72+
| [batch-resize-pdf-contents](./batch-resize-pdf-contents.cs) | Batch Resize PDF Contents with 10% Margins | `PdfFileEditor`, `ContentsResizeParameters`, `ContentsResizeValue` | Demonstrates how to shrink the contents of all pages in a PDF by adding a 10 % margin using Aspos... |
73+
| [batch-set-page-sizes](./batch-set-page-sizes.cs) | Batch Process PDFs and Set Individual Page Sizes | `Document`, `Page`, `PageInfo` | Processes all PDF files in a folder, sets each page size based on the file name (e.g., A4 or Lett... |
74+
| [booklet-margin-resize](./booklet-margin-resize.cs) | Create Booklet with 15% Margin Resize | `Document`, `PageInfo`, `Margin` | Demonstrates how to increase page margins by 15% and then generate a booklet layout using Aspose.... |
75+
| [center-align-page-duration](./center-align-page-duration.cs) | Center Align Page and Set Display Duration | `PdfPageEditor`, `Document`, `BindPdf` | Demonstrates how to center‑align a specific PDF page and set its display duration to four seconds... |
76+
| [center-align-second-page](./center-align-second-page.cs) | Center Align Text on Second Page | `Document`, `Page`, `TextFragment` | Demonstrates how to center-align text on the second page of a PDF using Aspose.Pdf. |
77+
| [chain-page-modifications](./chain-page-modifications.cs) | Chain Page Rotation, Size, and Zoom Modifications | `PdfPageEditor`, `Document`, `Rotation` | Demonstrates how to rotate pages, change page size, and apply zoom using PdfPageEditor before sav... |
78+
| [change-page-size-a3](./change-page-size-a3.cs) | Change PDF Page Size to A3 | `Document`, `SetPageSize`, `A3` | Demonstrates how to resize all pages of a PDF to A3 size, providing a larger canvas for high‑reso... |
79+
| [change-page-size-revert](./change-page-size-revert.cs) | Change PDF Page Size and Revert to Original | `Document`, `SetPageSize`, `Save` | Demonstrates changing a PDF page to custom dimensions and then restoring the original size to ver... |
80+
| [combine-rotation-zoom-transition](./combine-rotation-zoom-transition.cs) | Combine Page Rotation and Zoom with Transition Effects | `Document`, `PdfPageEditor`, `BindPdf` | Demonstrates using PdfPageEditor to rotate pages, apply zoom, and set a transition effect for pre... |
81+
| [convert-portrait-to-landscape](./convert-portrait-to-landscape.cs) | Convert Portrait PDF Page to Landscape Orientation | `Document`, `SetPageSize`, `PageInfo` | Demonstrates how to change a PDF page from portrait to landscape by swapping its dimensions and v... |
82+
| ... | | | *and 86 more files* |
83+
84+
## Category Statistics
85+
- Total examples: 116
86+
87+
## Category-Specific Tips
88+
89+
### Key API Surface
90+
- `Aspose.Pdf.Facades.PdfFileEditor`
91+
- `Aspose.Pdf.Facades.PdfFileEditor.Delete`
92+
- `Aspose.Pdf.Facades.PdfFileEditor.Extract`
93+
- `Aspose.Pdf.Facades.PdfFileEditor.SplitToEnd`
94+
95+
### Rules
96+
- Instantiate Aspose.Pdf.Facades.PdfFileEditor and call Delete({input_pdf}, {int[]} pagesToDelete, {output_pdf}) to remove the specified pages.
97+
- The page numbers in the array are 1‑based indices representing the pages to be removed.
98+
- Use PdfFileEditor.Delete({input_pdf_stream}, {int[] pagesToDelete}, {output_pdf_stream}) to remove the specified pages (1‑based indices) from a PDF without loading it into a Document object.
99+
- When working with streams, open the source PDF with FileMode.Open and create the destination PDF with FileMode.Create, then pass the streams to PdfFileEditor.Delete.
100+
- Use PdfFileEditor.Extract({input_pdf}, new int[] {{int}, {int}, ...}, {output_pdf}) to create a new PDF containing only the listed pages.
101+
102+
### Warnings
103+
- The example does not explicitly dispose the FileStream objects; callers should ensure streams are closed or wrapped in using statements.
104+
- The output file will be created or overwritten; ensure the path is correct.
105+
- The example assumes the input PDF exists at the specified location.
106+
- Page numbers must be within the bounds of the source document; otherwise an exception will be thrown.
107+
- Insert overwrites the output file if it already exists.
108+
109+
## General Tips
110+
- See parent [agents.md](../agents.md) for:
111+
- **Boundaries** — Always / Ask First / Never rules for all examples
112+
- **Common Mistakes** — verified anti-patterns that cause build failures
113+
- **Domain Knowledge** — cross-cutting API-specific gotchas
114+
- **Testing Guide** — build and run verification steps
115+
- Review code examples in this folder for Facades - Pages patterns
116+
117+
<!-- AUTOGENERATED:START -->
118+
Updated: 2026-03-31 | Run: `20260331_215520_d8165d`
119+
<!-- AUTOGENERATED:END -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
// Load the PDF into the page editor
20+
PdfPageEditor pageEditor = new PdfPageEditor();
21+
pageEditor.BindPdf(inputPath);
22+
23+
// Specify the page(s) to edit – page numbers are 1‑based
24+
pageEditor.ProcessPages = new int[] { 2 };
25+
26+
// Set horizontal alignment to left (justification)
27+
pageEditor.HorizontalAlignment = HorizontalAlignment.Left;
28+
29+
// Save the modified PDF
30+
pageEditor.Save(outputPath);
31+
pageEditor.Close();
32+
33+
Console.WriteLine($"Page 2 content left‑aligned and saved to '{outputPath}'.");
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
try
20+
{
21+
// Bind the PDF and specify the page(s) to edit.
22+
using (PdfPageEditor editor = new PdfPageEditor())
23+
{
24+
editor.BindPdf(inputPath);
25+
26+
// Align the content of page 3 to the centre both vertically and horizontally.
27+
editor.VerticalAlignmentType = VerticalAlignment.Center;
28+
editor.HorizontalAlignment = HorizontalAlignment.Center;
29+
editor.ProcessPages = new int[] { 3 }; // 1‑based page numbers
30+
31+
editor.Save(outputPath);
32+
editor.Close();
33+
}
34+
35+
Console.WriteLine($"Page 3 content aligned to middle vertically and saved as '{outputPath}'.");
36+
}
37+
catch (Exception ex)
38+
{
39+
Console.Error.WriteLine($"Error: {ex.Message}");
40+
}
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
using (PdfPageEditor pageEditor = new PdfPageEditor())
20+
{
21+
pageEditor.BindPdf(inputPath);
22+
// Specify the page(s) to edit using ProcessPages (1‑based indexing)
23+
pageEditor.ProcessPages = new int[] { 3 };
24+
// Align the content of the selected page(s) to the top
25+
pageEditor.VerticalAlignmentType = Aspose.Pdf.VerticalAlignment.Top;
26+
pageEditor.Save(outputPath);
27+
}
28+
29+
Console.WriteLine($"Page 3 content aligned to top and saved as '{outputPath}'.");
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf";
10+
const string outputPath = "output.pdf";
11+
12+
if (!File.Exists(inputPath))
13+
{
14+
Console.Error.WriteLine($"File not found: {inputPath}");
15+
return;
16+
}
17+
18+
using (PdfPageEditor editor = new PdfPageEditor())
19+
{
20+
editor.BindPdf(inputPath);
21+
22+
int pageCount = editor.GetPages();
23+
24+
for (int i = 1; i <= pageCount; i++)
25+
{
26+
// Example: start at 0.5x zoom and increase by 0.1x for each subsequent page
27+
float zoomFactor = 0.5f + (float)(i - 1) * 0.1f;
28+
editor.ProcessPages = new int[] { i };
29+
editor.Zoom = zoomFactor;
30+
editor.ApplyChanges();
31+
}
32+
33+
editor.Save(outputPath);
34+
}
35+
36+
Console.WriteLine($"Zoom factors applied and saved to '{outputPath}'.");
37+
}
38+
}

0 commit comments

Comments
 (0)