itextpdf打印A5的问题

使用A5打印的时候,再生成pdf是没有问题的。下面做了一个测试,在打印机中,使用A5的纸张横向放入,因为是家用打印机,A5与A4是同一个口,因此只能这么放。

        使用itextpdf生成pdf,在浏览器中预览pdf是正常的。

if (i%2 ==0){
                        pd.put(PdfName.ROTATE, new PdfNumber(0));
                    } else{
                        pd.put(PdfName.ROTATE, new PdfNumber(90));
                    }

        通过浏览器自带的打印预览,貌似也米有问题

        但实际打印出来却有问题,可以看到打印的不全。看来问题就是因为打印机将你放入的A5的纸张当成了A4,看似解决方案是将里面的内容进行旋转,但是纸张不旋转问题岂不是解决了。如何实现却成了问题。

        尝试了很多种方法,都没有生效,网上的案例太少了。

 private void handlePrintUdfDirection(HttpServletResponse response, Integer printDirection,
                                      ByteArrayOutputStream baos, String pageSize) throws IOException, DocumentException {
            try {
                // 创建 PdfReader 对象读取输入 PDF 文件
                byte[] content = baos.toByteArray();
                PdfReader reader = new PdfReader(content);

                // 创建一个新的 ByteArrayOutputStream 来保存旋转后的内容
                ByteArrayOutputStream modifiedBaos = new ByteArrayOutputStream();

                // 创建一个新的 PdfWriter 和 Document 对象
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, modifiedBaos);
                document.open();

                int numberOfPages = reader.getNumberOfPages();

                // 遍历每一页
                for (int i = 1; i <= numberOfPages; i++) {
                    // 获取当前页面的内容
                    PdfImportedPage importedPage = writer.getImportedPage(reader, i);

                    // 获取 PdfContentByte 对象
                    PdfContentByte canvas = writer.getDirectContent();

                    // 获取页面尺寸
                    float width = reader.getPageSize(i).getWidth();
                    float height = reader.getPageSize(i).getHeight();

                    // 应用旋转变换
                    canvas.saveState();
                    canvas.concatCTM(1, 0, 0, 1, 0, 0 );
//                    canvas.addTemplate(importedPage, -width, 0);
                    canvas.addTemplate(importedPage, 0, height+150);
                    canvas.restoreState();

                    // 添加新的页面到文档中
                    document.newPage();
                }

                // 关闭 PdfWriter 和 Document
                document.close();
                writer.close();
                reader.close();

                // 获取旋转后的 PDF 内容
                byte[] rotatedPdfContent = modifiedBaos.toByteArray();

                // 设置响应头
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition", "attachment; filename=rotated_output.pdf");

                // 获取响应的 OutputStream 并写入旋转后的 PDF 内容
                OutputStream out = response.getOutputStream();
                out.write(rotatedPdfContent);
                out.flush();
                out.close();
            } catch (DocumentException | IOException e) {
                e.printStackTrace();
            }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值