Page 1 of 1

Schwierigkeiten beim Einbetten des ICC-Profils in PDF mit PDFBox, iText und Ghostscrip

Posted: 05 Jan 2025, 05:53
by Guest
Ich habe daran gearbeitet, ein ICC-Profil (AdobeRGB1998.icc) in eine PDF-Datei einzubetten. Trotz mehrerer Versuche mit verschiedenen Tools gelingt es mir nicht, das ICC-Profil in den Metadaten anzuzeigen. Ich habe unzählige Stunden damit verbracht und die folgenden Ansätze ausprobiert:
PDFBox
Java Code

Code: Select all

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class EmbedICCWithPDFBox {
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: java EmbedICCWithPDFBox 
  ");
return;
}

String pdfPath = args[0];
String outputPdfPath = args[1];
String iccPath = args[2];

try {
embedICCProfile(pdfPath, outputPdfPath, iccPath);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void embedICCProfile(String pdfPath, String outputPdfPath, String iccPath) throws IOException {
PDDocument document = PDDocument.load(new File(pdfPath));

try (FileInputStream iccStream = new FileInputStream(iccPath)) {
PDStream iccPDStream = new PDStream(document, iccStream);
iccPDStream.addCompression();

PDOutputIntent outputIntent = new PDOutputIntent(document, iccPDStream);
outputIntent.setInfo("sRGB IEC61966-2.1");
outputIntent.setOutputCondition("sRGB IEC61966-2.1");
outputIntent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
outputIntent.setRegistryName("http://www.color.org");

document.getDocumentCatalog().addOutputIntent(outputIntent);

System.out.println("ICC profile embedded successfully.");
}

document.save(outputPdfPath);
document.close();
}
}

iText
Java-Code

Code: Select all

import com.itextpdf.kernel.pdf.*;
import java.io.FileInputStream;
import java.io.IOException;

public class EmbedICCWithIText {
public static void embedICCProfile(String pdfPath, String outputPdfPath, String iccPath) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfPath), new PdfWriter(outputPdfPath));

try (FileInputStream iccStream = new FileInputStream(iccPath)) {
byte[] iccProfileBytes = iccStream.readAllBytes();
PdfStream iccPdfStream = new PdfStream(iccProfileBytes);
iccPdfStream.put(PdfName.N, new PdfNumber(3));
iccPdfStream.put(PdfName.Length, new PdfNumber(iccProfileBytes.length));
iccPdfStream.put(PdfName.Filter, PdfName.FlateDecode);

PdfDictionary outputIntentDict = new PdfDictionary();
outputIntentDict.put(PdfName.Type, PdfName.OutputIntent);
outputIntentDict.put(PdfName.S, PdfName.GTS_PDFA1);
outputIntentDict.put(PdfName.OutputConditionIdentifier, new PdfString("AdobeRGB (1998)"));
outputIntentDict.put(PdfName.OutputCondition, new PdfString("Adobe RGB (1998)"));
outputIntentDict.put(PdfName.Info, new PdfString("Adobe RGB (1998)"));
outputIntentDict.put(PdfName.DestOutputProfile, iccPdfStream);

PdfArray outputIntents = new PdfArray();
outputIntents.add(outputIntentDict);
pdfDoc.getCatalog().put(PdfName.OutputIntents, outputIntents);

System.out.println("ICC profile embedded successfully.");
}
pdfDoc.close();
}

public static void main(String[] args) {
if (args.length != 3) {
System.err.println("Usage: java EmbedICCWithIText 
   ");
return;
}

String pdfPath = args[0];
String outputPdfPath = args[1];
String iccPath = args[2];

try {
embedICCProfile(pdfPath, outputPdfPath, iccPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}

GhostScript
Befehl

Code: Select all

gswin64c.exe -o C:/ghost/output_done_Ghostscript.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sProcessColorModel=DeviceRGB -sOutputICCProfile=C:/ghost/AdobeRGB1998.icc -dEmbedAllFonts=true -dSubsetFonts=true -dMaxSubsetPct=100 -dPDFX=true -dCompatibilityLevel=1.4 C:/ghost/test1_no_xref.pdf

Problem:
Trotz der gemeldeten erfolgreichen Einbettung wird das ICC-Profil von ExifTool nicht erkannt:

Code: Select all

exiftool.exe -G1 "C:/PDFWorkFlow/output_done_PDFBox.pdf"
Was könnte dazu führen, dass das ICC-Profil nicht korrekt in die PDF-Datei eingebettet wird? Gibt es zusätzliche Schritte oder alternative Methoden, die ich ausprobieren kann, um sicherzustellen, dass das ICC-Profil eingebettet und erkennbar ist?
Für Einblicke oder Vorschläge wäre ich sehr dankbar. Danke!