Again on JEditorPane printing, please help me

Hi everybody,
first of all excuse me if I write again about an already discussed topic,
but I still haven't found a good solution for it.
I have a JEditorPane that I use to render HTML documents, and I need
to be able to print the contents.
I then created my PrintableEditorPane class that implements Printable
add the following print() method:
public int print(Graphics g, PageFormat fmt, int index) throws PrinterException
        Graphics2D g2d = (Graphics2D) g;
        // Set default foreground color to black
        g2d.setColor(Color.black);
        // For faster printing, turn off double buffering
        RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
        // Get size of document
        Dimension d = getSize();
        double panelWidth = d.width; // Width in pixels
        double panelHeight = d.height; // Height in pixels
        double pageWidth = fmt.getImageableWidth(); // Width of printer page
        double pageHeight = fmt.getImageableHeight(); // Height of printer page
        int totalNumPages = (int) Math.ceil(panelHeight / pageHeight);
        // Make sure not print empty pages
        if (index >= totalNumPages)
            return Printable.NO_SUCH_PAGE;
        // Shift Graphic to line up with beginning of print-imageable region
        g2d.translate(fmt.getImageableX(), fmt.getImageableY());
        // Shift Graphic to line up with beginning of next page to print
        g2d.translate(0f, -index * pageHeight);
        // Repaint the page for printing
        paint(g2d);
        return Printable.PAGE_EXISTS;
}Everything works quite fine, with the exception that if text spans on
multiple pages, it can happen that the bottom part of the last line in
a page is printed in the next one.
I found that this topic occurs rather frequently in the forum, so I changed
the code with the following one, taken from the Internet:
public int print(Graphics g, PageFormat fmt, int index) throws PrinterException
        Graphics2D g2d = (Graphics2D) g;
        // Set default foreground color to black
        g2d.setColor(Color.black);
        // For faster printing, turn off double buffering
        RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
        // Shift Graphic to line up with beginning of print-imageable region
        g2d.translate(fmt.getImageableX(), fmt.getImageableY());
        View rootView = getUI().getRootView(this);
        if (index > currentPage)
            currentPage = index;
            pageStartY += pageEndY;
            pageEndY = g2d.getClipBounds().getHeight();
        g2d.translate(g2d.getClipBounds().getX(), g2d.getClipBounds().getY());
        Rectangle allocation = new Rectangle(0, (int) -pageStartY, getWidth(), getHeight());
        if (printView(g2d, allocation, rootView))
            return Printable.PAGE_EXISTS;
        else          
            currentPage = -1;
            pageStartY = 0.0;
            pageEndY = 0.0;
            return Printable.NO_SUCH_PAGE;
protected boolean printView(Graphics2D g2d, Shape alloc, View view)
        boolean pageExists = false;
        Rectangle clip = g2d.getClipBounds();
        Shape childAlloc;
        View childView;
        if (view.getViewCount() > 0)
            for (int i = 0; i < view.getViewCount(); i++)
                childAlloc = view.getChildAllocation(i, alloc);
                if (childAlloc != null)
                    childView = view.getView(i);
                    if (printView(g2d, childAlloc, childView))
                        pageExists = true;
        else
//  I
            if (alloc.getBounds().getMaxY() >= clip.getY())
                pageExists = true;
//  II
                if ((alloc.getBounds().getHeight() > clip.getHeight()) && (alloc.intersects(clip)))
                    System.out.println("Calling view.paint()... (1)");
                    view.paint(g2d, alloc);
                else
//  III
                    if (alloc.getBounds().getY() >= clip.getY())
                        if (alloc.getBounds().getMaxY() <= clip.getMaxY())
                            System.out.println("Calling view.paint()... (2)");
                            System.out.println(view.getClass().getName());
                            System.out.println(view.getElement().getName());
                            view.paint(g2d, alloc);
                        else
//  IV
                            if (alloc.getBounds().getY() < pageEndY)
                                pageEndY = alloc.getBounds().getY();
        return pageExists;
} Now pagination is handled correctly, but a new problem arises: table
borders are not printed at all, and this topic too is not new in the forum.
So I made some investigations that you can see from the println() in the
code and I found out that all the views are of class InlineView, that is returned
from HTMLEditorKit when an element with name "content" is found.
Now the real questions: why don't I get the real views hierarchy of the HTML
document, and how can I solve my problem?
Many thanks to anyone who will like to help me.
Matteo.

