537*_*037 6 java metadata file read-write eps
I'm currently reading and writing .EPS file to manipulate/add metadata (Keywords and Tags) in the file.
PS: File encoding is Windows-1251 or Cp1251 -Russian-
I'm reading EPS file like this: (String lines; is a global variable)
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "Cp1251"))) {
String line;
while((line = br.readLine()) != null) {
if(line.contains("</xmpTPg:SwatchGroups>")) {
lines.add(line);
lines.add(descriptionKwrds);
}
else
lines.add(line);
System.out.println(line);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(script.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(script.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(script.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)
In above descriptionKwrds is the metadata (tags) that I want to manipulate an EPS file like:
String descriptionKwrds = "<photoshop:AuthorsPosition>icon vector illustration symbol bubble sign</photoshop:AuthorsPosition>";
Run Code Online (Sandbox Code Playgroud)
And writing EPS file like this:
try {
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getName()), "Cp1251"))) {
for(String s : lines)
out.write(s + "\n");
out.flush();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(script.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(script.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)
File is reading and writing correctly, but when I open newly generated file. It says that the file is corrupted.
Files before and after manipulation are file1 and file2 respectively. And using ESP Converter to open EPS files online.
How I can achieve it? Anyone can help me. Thanks!
好的,您的问题是您的EPS文件是“带有预览的EPS”。除了实际的PostScript程序外,还有一个位图,任何将EPS放在页面上的应用程序都可以用来向用户显示“预览”。
该文件的开头是二进制文件,如下所示:
C5 D0 D3 C6 20 00 00 00 DC 49 05 00 00 00 00 00
00 00 00 00 FC 49 05 00 AE AC 4D 00 FF FF 00 00
Run Code Online (Sandbox Code Playgroud)
如果您阅读Adobe技术说明5002“封装的PostScript文件格式规范”并查看第23页,您将看到它定义了DOS EPS二进制文件头,该头以十六进制C5D0D3C6开头,就像您的文件一样。因此,您可以看到文件具有DOS标头,该标头定义了预览。
现在,字节4-7定义PostScript的开始,字节8-11定义PostScript部分的长度。12-15是图元文件的开头(根据您的情况为0,所以不存在),而16-19是字节长度,再次为0。然后在字节20-23处是TIFF表示的开始,而字节24 TIFF的长度为27。最后,剩下的两个字节中有标头的校验和。这里我们有0xFFFF,这意味着“忽略校验和”。在这种情况下,标头已用两个字节(0x00)填充以形成总共32个字节,这就是为什么PostScript节的偏移量为0x20的原因。
您的问题是,因为您已经向PostScript部分添加了内容(因此增加了其大小),但是没有更新文件头,以包含PostScript部分的新长度或预览的新位置,所以任何EPS使用者将无法删除预览。实际上,您已经破坏了PostScript程序。
您要么需要更新文件头,要么通过删除文件头并从末端修剪位图来剥离预览位图,以生成“纯” EPS文件(即,没有预览的EPS文件)。
我几乎忘了增加一些澄清;您没有在EPS文件中更新“关键字”或“标签”。您正在添加执行PostScript运算符的PostScript语言程序代码。在这种情况下,当通过类似“ Disitller”的PostScript解释程序(即,将PDF生成为输出的解释程序)运行时,PDF文件的元数据将被更改。您根本不需要更改EPS的元数据(通过标题中的注释即可完成)。对于不是Distiller的PostScript使用者,所做的更改将完全无效。
[更新]
修改'file2'的标头(即添加了pdfmark的文件),如下所示:
C5 D0 D3 C6 20 00 00 00 32 26 05 00 00 00 00 00
00 00 00 00 52 26 05 00 AE AC 4D 00 FF FF 00 00
Run Code Online (Sandbox Code Playgroud)
生成工作文件。似乎修改实际上使文件更短。PostScript部分的原始大小为0x0549DC,而TIFF位图的偏移量为0x0549FC。修改后,PostScript部分的大小为0x052632,TIFF位图的偏移量为0x052652。
我暗中怀疑这是由于CR / LF转换引起的,如果这样的话,也将更正存储在文件末尾的TIFF位图(我注意到末尾的二进制确实确实有所不同)。您需要以二进制文件而非文本的形式读取和写入此文件。