Skip navigation
Hoang_Huynh
Currently Being Moderated

[JS] PageItem Export to PNG: exported image's dimesion is different from PageItem's

Sep 3, 2012 9:55 PM

Tags: #cs5 #script #frame #text #cs5.5 #export #png #javascript #indesign #images #scripting #cs6 #page #pageitem #item #textframe

When I try to export a rectangular text frame to PNG with JavaScript, the result PNG image may have bigger dimension than that of the text frame itself in InDesign. I believe this has something to do with the line height of the text.

 

Is there any Export option I can use to make the exported image's dimension the same as the text frame's?

 

Please see my attached pictures for an example:

 

  • The text frame in InDesign has a dimension of 400 x 80 (pixels)

frame.png

 

  • This is the exported PNG, which has a dimesion of 400 x 96 (pixels). Notice the padding at the top and bottom of the image.

real-dimension.png

 
Replies
  • Currently Being Moderated
    Sep 4, 2012 3:09 AM   in reply to Hoang_Huynh

    @Hoang_Huynh – at the moment you do the export, I think, you cannot do much against that. It's the way PNG export is working…
    And it's not only forced by the line height of the text, but could also be caused by effects like drop shadow etc.pp.

    After export, you could open the file in PhotoShop and trim the file to its writing pixels.

     

    You could ask that question at the PhotoShop Scripting forum.

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 5:14 AM   in reply to Hoang_Huynh

    Copy the page item to a page with the dimensions of the text frame and export the page.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 12:31 AM   in reply to Harbs.

    @Harbs – hm, with that approach you could get into trouble, if you are using the "baseline grid" feature in your paragraphs…

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 12:55 AM   in reply to Hoang_Huynh

    @Hoang_Huynh – did you try with PhotoShop to trim the size to the writing pixels?

     

    Your background in the PNG are white pixels? Or can you export with a transparent background, so it would be easy in PhotoShop to make a trim area for writing pixels only?

     

    The "padding" you see might  vary, if you choose other examples. So I would not bet on a fixed count of pixels to cut…

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 12:59 AM   in reply to Hoang_Huynh

    You'll get transparent pixels, if you choose "transparentBackground = true" in the PNG export preferences of InDesign CS6.
    This is a new feature in CS6 and not available in older versions.

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 2:02 AM   in reply to Hoang_Huynh

    Thanks, but I want to trim the PNG to the exact dimensions of the text frame.

    To get the exact dimensions of the text frame in relation to your exported PNG is hard, because the "offset" is not predictable.
    You could export the whole page and crop to the geometricBounds of the text frame…

     

    With a transparent background you could use PhotoShop to crop to the writing pixels.

    But this seems not the thing you want.

     

    You could use InDesign scripting in conjunction with PhotoShop scripting by using "BridgeTalk" (that's not scripting the "Bridge" app ;-)).

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 2:09 AM   in reply to Laubender

    The easy way would be the one Harbs suggested.

    Provided you do not use the baseline grid in your paragraph formatting…


    Duplicate the text frame to a new document using the geometric bounds of the text frame as page size and export from there.

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 2:18 AM   in reply to Laubender

    In CS6 you could define the "pngExportRange" with a page string definition. I did not try that, but I hope in that way you will not get any additional pixels.

     

    But if that will not work and you also get additional pixels with that method, you need to export to PDF (it's always the whole page), open the PDF in PhotoShop with some rendering options (color space and resolution) and save it from there as PNG. Use the export to web functionality there, which will minimize the file size in contrast to the "normal" save-as function.

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 2:27 AM   in reply to Laubender

    Hi Hoang_Huynh and Uwe,

     

    getting the TextFrame-bounds, exporting a page to pdf, creating a new doc in textframesize, import and position the pdf, export to png ...

     

    may be the complicated way ;-)

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 3:02 AM   in reply to Hoang_Huynh

    There seems to be some problem with aligning your text to the very top of the frame. You can see this if you add a black stroke to the text frame before exporting -- left, right, and bottom align perfectly, on the top you can see some additional padding. Presumably it has something to do with ascenders? font size? default line height?

     

    You can solve it by tricking ID. Create a new, empty rectangle the exact size of the text frame, copy text frame, paste "into" the new rectangle, and export this instead. This way, the new rectangle acts as a clipping mask, and the excess space will be cropped off.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 3:21 AM   in reply to [Jongware]

    Hmm -- moving any item "into" a rectangle is not a straightforward operation. Well, let's (ab)use "app.copy" and "app.paste" then, although, theoretically at least, it should be possible by manipulating the objects.

     

    This duplicates the selected item, removes its contents, sets stroke and fill to none, and then pastes the original selection into the new one. After exporting, the duplicated object automatically gets deleted so you end up with what you started with.

     

    (Warning! Tested only with CS4!)

     

    obj = app.activeDocument.selection[0];
    cliprect = obj.duplicate(undefined, [0,0]);
    cliprect.texts[0].remove();
    cliprect.strokeWeight = 0;
    cliprect.fillColor = app.activeDocument.swatches.item("None");
    cliprect.contentType = ContentType.UNASSIGNED;
    app.copy();
    app.select(cliprect);
    app.pasteInto();
    cliprect.exportFile(ExportFormat.PNG_FORMAT, File(new File("~/Desktop/test.png")));
    cliprect.remove();
    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 10:38 AM   in reply to Hoang_Huynh

    it cannot produce transparent PNGs

    Glad, it's working for you!

    About the not working transparency: did you test with InDesign CS6?

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 5, 2012 2:45 PM   in reply to [Jongware]

    Any ideas on how to do this without paste? I want to preserve the user's clipboard.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 5, 2012 5:00 PM   in reply to Justin Putney

    It occurs to me that I might paste the whatever's in the clipboard to a holding area beforehand and re-copy it once I'm done...I'm open to better ideas, though.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 7, 2012 1:47 AM   in reply to Justin Putney

    My suggestion will work without involving the clipboard.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 7, 2012 12:19 PM   in reply to Harbs.

    Thanks, Harbs. I also need transparency. Unfortunately, I've found that the visible bounds do not include effects (like drop shadow), but pageItem.exportFile does. For my purposes, I think I just need to be able to calculate how much padding is created based on the first line of text.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 13, 2012 12:00 AM   in reply to Hoang_Huynh

    You could calculate it, if you open the PNG (including transparency) in PhotoShop and get a selection of its "drawing" pixels. You then could have it's real dimensions including effects and crop the PNG accordingly. This would be possible by using some BridgeTalk and PhotoShop scripting.

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 6, 2012 4:40 PM   in reply to Justin Putney

    P.S. You can actually see the discrepancy when you select the text. The selection rectangle seems show were the "actual" top of the TextFrame is:

    Screen Shot 2012-11-06 at 4.38.34 PM.png

     

    Any ideas on how to get the bounds for that?

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 6, 2012 8:29 PM   in reply to Justin Putney

    I think I found it: you can check the ascent value of the first paragraph and adjust according to that value.  Nope

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points