Prompt
How do I rotate and stamp PDFs in code?
Latest observation
Oct 20, 2026
OpenAI APIWeb search: off
You generally do this by using a PDF library that can:
- open the PDF
- rotate pages by changing page rotation or transforming content
- stamp content onto each page, such as text, images, or watermarks
- save the modified PDF
The exact code depends on the language and library. Here are common approaches.
Python example with PyMuPDF
PyMuPDF is very convenient for both rotating and stamping.
Install
pip install pymupdf
Rotate pages and add a stamp
import fitz # PyMuPDF
doc = fitz.open("input.pdf")
for page in doc:
# Rotate page 90 degrees clockwise
page.set_rotation((page.rotation + 90) % 360)
# Add a diagonal stamp-like text watermark
rect = page.rect
page.insert_text(
(rect.width / 2 - 100, rect.height / 2),
"APPROVED",
fontsize=36,
rotate=45,
color=(1, 0, 0),
overlay=True,
)
doc.save("output.pdf")
doc.close()
Notes
set_rotation()changes the page rotation metadata.insert_text(... rotate=45)adds a visible stamp/watermark.overlay=Trueplaces the stamp on top of existing content.
Python example with ReportLab + PyPDF2
If you want to create a stamp separately and merge it onto pages, this is a common pattern.
Install
pip install pypdf reportlab
Code
from pypdf import PdfReader, PdfWriter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from io import BytesIO
# Create a stamp PDF in memory
packet = BytesIO()
c = canvas.Canvas(packet, pagesize=letter)
c.setFont("Helvetica-Bold", 36)
c.setFillColorRGB(1, 0, 0)
c.saveState()
c.translate(300, 400)
c.rotate(45)
c.drawCentredString(0, 0, "APPROVED")
c.restoreState()
c.save()
packet.seek(0)
stamp_pdf = PdfReader(packet)
reader = PdfReader("input.pdf")
writer = PdfWriter()
for page in reader.pages:
# Rotate page clockwise 90 degrees
page.rotate(90)
# Merge stamp
page.merge_page(stamp_pdf.pages[0])
writer.add_page(page)
with open("output.pdf", "wb") as f:
writer.write(f)
Notes
page.rotate(90)rotates the page.merge_page()stamps one PDF page onto another.
JavaScript / Node.js with pdf-lib
Install
npm install pdf-lib
Code
const fs = require('fs');
const { PDFDocument, rgb, StandardFonts, degrees } = require('pdf-lib');
async function main() {
const pdfBytes = fs.readFileSync('input.pdf');
const pdfDoc = await PDFDocument.load(pdfBytes);
const font = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const pages = pdfDoc.getPages();
for (const page of pages) {
// Rotate page
const currentRotation = page.getRotation().angle;
page.setRotation(degrees((currentRotation + 90) % 360));
// Stamp text
const { width, height } = page.getSize();
page.drawText('APPROVED', {
x: width / 2 - 100,
y: height / 2,
size: 36,
font,
color: rgb(1, 0, 0),
rotate: degrees(45),
opacity: 0.5,
});
}
const out = await pdfDoc.save();
fs.writeFileSync('output.pdf', out);
}
main();
C# example with iText 7
If you’re using .NET, iText is powerful for stamping and rotation.
using iText.Kernel.Pdf;
using iText.Kernel.Geom;
using iText.Layout;
using iText.Layout.Element;
using iText.Kernel.Colors;
using iText.Layout.Properties;
using var reader = new PdfReader("input.pdf");
using var writer = new PdfWriter("output.pdf");
using var pdf = new PdfDocument(reader, writer);
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
var page = pdf.GetPage(i);
page.SetRotation((page.GetRotation() + 90) % 360);
var canvas = new Canvas(new iText.Kernel.Pdf.Canvas.PdfCanvas(page), pdf, page.GetPageSize());
canvas.ShowTextAligned(
new Paragraph("APPROVED").SetFontSize(36).SetFontColor(ColorConstants.RED),
page.GetPageSize().GetWidth() / 2,
page.GetPageSize().GetHeight() / 2,
i,
TextAlignment.CENTER,
VerticalAlignment.MIDDLE,
45
);
canvas.Close();
}
Important distinction: rotate page vs rotate content
There are two different tasks:
- Rotate page: changes how the viewer displays the page.
- Rotate content: physically transforms the content stream.
Usually, for scanned documents or normal viewing, page rotation metadata is enough. For permanent transformation of what appears on the page, you may need to rotate the content itself.
Which library should you use?
- Python: PyMuPDF is easiest for both stamping and rotation.
- Node.js: pdf-lib is straightforward.
- .NET: iText 7 is robust.
- Need high-volume or complex PDF manipulation: consider commercial libraries or more advanced PDF toolkits.
If you want, I can give you:
- a single-language solution for Python, JavaScript, Java, or C#
- code that stamps an image watermark
- code that rotates only specific pages
- code that stamps the same PDF on every page