empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
492 stars 114 forks source link

Does anyone know how to remove image from a PDF? #135

Closed Charltsing closed 2 months ago

Charltsing commented 2 months ago

2.zip Removed-err.zip

2

PdfDocument pdfDocument = PdfReader.Open("2.pdf", PdfDocumentOpenMode.Modify);
foreach (PdfPage page in pdfDocument.Pages)
{
    PdfResources resurce = page.Resources;
    PdfDictionary xobjects = (PdfDictionary)resurce.Elements["/XObject"];
    foreach (var xobject in xobjects.Elements)
    {
        if (xobject.Key == "\Im2") xobjects.Elements.Remove(xobject.Key);     
    }
}
pdfDocument.Save("2-new.pdf");
pdfDocument.Close();

PDF files that delete images in this way will report an error when opened

Does anyone know how to properly remove images from a PDF?

ThomasHoevel commented 2 months ago

You also have to remove all references to "\Im2" from the content stream.

Please use Discussions or the Forum for that sort of questions.

Charltsing commented 2 months ago

You also have to remove all references to "\Im2" from the content stream.

Please use Discussions or the Forum for that sort of questions.

https://github.com/empira/PDFsharp/discussions/136 How to get the contents stream?