java OpenPDF收缩PDF

hgb9j2n6  于 2023-08-02  发布在  Java
关注(0)|答案(2)|浏览(148)

设置:从多页TIFF创建OpenPDF,将每个TIFF页添加为com.lowagie.text.Image

...
com.lowagie.text.Document pdfDocument = new com.lowagie.text.Document();
com.lowagie.text.Image image01 = getPage(pageNumber);
document.add(image01);
...

字符串

问题:文件大小
问题:如何减少文件大小?

iyr7buue

iyr7buue1#

希望这对外面的人有帮助。我使用了以下

com.lowagie.text.pdf.PdfStamper.setFullCompression();
com.lowagie.text.pdf.PdfStamper.getWriter().setFullCompression();
com.lowagie.text.pdf.PdfWriter.setFullCompression();
com.lowagie.text.pdf.PdfWriter.setCompressionLevel(com.lowagie.text.pdf.PdfStream.BEST_COMPRESSION);

字符串
而不是用

Image image = Image.getInstance(((java.io.File) singlePageTiff).getAbsolutePath());
//or
Image image = Image.getInstance((java.awt.image.BufferedImage) pageImage, null, forceBlackAndWhite);


我使用了压缩图像

java.awt.image.BufferedImage pageImage = ...
int width = pageImage.getWidth(); 
int height  = pageImage.getHeight();
boolean reverseBits = false;
int typeCCITT = com.lowagie.text.Image.CCITTG4; 
//see com.lowagie.text.Image.CCITT_BLACKIS1; 
int parameters = 0;          
byte[] data = ((java.awt.image.DataBufferByte) pageImage.getRaster().getDataBuffer()).getData();
Objects.requireNonNull(data);
int[] transparency = null;
byte[] g4 = com.lowagie.text.pdf.codec.CCITTG4Encoder.compress(data, width, height);
com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(width, height, reverseBits, typeCCITT, parameters, g4, transparency);

11dmarpk

11dmarpk2#

也许你会尝试这样的东西(只是作为例子,你必须将它集成到你的代码中):

com.lowagie.text.Image image01 = getPage(pageNumber);
float desiredWidth = 600; // Set the desired width (in points or pixels)
image01.scaleAbsolute(desiredWidth, -1); // -1 maintains aspect ratio
document.add(image01);

字符串

相关问题