public boolean printPdf(String filePath, PrintService printService) {
try {
PdfUtil.getLicense();
PdfViewer viewer = new PdfViewer();
viewer.bindPdf(filePath);
viewer.setAutoResize(true); // Print the file with adjusted size
viewer.setAutoRotate(true); // Print the file with adjusted rotation
viewer.setPrintPageDialog(false); // Do not produce the page number dialog when printing
viewer.setResolution(600);
PdfPrinterSettings printerSettings = new PdfPrinterSettings();
PrintPageSettings pageSettings = new PrintPageSettings();
printerSettings.setPrinterName("RICOH P 201W");
viewer.printDocumentWithSettings(pageSettings, printerSettings);
viewer.close();
return true;
}catch (Exception e) {
log.error("执行pdf打印异常:{}", JSON.toJSONString(e));
return false;
}
} 这是我的 打印 代码 打出来的 pdf 很模糊 怎么解决?
@PonyWhite
打印PDF时出现模糊或发虚的问题,通常与打印机的分辨率设置有关。根据您的代码,您已经将分辨率设置为600 DPI,这通常是足够的,但有时打印机的实际能力可能会影响输出质量。
原因:打印质量不佳可能是由于打印机设置不当、分辨率不足、驱动程序过时或特定PDF文件的问题。
解决方案:
- 检查打印机设置:确保打印机的设置与您在代码中指定的分辨率一致。有些打印机在高分辨率下可能表现不佳。
- 调整分辨率:尝试将分辨率设置为更高的值,例如1200 DPI,以改善打印质量。
- 使用PDF打印机设置:确保您使用的
PdfPrinterSettings
和PrintPageSettings
配置正确,调整这些设置以优化打印效果。
- 更新打印机驱动程序:确保打印机驱动程序是最新的,过时的驱动程序可能会导致打印质量问题。
- 测试其他PDF文件:尝试打印其他PDF文件,以确定问题是否特定于某个文件。
下一步:请尝试上述建议,特别是调整分辨率和检查打印机设置。如果问题仍然存在,请提供更多信息,以便进一步诊断。
希望这些建议能帮助您解决打印模糊的问题!如果还有其他问题,请随时告诉我!
用的是 aspose.pdf.facades 的打印方法 打出来的都是糊的 怎么解决啊
@PonyWhite
请确保您正在使用最新版本的API。此外,请使用Microsoft Print to PDF打印并分享您的示例源文件和输出的PDF文档,我们将据此进一步协助您。
@asad.ali 是我的代码问题 还是版本问题 或者是 其他什么原因?
package org.aponywhite;
import com.aspose.pdf.Document;
import com.aspose.pdf.devices.PngDevice;
import com.aspose.pdf.devices.Resolution;
import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import java.awt.;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
// 2) 需要打印的 PDF 文件
String pdfPath = "D:\\JAVA\\BFR\\bfr\\temp\\AsposeTest.pdf";
// 3) 打印机(可留空用默认打印机)
String printerName = "null"; // 或置为 null 走默认打印机
// 4) 执行打印(600DPI 更清晰;也可传 300)
boolean ok = printPdfViaCore(pdfPath, printerName, 600);
System.out.println("print result: " + ok);
}
/** 方案B:Core 渲染为高DPI位图,再用 Java 打印(不依赖 facades) */
public static boolean printPdfViaCore(String filePath, String printerName, int dpi) {
try {
int targetDpi = dpi > 0 ? dpi : 600;
// 1) 把 PDF 每页渲染为 BufferedImage(内存中,不落盘)
List<BufferedImage> pages = renderPdfToImages(filePath, targetDpi);
// 2) 取得打印机
PrinterJob job = PrinterJob.getPrinterJob();
if (printerName != null && !printerName.trim().isEmpty()) {
PrintService svc = findDefaultPrintService();
if (svc == null) {
System.err.println("未找到打印机:" + printerName + ",改用默认打印机。");
} else {
job.setPrintService(svc);
}
}
// 3) 设定纸张(示例:A4,单位 point:72pt = 1in)
PageFormat pf = job.defaultPage();
Paper paper = new Paper();
// A4 尺寸:8.27 x 11.69 inch => 595 x 842 pt
paper.setSize(595, 842);
// 可打印区域尽量铺满整页(由驱动最终裁剪到可打印边界)
paper.setImageableArea(0, 0, 595, 842);
pf.setPaper(paper);
// 4) 定义 Printable:把每页图像按等比缩放画到 imageable 区域
Printable printable = (graphics, format, pageIndex) -> {
if (pageIndex < 0 || pageIndex >= pages.size()) return Printable.NO_SUCH_PAGE;
BufferedImage img = pages.get(pageIndex);
Graphics2D g2 = (Graphics2D) graphics;
// 高质量渲染参数
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
double ix = format.getImageableX();
double iy = format.getImageableY();
double iw = format.getImageableWidth();
double ih = format.getImageableHeight();
double sx = iw / img.getWidth();
double sy = ih / img.getHeight();
double scale = Math.min(sx, sy);
AffineTransform at = new AffineTransform();
at.translate(ix, iy);
at.scale(scale, scale);
g2.drawImage(img, at, null);
return Printable.PAGE_EXISTS;
};
// 5) 绑定页簿并打印
Book book = new Book();
book.append(printable, pf, pages.size());
job.setPageable(book);
job.print(); // 直接打印(不弹对话框)
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** 用 Aspose.PDF Core 把 PDF 渲染为高DPI BufferedImage 列表 */
private static List<BufferedImage> renderPdfToImages(String filePath, int dpi) throws Exception {
List<BufferedImage> list = new ArrayList<>();
Document doc = null;
try {
doc = new Document(filePath);
PngDevice dev = new PngDevice(new Resolution(dpi));
for (int i = 1; i <= doc.getPages().size(); i++) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
dev.process(doc.getPages().get_Item(i), baos);
try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
BufferedImage img = ImageIO.read(bais);
list.add(img);
}
}
}
} finally {
if (doc != null) {
doc.close();
}
}
return list;
// 如果页面特别多/内存吃紧,可改为边渲染边打印,或渲染到临时文件再读取
}
/** 按名称查找打印机(大小写不敏感,包含匹配) */
private static PrintService findDefaultPrintService() {
// 直接获取系统默认打印机
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
if (defaultService != null) {
System.out.println("默认打印机是: " + defaultService.getName());
} else {
System.out.println("没有找到默认打印机");
}
return defaultService;
}
}
AsposeTest.pdf (144.0 KB)
现在遇到一个问题 是 打印出来的pdf 左右边距不一样 左边宽 右边窄
@PonyWhite
感谢您分享示例PDF文件。请注意,我们需要调查实际问题的来源。代码片段看起来没有问题,但为了彻底分析问题,我们需要一些显示问题的输出图片。这也是为什么我们请求您使用Microsoft Print to PDF打印机生成PDF并与我们分享。这将有助于明确问题是与特定打印机有关还是与API有关。
aspose.jpg (86.2 KB)
左右两边边距不一样 但是我的pdf在预览时 两边是一样的
@PonyWhite
我们已在内部问题跟踪系统中创建以下新工单,并将按照 Free Support Policies 中提到的条款交付其修复。
问题编号:PDFJAVA-45297
如果您需要优先支持,可以通过 Paid Support Services 获取服务,并直接联系我们的付费支持管理团队。