Results 1 to 14 of 14
  1. #1
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default print the full content of JScrollpane

    Hi,

    I want to print JPanel (print, export to PDF, etc.) which is inside jScrollpane. JPanel is kinda large.
    I need to scale it to fit A4 width and then print it (probably many pages will be needed).

    I tried to use iText to export to Pdf, but it exports only the first page.

    I tried to overwrite print method for JFrame(which contains JScrollpane), smth like

    public int print(Graphics g, PageFormat pageFormat,
    int pageIndex) throws PrinterException {
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.black);
    int fontHeight=g2.getFontMetrics().getHeight();
    int fontDesent=g2.getFontMetrics().getDescent();

    ...
    double pageWidth =
    pageFormat.getImageableWidth();
    double rectWidth = (double) drawRect.getWidth();
    ...

    double scale = 1;
    if (rectWidth >= pageWidth) {
    scale = pageWidth / rectWidth;
    logger.debug("scale " + scale);
    }

    double rectHeight = drawRect.getHeight()*scale;
    ...

    double rectHeightPage = pageFormat.getImageableHeight() - 25;
    ...

    int totalNumPages=
    (int)Math.ceil((
    (double)rectHeight/rectHeightPage));
    logger.debug("totalNumPages " + totalNumPages);

    if(pageIndex>=totalNumPages) {
    return NO_SUCH_PAGE;
    }

    g2.translate(pageFormat.getImageableX(),
    pageFormat.getImageableY());


    if (pageIndex + 1 == totalNumPages) {


    ...
    }
    //else clip to the entire area available.
    else{



    g2.setClip(0,
    (int)(rectHeightPage*pageIndex),
    (int) Math.ceil(rectWidth),
    (int) Math.ceil(
    rectHeightPage
    ));
    ...


    }

    g2.scale(scale,scale);
    drawRect.paint(g2);
    g2.scale(1/scale,1/scale);


    return Printable.PAGE_EXISTS;
    }



    Scaling works, but again - it prints only the first page, and the rest of pages are empty.

    What shall I do? Please help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    Can you advance the contents of the JScrollPane as each new page is requested to be printed? What is the value of pageIndex?
    I haven't worked with printing in a while.

  3. #3
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    pageIndex values are from 0 till totalNumPages.
    setClip selects the right area (according to logger - Set clip x: 0; y: 2450; width:1165; height: 817, etc. - it changes the y coordinate as it should do).
    But anyway other pages are empty ;(


    And I am a total newbie, working with Swing for the first time actually....

  4. #4
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    OK, I found a mistake.

    g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

    I changed to g2.translate(pageFormat.getImageableX(), -rectHeightPage*pageIndex); and it works.

    But it separates pages by pixels (meaning if I have some text on the picture, the letters are split across pages).

    Any ideas how to solve it?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    the letters are split across pages). Any ideas how to solve it?
    Scan the image and set the page length to be before the letters that are split.

  6. #6
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    And how to do the scanning?

    P.S. Images can be of different size; multiple textareas on them can be of different size and on different positions.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    Where do you want the page break to be?
    Do you know how many pixels fit on a page vertically?
    Start at that y location and move up until you find a horizontal row of pixels that do not have and thing that you don't want to split across pages. Like a row of white pixels.

  8. #8
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    There can be vertical lines and the background color is set.
    But I do not want to make breaks on text, horizontal lines or any other graphics.

    And since I am a total newbie, I do not know how to scan JPanel Graphics to get pixel array or smth and how to get to know if there is text/smth.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    When you build the page layout, can you determine where the components are being placed?
    If you can, then make sure they are not crossing a page boundary.

  10. #10
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    It is generated dynamically...
    Meaning I check the y coordinate of the last drawn object;
    and then smth like
    lastY = lastY + newObject.getHeight + someExtraSpace.

    So, I can't know the coordinates of objects on the Graphics (objects - polygons, lines, texts - everything of different size).

  11. #11
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    And btw pageLayout is null (otherwise I couldn't position textAreas properly).

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    If you are positioning the components (layout = null) then you should have 100% control over where the component is positioned.

  13. #13
    Mion is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: print the full content of JScrollpane

    Well, the positioning is kinda relative - according to other objects positions....

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: print the full content of JScrollpane

    Yes, of course. Your problem is to keep components from spanning a page break.
    Since you have 100% control of where you put each component and you know where the page break is, you should be able to detect if a component will span the break.

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2011, 08:52 AM
  2. Moving whole content of JScrollPane by mousedrag
    By Taiko in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 08:44 PM
  3. How to print the content of List
    By cassysumandak in forum New To Java
    Replies: 4
    Last Post: 10-01-2009, 10:36 AM
  4. Replies: 2
    Last Post: 05-30-2009, 10:42 AM
  5. Java code to connect printer to print content
    By wendyz in forum Advanced Java
    Replies: 3
    Last Post: 04-30-2009, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •