That's a common observation and often a point of confusion when working with PDFs! While you're removing page objects, the overall file size might not decrease significantly because a PDF's size is determined by more than just the number of pages.
Here are the main reasons why your new PDF might still be large:
-
Embedded Fonts: This is often the biggest contributor. If the original PDF embeds fonts (which is necessary for consistent display across different systems), that font data is usually stored once within the PDF structure. Even if you only keep the first few pages, the embedded font data is still required for those pages and remains in the new file. Removing pages doesn't remove the embedded font file itself.
-
Embedded Images/Resources: PDFs can contain large embedded raster images (like scanned photos or documents) or complex vector graphics. If these resources are used on the pages you are keeping, their data remains. Removing pages only removes the references to resources used exclusively on the deleted pages. If the large images are on the first 'n' pages, the size won't drop much.
-
Document Structure and Overhead: Every PDF has a certain amount of overhead related to its internal structure, such as the cross-reference table, catalog, page tree, metadata, color spaces, etc. This structural data is necessary regardless of the number of pages and remains largely intact.
-
Compression: While the content streams on the pages you remove are gone, the remaining content streams are typically compressed using algorithms like Flate (DEFLATE). Simply removing pages doesn't trigger a re-compression or optimization of the remaining content that could potentially further reduce its size. PdfSharp is primarily copying objects; it's not necessarily an optimizing "save-as" tool.
-
Shared Resources: Resources (like fonts, images, color profiles) might be shared across many pages. Removing some pages doesn't eliminate these shared resources if they are still needed by the pages you're keeping.
In essence: You're removing the "content" specific to pages n+1 to the end, but you're keeping the structural overhead, any embedded fonts, and any large resources (like images) that are used by the first n pages.
What can you do?
- Optimize/Re-save: If your goal is purely size reduction after subsetting, you might need to process the newly created PDF with a tool specifically designed for PDF optimization (like Adobe Acrobat's "Save As Optimized" or Ghostscript with specific settings). These tools can re-compress streams, downsample images (if appropriate), remove redundant objects, and optimize the structure.
- Check Image vs. Text: If the original PDF is mostly text and vector graphics, the file size should decrease more noticeably. If it's primarily scanned images, the size reduction will likely be minimal unless the images themselves are reprocessed.
- Consider the Source: How was the original PDF created? PDFs generated from scanned documents (Image-only PDFs) will behave differently regarding size than PDFs generated from word processors or design software (often text/vector-based).
PdfSharp is excellent for programmatically creating and manipulating PDF structures, but it doesn't inherently perform deep content optimization or re-compression like a dedicated optimization tool would.