大 SWT 图像打印不正确

发布于 2024-12-03 07:33:31 字数 3064 浏览 0 评论 0原文

嘿,SWT 大师,我遇到了一个非常奇怪的情况。问题是,我正在尝试在 Eclipse RCP 应用程序中打印甘特图。甘特图很长,有时也很高。其尺寸如下:高度=3008px(2个垂直页面),宽度>3008px。 20000 像素。我的打印区域可以容纳 1400x2000 像素。首先,我正在用我的图表创建图像(图像没问题,我可以单独保存它并查看,一切都在那里)。但是,为了将其打印在纸张上,我将其逐块打印(分别移动源 X 和 Y 位置)。这个算法在一段时间内运行良好,但现在发生了一些奇怪的事情:

当图表不够高并且可以容纳 1 个垂直页面时,则可以正常打印,但是当它是 2 个垂直页面时,则只打印第二个垂直页面第一个被遗漏了。没有任何错误,也没有任何东西可以帮助我。我想,可能是没有足够的堆内存,所以我为我的应用程序分配了 -Xmx1014m 空间,但这没有帮助。所以,我真的很迷失,找不到任何解决方案,甚至找不到这个问题的解释。我试图简单地通过 gc.drwImage(image, x, y) 打印图像,但也只打印了它的后半部分。我还在每次尝试打印图像后打印一些文本,并且打印了

负责打印图像的代码如下:

for (int verticalPageNumber = 0; verticalPageNumber <= pageCount.vGanttChartPagesCount; verticalPageNumber++) {
        // horizontal position needs to be reset to 0 before printing next bunch of horizontal pages

        int imgPieceSrcX = 0;
        for (int horizontalPageNumber = 0; horizontalPageNumber <= pageCount.hGanttChartPagesCount; horizontalPageNumber++) {

            // Calculate bounds for the next page
            final Rectangle printBounds = PrintingUtils.calculatePrintBounds(printerClientArea, scaleFactor, verticalPageNumber, horizontalPageNumber);

            if (shouldPrint(printer.getPrinterData(), currentPageNr)
                  && nextPageHasSomethingToPrint(imgPieceSrcX, imgPieceSrcY, totalGanttChartArea.width, totalGanttChartArea.height)) {
                printer.startPage();

                final Transform printerTransform = PrintingUtils.setUpTransform(printer, printerClientArea, scaleFactor, gc, printBounds);

                printHeader(gc, currentPageNr, printBounds);

                imgPieceSrcY = printBounds.y;

                final int imgPieceSrcHeight =
                      imgPieceSrcY + printBounds.height < ganttChartImage.getBounds().height ? printBounds.height : ganttChartImage.getBounds().height
                            - imgPieceSrcY;

                final int imgPieceSrcWidth =
                      imgPieceSrcX + printBounds.width < ganttChartImage.getBounds().width ? printBounds.width : ganttChartImage.getBounds().width
                            - imgPieceSrcX;

                if (imgPieceSrcHeight > 0 && imgPieceSrcWidth > 0) {
                    // gantt chart is printed as image, piece by piece
                    gc.drawImage(ganttChartImage,
                          imgPieceSrcX, imgPieceSrcY,
                          imgPieceSrcWidth, imgPieceSrcHeight,
                          printBounds.x, printBounds.y,
                          imgPieceSrcWidth, imgPieceSrcHeight); // destination width and height equal to source width and height to prevent
                    // stretching/shrinking of image
                    // move x and y to print next image piece

                    gc.drawText("Text " + currentPageNr, imgPieceSrcX, imgPieceSrcY);

                    imgPieceSrcX += printBounds.width;

                }

                currentPageNr++;

                printer.endPage();
                printerTransform.dispose();
            }
        }

提前致谢。

Hei, SWT Gurus, I have a pretty weird situation. The thing is, that I am trying to print gantt chart in my Eclipse RCP application. Gantt chart is quite long and sometimes high as well. Its dimensions are following: height=3008px (2 vertical pages), width > 20000px. My print area can fit something like 1400x2000 px. First of all, I am creating image out with my chart (image is OK, I can save it separately and see, that everything is there). However, in order to print it on the paper, I am printing it piece by piece (moving source X and Y positions respectively). This algorithm was working fine for some time, but now something strange happened:

When chart is not high enough and can fit on 1 vertical page, then it is printed normally, but when it is 2 vertical pages, then only second vertical page is printed and first one is left out. There are no errors, nor anything, that could help me. I thought, may be there is not enough heap memory, so I allocated -Xmx1014m space to my application, but it didn't helped. So, I am really lost, and can not find any solution or even an explanation to this problem. I was trying to simply print image by gc.drwImage(image, x, y), but is also printed me only the second half of it. I am also printing some text after every try of printing an image, and it is printed

The code, that is responsible for printing an image is following:

for (int verticalPageNumber = 0; verticalPageNumber <= pageCount.vGanttChartPagesCount; verticalPageNumber++) {
        // horizontal position needs to be reset to 0 before printing next bunch of horizontal pages

        int imgPieceSrcX = 0;
        for (int horizontalPageNumber = 0; horizontalPageNumber <= pageCount.hGanttChartPagesCount; horizontalPageNumber++) {

            // Calculate bounds for the next page
            final Rectangle printBounds = PrintingUtils.calculatePrintBounds(printerClientArea, scaleFactor, verticalPageNumber, horizontalPageNumber);

            if (shouldPrint(printer.getPrinterData(), currentPageNr)
                  && nextPageHasSomethingToPrint(imgPieceSrcX, imgPieceSrcY, totalGanttChartArea.width, totalGanttChartArea.height)) {
                printer.startPage();

                final Transform printerTransform = PrintingUtils.setUpTransform(printer, printerClientArea, scaleFactor, gc, printBounds);

                printHeader(gc, currentPageNr, printBounds);

                imgPieceSrcY = printBounds.y;

                final int imgPieceSrcHeight =
                      imgPieceSrcY + printBounds.height < ganttChartImage.getBounds().height ? printBounds.height : ganttChartImage.getBounds().height
                            - imgPieceSrcY;

                final int imgPieceSrcWidth =
                      imgPieceSrcX + printBounds.width < ganttChartImage.getBounds().width ? printBounds.width : ganttChartImage.getBounds().width
                            - imgPieceSrcX;

                if (imgPieceSrcHeight > 0 && imgPieceSrcWidth > 0) {
                    // gantt chart is printed as image, piece by piece
                    gc.drawImage(ganttChartImage,
                          imgPieceSrcX, imgPieceSrcY,
                          imgPieceSrcWidth, imgPieceSrcHeight,
                          printBounds.x, printBounds.y,
                          imgPieceSrcWidth, imgPieceSrcHeight); // destination width and height equal to source width and height to prevent
                    // stretching/shrinking of image
                    // move x and y to print next image piece

                    gc.drawText("Text " + currentPageNr, imgPieceSrcX, imgPieceSrcY);

                    imgPieceSrcX += printBounds.width;

                }

                currentPageNr++;

                printer.endPage();
                printerTransform.dispose();
            }
        }

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文