Hello, and thanks for all the great help so far in this thread! I have implemented the above code, and it seems to work great, except for one thing. It always prints the first page twice. My print method is exactly as listed above. Here is my printView method:
protected boolean printView(Graphics2D g2d, Shape alloc, View view)
               boolean pageExists = false;
               Rectangle clip = g2d.getClipBounds();
               Shape childAlloc;
               View childView;
               //System.out.println("View name = " + view.getElement().getName());
               if (view.getViewCount() > 0 && !view.getElement().getName().equalsIgnoreCase("td"))
                    for (int i = 0; i < view.getViewCount(); i++)
                         childAlloc = view.getChildAllocation(i, alloc);
                         if (childAlloc != null)
                              childView = view.getView(i);
                              if (printView(g2d, childAlloc, childView))
                                   pageExists = true;
               else
//          I
                    if (alloc.getBounds().getMaxY() >= clip.getY())
                         pageExists = true;
//          II
                         if ((alloc.getBounds().getHeight() > clip.getHeight()) && (alloc.intersects(clip)))
                         //     System.out.println("Calling view.paint()... (1)");
                              view.paint(g2d, alloc);
                         else
//          III
                              if (alloc.getBounds().getY() >= clip.getY())
                                   if (alloc.getBounds().getMaxY() <= clip.getMaxY())
                              //          System.out.println("Calling view.paint()... (2)");
                              //          System.out.println(view.getClass().getName());
                              //          System.out.println(view.getElement().getName());
                                        view.paint(g2d, alloc);
                                   else
//          IV
                                        if (alloc.getBounds().getY() < pageEndY)
                                             pageEndY = alloc.getBounds().getY();
               return pageExists;
     }Can anyone see why it would print the first page twice every time?
Thanks for you help!

