From ff11cfff2296dd27240f806fb4b7136b75eae304 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 13 Dec 2021 13:06:06 +0100 Subject: [PATCH 1/3] Fix multiple write metadata to pdf --- .../logic/exporter/EmbeddedBibFilePdfExporter.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java b/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java index bfe8beb850f..7aa75fda00b 100644 --- a/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java +++ b/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java @@ -91,16 +91,24 @@ private void embedBibTex(String bibTeX, Path file, Charset encoding) throws IOEx } } + PDComplexFileSpecification fileSpecification; + if (names.containsKey(EMBEDDED_FILE_NAME)) { + fileSpecification = names.get(EMBEDDED_FILE_NAME); + } else { + fileSpecification = new PDComplexFileSpecification(); + } if (efTree != null) { - PDComplexFileSpecification fileSpecification = new PDComplexFileSpecification(); - fileSpecification.setFile(EMBEDDED_FILE_NAME); InputStream inputStream = new ByteArrayInputStream(bibTeX.getBytes(encoding)); + fileSpecification.setFile(EMBEDDED_FILE_NAME); PDEmbeddedFile embeddedFile = new PDEmbeddedFile(document, inputStream); embeddedFile.setSubtype("text/x-bibtex"); embeddedFile.setSize(bibTeX.length()); fileSpecification.setEmbeddedFile(embeddedFile); - names.put(EMBEDDED_FILE_NAME, fileSpecification); + if (!names.containsKey(EMBEDDED_FILE_NAME)) { + names.put(EMBEDDED_FILE_NAME, fileSpecification); + } + efTree.setNames(names); nameDictionary.setEmbeddedFiles(efTree); document.getDocumentCatalog().setNames(nameDictionary); From b17ddfdd774f2f93cb245bd24bf0f3415cd56e8b Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 13 Dec 2021 13:31:44 +0100 Subject: [PATCH 2/3] Reformulated error when trying to export metadata to write protected pdf --- .../jabref/logic/exporter/EmbeddedBibFilePdfExporter.java | 7 ++++++- src/main/resources/l10n/JabRef_en.properties | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java b/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java index 7aa75fda00b..ab584fbfbb4 100644 --- a/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java +++ b/src/main/java/org/jabref/logic/exporter/EmbeddedBibFilePdfExporter.java @@ -15,6 +15,7 @@ import org.jabref.logic.bibtex.BibEntryWriter; import org.jabref.logic.bibtex.FieldWriter; import org.jabref.logic.bibtex.FieldWriterPreferences; +import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.OS; import org.jabref.logic.util.StandardFileType; import org.jabref.logic.util.io.FileUtil; @@ -106,7 +107,11 @@ private void embedBibTex(String bibTeX, Path file, Charset encoding) throws IOEx fileSpecification.setEmbeddedFile(embeddedFile); if (!names.containsKey(EMBEDDED_FILE_NAME)) { - names.put(EMBEDDED_FILE_NAME, fileSpecification); + try { + names.put(EMBEDDED_FILE_NAME, fileSpecification); + } catch (UnsupportedOperationException e) { + throw new IOException(Localization.lang("File '%0' is write protected.", file.toString())); + } } efTree.setNames(names); diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index bf825044c48..1bd26af7d11 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -916,6 +916,7 @@ Writing\ metadata\ for\ selected\ entries...=Writing metadata for selected entri Writing\ metadata...=Writing metadata... Embed\ BibTeXEntry\ in\ PDF.=Embed BibTeXEntry in PDF. +File\ '%0'\ is\ write\ protected.=File '%0' is write protected. Write\ BibTeXEntry\ as\ XMP\ metadata\ to\ PDF.=Write BibTeXEntry as XMP metadata to PDF. Write\ BibTeXEntry\ metadata\ to\ PDF.=Write BibTeXEntry metadata to PDF. Write\ metadata\ to\ PDF\ files=Write metadata to PDF files From 2ba272f1d877d1b7372776bdba736607e966863e Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Mon, 13 Dec 2021 13:34:11 +0100 Subject: [PATCH 3/3] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9b718e7265..44500cc525a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where an exception would occur when clicking on a DOI link in the preview pane [#7706](https://github.com/JabRef/jabref/issues/7706) - We fixed an issue where XMP and embedded BibTeX export would not work [#8278](https://github.com/JabRef/jabref/issues/8278) - We fixed an issue where the XMP and embedded BibTeX import of a file containing multiple schemas failed [#8278](https://github.com/JabRef/jabref/issues/8278) +- We fixed an issue where writing embedded BibTeX import fails due to write protection or bibtex already being present [#8332](https://github.com/JabRef/jabref/pull/8332) ### Removed