Recursive AI

Add an image or a watermark to a PDF

All onepdf recipes

Draw a PNG or JPEG onto a page of an existing PDF in C#, stamp a translucent “DRAFT” across every page, and put a faint logo behind the content.

C#
using OnePdf;
 
using PdfDocument doc = PdfDocument.Open("report.pdf", new PdfOpenOptions { AllowEditing = true });
 
// An image on one page. PNG is read and stored losslessly; JPEG is embedded as it is (AddJpegImage).
PdfCreatedImage logo = doc.AddPngImage(File.ReadAllBytes("logo.png"));
PdfCreatedImage photo = doc.AddJpegImage(File.ReadAllBytes("photo.jpg"));
PdfPage first = doc.Pages[0];
using (PdfPageContentBuilder b = first.BeginContent())       // over the existing content
{
    b.DrawImage(logo, 522, 738, 36, 36);                       // x, y of the lower left corner, width, height (points)
    b.DrawImage(photo, 54, 100, 240, 150);
    b.Commit();                                                // nothing is written before Commit
}
 
// A text watermark on every page, upright even on rotated pages.
doc.AddTextStamp("DRAFT", doc.AddStandardFont("Helvetica-Bold"), 120,
    new PdfStampOptions { Opacity = 0.15, RotationDegrees = 45, Color = (0.6, 0.1, 0.1) });
 
// An image watermark under the content of pages 2 and 3, centered on each page.
doc.AddImageStamp(logo, 200, 200,
    new PdfStampOptions { Opacity = 0.1, Placement = PdfContentPlacement.Underlay }, pageIndexes: [1, 2]);
 
doc.Save("report-stamped.pdf");

The first page gets the logo in its top right corner and the photo near the bottom. Every page gets “DRAFT” diagonally at 15% opacity, and pages 2 and 3 get the logo, large and faint, under their text.

Images

  • AddJpegImage embeds the JPEG file as it is, without decoding or recompressing it. Baseline and progressive JPEGs with 1, 3 or 4 components work; arithmetic-coded and lossless JPEGs are refused.
  • AddPngImage reads a PNG and stores it losslessly. An alpha channel becomes a soft mask, so transparency is kept.
  • Other formats go in as raw samples through AddRawImage, or convert them to PNG first.
  • An image added once can be drawn on any number of pages and is stored once.

Placing content

  • Coordinates are in points from the lower left corner of the page, y going up. A US Letter page is 612 × 792 points.
  • page.BeginContent() draws over the existing content. Pass PdfContentPlacement.Underlay to draw under it. Either way the page’s own content is left byte for byte as it was.
  • The builder also draws text (SetFont, DrawText, DrawTextBox), lines, rectangles and gradients. Text drawn this way isn’t shaped: for Arabic, Indic and other complex scripts use OnePdf.Layout (see Arabic and Hindi text).
  • Content you draw isn’t rotated with the page, so on a page with /Rotate it appears turned. The stamp helpers compensate, so stamps read upright.

Stamps

AddTextStamp and AddImageStamp draw into one shared form and place it on each chosen page (all pages by default), centered on the visible area unless you set CenterX and CenterY. PdfStampOptions sets the opacity, rotation, color, blend mode and placement over or under the content.

A standard font such as Helvetica isn’t embedded. For PDF/A, or to choose your own typeface, embed a TrueType or OpenType font with doc.AddFont(bytes).

All drawing calls are in Content creation.

For images, logos and watermarks on existing pages, see Add an image to a PDF in C#.