Similar Messages

  • So my computer broke some how and now it wont even turn on i just such without want to know if i can still have all my old songs and buying it all over again or something for please help me

    so my computer broke some how and now it wont even turn on i just want to know if i can still have all my old songs and such without buying it all over again or something so please help me oh also my ipod broke so i dont have anything that has my old songs except for the itunes ppl who know all of my songs

    You buy one download and one download only,  it is your responsibility to backup your music.
    it has always been very basic to always maintain a backup copy of your computer.  Use your backup copy to put everything back.
    If for some reason you have failed to maintain a backup ( not good), then you can transfer itunes purchases from an ipod:  File>Transfer Purchases

  • TS1717 I got this message every time i connect my iphone to itunes.. said 'iTunes was unable to load provider data from Sync Services. Reconnect or try again later.' so please help me to solve out this problem..

    I got this message every time i connect my iphone to itunes.. said 'iTunes was unable to load provider data from Sync Services. Reconnect or try again later.' so please help me to solve out this problem..

    In the course of your troubleshooting to date, have you also worked through the following document?
    iTunes for Windows: "Unable to load data class" or "Unable to load provider data" sync services alert

  • I can not connect my i phon with my pc it shows athorised computer when i am trying to do authorised process the process done but again i connect my phon again show this errore please help me to solve my prob....

    i can not connect my i phon with my pc it shows athorised computer when i am trying to do authorised process the process done but again i connect my phon again show this errore please help me to solve my prob....

    I have clicked on the  connect button, but it brings up the same There is a problem connecting,  and I have also tried SMB://
    i tried pinging the ip address with in termial and it times out.  
    From my windows PC and I can access my MacBook Air just fine,

  • My iPhones iTunes Radio doesn't work it says "iTunes radio is temporarily unavailable try again later" some one please help I really want to listen to the radio. 

    My iPhones iTunes Radio doesn't work it says "iTunes radio is temporarily unavailable try again later" some one please help I really want to listen to the radio.  please help my life depends on anyone who fixes the problem. 

    Your life depends on it? See this discussion: https://discussions.apple.com/thread/5345664

  • Printing, please help!

    I am still having so much trouble printing to my HP Photosmart B209a printer. I have tried everything, and nothing is working. Whenever I print the colors do not match up at all. I am using a Mac OSx and again printing to HP Photosmart B209a.
    Please help!

    I have tried that .. and still no luck! The colors still look significantly different.

  • Canon IP7250 - Color won't print - Please help

    I have a Canon IP7250, two day ago, my printer showed me error C000. I resolved this error, but now my printer won't printing in colour. Black working properly. I tried many times to cleaning a toner in mainteace. When I go to "print head alignment" then shows error 2500. ? In nozzle check black colour is excellent but no other colors on paper. 
    Please, help me. 
    Thanks.

    Again, the username/password, would either be your logon username/password or your "root" username/password if you have enabled it.
    Another option would be to update your CUPS software. Multi-functional printers are usually "tricky" to get full-featured drivers for. It's possible that a newer version for this particular printer may exist with an update.
    Still another option (and possibly a better one) would be to download and install the latest Gutenprint printer drivers. These were formally known as Gimp-Print drivers. (ref: http://gimp-print.sourceforge.net/MacOSX.php3)

  • When trying to save a bookmark to a specific pre-established folder the window disappears and will not allow me to make a selection. If I hover in the area it will flash on the screen and disappear again, it's annoying please help!

    When I attempt to save a new bookmark and want to save it in a folder the window that typically appears just below the address bar will not stay on the screen. It flashes on the screen and as soon as I move my cursor over it, it disappears. It will not allow me to make a selection of any kind. If I randomly hover over the general area the window will flash back in view but disappear again just as quickly. If I then show all my bookmarks, the bookmark was saved and I have to move it to the folder I actually want it in. Just a few extra steps, but still very annoying. Please help!

    DID YOU FIND A SOLUTION???I HAVE TH ASAME THING AS WELL

  • I didnt konw how to make a new account for my iTues card so i did it with a diffrent email but that email doesnt exist and when i tried to make it on a real email i just sai invalid code check the code and try again and can u please help me

    please help me it is a card of 25 dollars and i dont want it to go to waist i by mistake thought you used a frake email so i made one up i thought i was going to have a new one but then it said it was going to send a verifaction to my email so then i tried to use a real email and put in the code but it just kept on saying invalid code. check the code and try again. and i dont know what to do can u please please help me it was a gift and i really dont want it to go to waist please i need help

    for #1
    Frequently asked questions about Apple ID - Apple Support
    I have multiple Apple IDs. Is there a way for me to merge them into a single Apple ID?
    Apple IDs cannot be merged. You should use your preferred Apple ID from now on, but you can still access your purchased items such as music, movies, or software using your other Apple IDs.
    If you are wondering how using multiple Apple IDs relate to iCloud, see Apple IDs and iCloud.
    for #2
    Apple does not accept unsolicited ideas see Apple - Legal - Unsolicited Idea Submission Policy

  • My Photosmart Printer is not printing, Please Help.

    I need help, my printer, acts like it's going to print, it goes through the processes as it sounds like it's going to Print, but the paper comes out blank, and I just put new ink in it. Can someone be so kind to please help.
    Thanks

    Hi MsLew,
    Is the C7280 able to make a perfect copy of a document if you lay something on the glass of the printer and select color copy?
    If so, reset the printing system (don't pay attention to the OS listed in the document) on the Mac and then install this HP driver package for Mac: HP Printer Drivers v3.0 for OS X. Restart the Mac, add the printer back into System Preferences.
    Try printing at this time and let me know what happens.
    JERENDS
    I work on behalf of HP
    Please click “Accept as Solution” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos Thumbs Up" to the left of the reply button to say “Thanks” for helping!

  • Check printing ,Please help : Urgent!!!!!!!

    One of the user printed wrong information on a check.She wants to void that check and reprint a new one.The information also came on next check,But the information of second check is not the check lot ,I mean in the system,where as first check information is in the check lot.Which T. codes do we use and how do i deal with second check..Do i need to do anything in FCHI(make changes to next available check number).Please help me as she needs it immediately.
    First check : 1469
    second check: 1470
    1470 information is not in the system.
    I will assign you points.
    Thanks

    hi first check in FCHI - as to what your range is. post your range here. Then go to FCh1 and view the check info. maybe you have the test out put checked so the first check is voiding automatically. Aslo check in FCHN
    Points will be appreciated
    Regards,
    Zaid

  • I cannot get my iphone 4 to print yo my brand new photosmart 6510 printer please help

    please help me get it to print. I've tried to search for the printer serveral time and it can't find it. All of my other computer on working wirelessly
    Thanks

    The steps given above would be the correct way to utlize Air Print.
    AirPrint makes printing your email, photos, web pages, and other documents easy with no software to download, no drivers to install, and no cables to connect. Simply hit 'print' and continue surfing the web without a single interruption.
    A quick workaround, for your issue would be to download HP ePrint Home & Biz app from the app store which will help with finding printer on network and give you diffrent print options.
    Print from virtually anywhere with HP ePrint Home & Biz: print wirelessly to supported HP printers on a local Wi-Fi/other network; print to any registered HP ePrint-enabled printer in the world.
    1. On your mobile device, install and register with iTunes if you have not done so already.
    2. Open the Apps Store .
    3. In the Apps Store Search feature, type HP ePrint Home & Biz , and then tap Search .
    4. If prompted, provide your iTunes account information, and then tap OK .
    If I have solved your issue, please feel free to provide kudos and make sure you mark this thread as solution provided!
    Although I work for HP, my posts and replies are my own opinion and not those of HP. .....

  • I have windows xp and I keep getting the iTunes store is temporarily unavailable.  Please try again later. Some please help?

    I've tried everything and still can't get my ITunes to work
    Please help
    Thanks

    Hello dproctor4,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at:
    "We could not complete your iTunes Store request. The iTunes Store is temporarily unavailable. Please try again later."
    Can't connect to the iTunes Store
    http://support.apple.com/kb/TS1368
    Best of luck,
    Mario

  • I upgraded some of my apps on my ipod touch, and one of them, my candy crush app is messed up, couldn't connect to facebook, tried re-installing it, same thing. now i have to start from the begining again in candy crush. please help

    I upraded some of my apps on my ipod touch, and one of them, my candy crush app is messed up, couldn't connect to facebook. tried re-installing it, same thing happened. now i have to start from the begining again, which really *****!! please help!!

    There is nothing wrong with the OS update.
    Delete ALL your email accounts.
    Restart Playbook
    Put the accounts back and ensure they are all set with PUSH ON.  Manual (push off) will burn battery.
    Similarly delete your wifi connections and add back when required. 
    Turn off wifi is not connected to wifi. 
    Any "hunting for connection" in email or wifi will burn up battery.

  • I can't connect OS X Yosemite 10.10.2 version with Canon LBP2900 Printer, Please help how to connect...

    Hi.... Apple Support Communities...
    I used Apple MacBook Air 2014 (13 inch) series with OS X Yosemite 10.10.2 version...
    And I have a printer of Canon LBP2900...
    My problem is that, I can't connect my Canon LBP2900 printer with my new OS X Yosemite 10.10.2 version.
    My previous OS X Mavericks is working well with my Canon LBP2900 printer...
    But when I upgrade it to OS X Yosemite 10.10.2 version, my printer is not working...
    Anybody please help me how can i connect OS X Yosemite 10.10.2 version with my Canon LBP2900 printer....
    Thanking you.... Your suggestion in this regards is highly appriciate...

    Your first step would be to upgrade to Snow Leopard (10.6). To do that, you would need to purchase the Snow Leopard DVD. Following that you could determine how much further you could upgrade.
    http://store.apple.com/us/product/MC573/mac-os-x-106-snow-leopard

Maybe you are looking for

  • ADF Popup for adding a new Row in  table

    Hi All, I have VO which is rendering as table on my jsff.I created a button of createInsert to add a new row and after commit it saves the values in Table in DB. Works abst fine. Now i want to change this feature to that when a New Row should be Adde

  • Should I upgrade to CS5 on 3-year old pc?

    HP Pavilion d5000t desktop pc. I currently have CS4 Prod Premium on the 64-bit Windows 7. 8 GB Ram DDR2 (max) Core 2 Quad Q9450 2.66 GHz processor. Graphics card GeForce 9600 GS. I work with HDV footage I have no plans to purchase a new computer any

  • How to retrive question name in msg in procedure by writing custom coding

    how to retrive question name in message in procedure by writing custom coding.........

  • Deploying an .ear failed miserably.

    My log has entries like this from my attempt to deploy a perfectly healthy .ear file ####<May 14, 2001 8:43:36 PM PDT> <Error> <Management> <ann> <petstoreServer112> <Application Manager Thread> <> <> <141004> <IOException opening application petstor

  • Failed to extract data by OracleXMLQuery

    Hi, Sir: When I used XDK to extract data from Oracle8i. I received an error message. Here is part of my codes: String tblName = "sp_collect_open_master"; String optQry = "select count(*) from sp_collect_reason";           try{                DBConn d