Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

For a legacy iTextSharp project, use XML Worker to convert well-formed XHTML and CSS, then test each absolutely positioned element against the PDF page size and margins. HTML position:absolute is not a promise of browser-identical layout: the page must accommodate the coordinates, and the element does not reserve space in the normal content flow. For a label, stamp, or other object that must land at exact PDF coordinates, draw it with iTextSharp’s PDF canvas APIs instead.

Choose the right placement method

HTML describes higher-level content such as paragraphs and tables; a PDF must preserve a page appearance. That difference matters when moving browser-oriented CSS into a fixed page. iText’s guidance is to use XML Worker for CSS-aware XHTML conversion, but it also warns that an HTML coordinate layout may not fit the chosen PDF page.

  • Use XML Worker when the document is primarily HTML content and a constrained set of CSS positioning rules is adequate.
  • Use the PDF canvas when a stamp, label, line, shape, or text block needs controlled placement or layering independent of HTML flow.
  • Consider iText 7 with pdfHTML for new development. iTextSharp and XML Worker are legacy, deprecated/EOL products; iText recommends its newer platform for new projects and broader HTML/CSS support.

XML Worker is not a full browser engine. Treat its output as a PDF layout produced from XHTML and supported CSS, not as a guarantee that every browser rendering feature will carry over unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set up iTextSharp and XML Worker

Use the iTextSharp 5 / XML Worker pattern below in an existing C# application. Add the iTextSharp and itextsharp.xmlworker packages, and keep their DLL/package versions aligned. iText’s .NET installation guidance specifically cautions against mixing iTextSharp and XML Worker versions.

The example writes a Letter PDF to a byte array. It supplies the XHTML and a separate CSS stream to ParseXHtml. Change the page size and margins to match the document you are designing.

using System;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;

public static byte[] CreatePdf()
{
    const string xhtml = @"<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head><meta charset='utf-8' /></head>
<body>
  <p>Ordinary content stays in the document flow.</p>
  <div class='fixed-label'>Positioned label</div>
</body>
</html>";

    const string cssText = @".fixed-label {
  position: absolute;
  left: 72pt;
  top: 100pt;
  width: 220pt;
}";

    using (var ms = new MemoryStream())
    {
        using (var document = new Document(PageSize.LETTER, 36, 36, 36, 36))
        {
            PdfWriter writer = PdfWriter.GetInstance(document, ms);
            document.Open();

            using (var html = new MemoryStream(Encoding.UTF8.GetBytes(xhtml)))
            using (var css = new MemoryStream(Encoding.UTF8.GetBytes(cssText)))
            {
                XMLWorkerHelper.GetInstance().ParseXHtml(
                    writer, document, html, css);
            }
        }
        return ms.ToArray();
    }
}

The strings are deliberately XHTML-compatible: elements are closed, attribute values are quoted, and the document has a root html element. In a real application, construct or normalize the input as valid XHTML before passing it to XML Worker.

Position an HTML element without losing it off-page

Design within the PDF page

The example uses left, top, and width as part of a designed page layout. Set the PDF page size and margins first, then choose offsets that keep the entire element on that page. If the coordinates and dimensions extend beyond the page, content can be clipped or appear misplaced. Do not assume that a coordinate arrangement copied from a browser viewport will fit a PDF page.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Try one positioned element before adding the rest of the design. Keep a simple page sketch or coordinate plan alongside the template so that changes to paper size or margins do not silently invalidate the positions.

Reserve space for normal-flow content

An absolutely positioned element is removed from ordinary flow: paragraphs and other flowing content do not automatically move aside to make room for it. If a positioned header overlaps a paragraph, the remedy may be to change the template’s flow content or coordinate plan, not simply to keep adjusting the offset. For repeated or content-dependent layouts, prefer normal flow where it can meet the design requirement.

Pass the stylesheet explicitly

XML Worker can receive CSS through the stylesheet stream, as in the sample, or through suitable inline or embedded CSS in the XHTML. The key debugging point is to make sure the stylesheet is actually available to the converter. The older HTMLWorker has only basic CSS support; do not expect it to apply a stylesheet as XML Worker does.

Use PdfWriter canvases for exact PDF placement

When a layout is fundamentally a fixed-position PDF rather than flowing HTML, use the PDF-level APIs. PdfWriter exposes GetDirectContent() and GetDirectContentUnder() canvases for drawing above or below page content. iText’s absolute-placement guidance covers text, rectangles, lines, fitting, and truncation patterns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For text, define a bounded rectangle and use a ColumnText-style layout rather than sending an arbitrarily long string to a point. A bounded box gives the application a place to check whether text fits and to decide what to do when it does not. For graphics, draw on the appropriate canvas after the document and page setup are established. Keep these PDF drawing operations separate from the HTML flow so that the source of each visual element remains clear.

Requirement XML Worker in an existing iTextSharp app PdfWriter canvas iText 7 with pdfHTML
Reuse a legacy C# application Practical for constrained XHTML/CSS Useful for precise exceptions and overlays Requires migration
Browser-like HTML/CSS fidelity Limited and XHTML-oriented Not an HTML renderer Newer, broader supported layout model
Exact coordinates for a stamp, label, or form field May work with tested templates Strong fit Strong fit with newer layout APIs
New production development Maintenance risk: iTextSharp/XML Worker are deprecated/EOL Low-level technique; does not replace HTML conversion Preferred direction in iText guidance

