书法字帖 PDF转化为可打印PDF

时间:2024-07-29 16:05:50

书法类的PDF,因为底色是黑色的,打印起来特别费墨,所以需要转化成白底黑字的文件,

才好打印。

1)用 pdfbox 的 ExtractImages 命令,抽出所有的图片

https://pdfbox.apache.org/2.0/commandline.html

java -jar pdfbox-app-2.0..jar ExtractImages "E:\Calligraphy\jiu.pdf"

2 ) 用 imagemagick 把图片转化为白底黑字。

可以创建一个bash 文件,在Cygwin里执行

for i in *.jpg
do
magick $i -negate negate_`basename $i .jpg`.jpg
done

不可以直接 magick *.jpg -negate negate_.jpg . 这样转化出来的图片,不能和原图一一对应,后续就不好做了。

3)用gimp2 来分别处理每一张 图片

a) [颜色-> 去色] 转化为灰度图片

b) [颜色->阀值] 去掉多余的灰点

c) *选择工具 和 油漆桶 进行 精细去除

d) 用画笔工具,进行更精细的去除

4 ) 用自己做的工具,转化文件名 xxx1.jpg xxx01.jpg  xxx2.jpg xxx02.jpg . 这样为下一步程序能顺利排序做好准备

5)运行java 程序

    public static void main(String[] args) {

        if (args.length < 3) {
System.out.println("ImagesToPdf <folderpath> <width> <height>");
System.out.println("For example: ImagesToPdf \"E:\\Calligraphy\\create_pdf\\jiu_liqi\\jiu01_last\" 800 1200");
return;
} List<String> alist = listFilesAndFolders(args[0]);
if (alist.size() < 2) {
System.out.println("Error:File Count in Folder is < 2");
return;
} float width = Float.parseFloat(args[1]);
float height = Float.parseFloat(args[2]); PDDocument document = new PDDocument(); try {
for(String s:alist) {
PDPage page = new PDPage(new PDRectangle(width, height));
PDPageContentStream contentStream = new PDPageContentStream(document, page);
document.addPage(page);
PDImageXObject img = PDImageXObject.createFromFile(args[0] + "/" + s, document);
contentStream.drawImage(img, 0, 0);
contentStream.close();
} document.save("out.pdf");
document.close(); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static List<String> listFilesAndFolders(String directoryName){
File directory = new File(directoryName);
//get all the files from a directory
ArrayList<String> alist = new ArrayList<String>();
File[] fList = directory.listFiles();
for (File file : fList){
alist.add(file.getName());
}
alist.sort(Comparator.naturalOrder());
return alist;
}