PDF changing on open and save? #184
-
Hi, I'm looking to modify some existing PDFs I have but wanted to make certain that I could open and save them without loss. I created the following .NET project: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PdfSharp" Version="6.1.1" />
</ItemGroup>
</Project> And the following Program.cs: using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
PdfDocument input = PdfReader.Open(args[0], PdfDocumentOpenMode.Modify);
PdfDocument output = input;
output.Save(args[1]); When I run the following: $ dotnet run -- Input.pdf Output.pdf
$ ls -l Input.pdf Output.pdf
-rw-r--r-- 1 oco oco 31070533 Jun 14 2023 Input.pdf
-rw-rw-r-- 1 oco oco 30917970 Oct 12 21:28 Output.pdf I get an output PDF that is slightly smaller than the input PDF. Obviously they are not identical so my questions are:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
PDFsharp reads everything into memory and writes a completely new file on saving. Some orphaned objects may be dropped in that process. It is normal that the file size changes. |
Beta Was this translation helpful? Give feedback.
PDFsharp reads everything into memory and writes a completely new file on saving. Some orphaned objects may be dropped in that process.
Parts of PDF files are zipped. This is a lossless process, but size can change if an object is unzipped and re-zipped.
PDFsharp does not support XREF streams, but that usually leads to larger PDF files after saving.
It is normal that the file size changes.