Validate the document before tuning offsets

  1. Make the input valid XHTML. Close every tag, nest elements correctly, and use XML-compatible forms such as <br /> rather than an unclosed <br>.
  2. Confirm both assemblies are present and aligned. Check that the iTextSharp and XML Worker package/DLL versions match; do not combine unrelated versions.
  3. Confirm the CSS reaches XML Worker. Pass the CSS stream or include supported inline/embedded rules. Do not rely on HTMLWorker to apply a stylesheet.
  4. Check page geometry. Verify page size, margins, offsets, and element dimensions before changing CSS values. A layout that does not fit the PDF page can be clipped or misplaced.
  5. Reduce to a minimal case. Render one positioned element, then add flow content, images, and other CSS incrementally. This isolates whether the issue is the coordinate design, input markup, or a particular feature.
  6. Check release-specific behavior. If positioning changes after an upgrade, consult the XML Worker release notes; iText has recorded fixes for div positioning and added relative-positioning support over time.

Common failures and practical fixes

The positioned element disappears or is clipped

First compare its offsets and full width/height with the selected page and margins. Then reduce the example to one element. A coordinate layout can exceed the PDF page even when it looks reasonable in a browser viewport.

Rank #4
Sale
Funny Coding I Know HTML How To Meet Ladies T-Shirt
  • Funny saying for any front-end developer, web developer, computer programmer, computer systems engineer, mobile app developer, software developer, or code lover who likes to code, make funny programming jokes, and take memorable photos.
  • Wear it proudly at International Programmers' Day, school, coding classes, or coding communities! It also makes a funny present for a computer programming lover friend.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

The CSS seems to be ignored

Verify that XML Worker, not just the older HTMLWorker, is being used and that the CSS is passed to ParseXHtml or included in a form XML Worker can read. Simplify the rule to the position and dimensions under investigation before restoring unrelated styles.

Parsing fails on markup that works in a browser

Browsers often tolerate malformed HTML; XML Worker expects XHTML. Close and correctly nest tags, quote attribute values, and use XML-compatible empty elements such as <br />. Normalize the template rather than expecting the PDF converter to repair it.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Behavior changes after a package update

Check the versions of both assemblies and the relevant XML Worker release notes. Keep the package versions aligned, then reproduce the issue with a small input before making template-wide changes.

Best Value
I Know HTML How To Meet Ladies Funny Programming Language T-Shirt
  • Programming Language Lover Code Apparel. App or Web Design and Development Expert Funny Dress. Best Valentines Idea For Coding Lover. HTML Code or Meaning Costume
  • Funny I Know HTML - How To Meet Ladies Computer Programmer Quotes
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Text overlaps or runs outside its intended box

Absolute positioning does not reserve room in normal flow. For content whose length can vary, use normal flow if possible or define a bounded PDF text rectangle and check fit with a canvas-based approach.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and maintenance

The XML Worker conversion shown here is an in-process XHTML/CSS-to-PDF path, not a browser rendering session. The cited iText material does not publish a performance benchmark that can predict throughput for a particular template or application. Test with representative content—including the longest text and largest images your real documents contain—and measure the complete conversion path in your own environment.

For predictable output, keep templates small and valid, make page geometry explicit, and add complex positioning only where the design needs it. Retain representative output PDFs as regression fixtures when changing templates or dependencies. XML Worker received positioning-related fixes across releases, so a change in package version is a reason to validate output rather than assume coordinates render identically.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The bigger maintenance decision is whether to keep a legacy iTextSharp conversion alive or migrate. XML Worker was introduced as an extensible XHTML/CSS tool, but current iText package metadata labels iTextSharp EOL, and iText recommends iText 7 and pdfHTML for new projects. That direction is particularly relevant if the application needs broader HTML/CSS support or ongoing maintenance; a migration still requires adapting and testing the existing templates.

Or skip the browser setup

If your actual requirement is a screenshot or PDF capture of a rendered webpage—not a custom iTextSharp document with precise PDF canvas placement—ScreenshotNeo can capture a URL with one GET request. It is a different approach from converting your own XHTML template with XML Worker.

cURL example and ScreenshotNeo API documentation:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Python:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
  • Cookie/consent banners are accepted like a visitor and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before the shot; each step can be turned off.
  • Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing; response headers report the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.
  • The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. All features are available on every plan.

Sign up for 1,000 free screenshots a month with no card.

Quick Recap

Bestseller No. 2
SaleBestseller No. 4
Funny Coding I Know HTML How To Meet Ladies T-Shirt
Funny Coding I Know HTML How To Meet Ladies T-Shirt
Lightweight, Classic fit, Double-needle sleeve and bottom hem
$14.27
Bestseller No. 5
I Know HTML How To Meet Ladies Funny Programming Language T-Shirt
I Know HTML How To Meet Ladies Funny Programming Language T-Shirt
Funny I Know HTML - How To Meet Ladies Computer Programmer Quotes; Lightweight, Classic fit, Double-needle sleeve and bottom hem
$19.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.