diff --git a/pypdf/pdf.py b/pypdf/pdf.py index af2c10f055..4f69b74a7d 100644 --- a/pypdf/pdf.py +++ b/pypdf/pdf.py @@ -374,21 +374,20 @@ def addAttachment(self, fname, fdata): endobj """ - embeddedFilesNamesDictionary = DictionaryObject() - embeddedFilesNamesDictionary.update({ - NameObject("/Names"): ArrayObject( - [createStringObject(fname), filespec] - ) - }) + embeddedFilesDictionary = self._rootObject.setdefault( + NameObject("/Names"), + DictionaryObject()) - embeddedFilesDictionary = DictionaryObject() - embeddedFilesDictionary.update({ - NameObject("/EmbeddedFiles"): embeddedFilesNamesDictionary - }) - # Update the root - self._rootObject.update({ - NameObject("/Names"): embeddedFilesDictionary - }) + embeddedFilesNamesDictionary = embeddedFilesDictionary.setdefault( + NameObject("/EmbeddedFiles"), + DictionaryObject()) + + embeddedFilesList = embeddedFilesNamesDictionary.setdefault( + NameObject("/Names"), + ArrayObject()) + + embeddedFilesList.append(createStringObject(fname)) + embeddedFilesList.append(filespec) def appendPagesFromReader(self, reader, afterPageAppend=None): """ @@ -1007,6 +1006,15 @@ def removeText(self, ignoreByteStringObject=False): pageRef.__setitem__(NameObject('/Contents'), content) + def removeAttachments(self): + """ + removes attachments from this output. + + """ + embeddedFilesDictionary = self._rootObject.get("/Names", {}) + embeddedFilesDictionary.update({ + NameObject("/EmbeddedFiles"): DictionaryObject()}) + def addURI(self, pagenum, uri, rect, border=None): """ Add an URI from a rectangular area to the specified page. This uses the @@ -2682,7 +2690,7 @@ def getAttachFiles(self): :rtype: list """ res = [] - catalog = self.trailer['/Root'] + catalog = self._trailer['/Root'] # get the name tree catalog_name = self._get_dict_entry(catalog, '/Names') if not catalog_name: