Jump to content

rotating graphic resource based on dimensions??


allison

Recommended Posts

Is there a way to automatically rotate a graphic resource based on it's width or height?

 

I have a directory of PDF resources that I'm using. Some will be portrait orientation, some landscape. Can a rule detect the orientation of the resource and rotate it appropriately in the variable graphic box? (i.e. If the height is greater than the width, rotate the graphic 90 degrees)

 

OR, can I have a rule that detects the orientation and puts the graphic in Box A (portrait shape) or Box B (landscape shape) according to which it would fit best in?

 

I'm under the impression this is possible with FP6.0 which I just installed, but I can't understand the documentation enough to figure out how to write the rule.

 

Using: FP 6.0 / PC

Link to comment
Share on other sites

You can use Text Measurement with an inline graphic to determine the graphic's dimensions, and then put the graphic into the appropriate box. I would use a function like this, in the JavaScript Globals:

function IsResourceLandscape(res)
{
 if (!res instanceof FusionProResource)
   throw "Not a resource: " + res;

 if (!res.exists)
   throw "Resource not found: " + res;

 var TM = new FusionProTextMeasure;
 TM.useTags = true;

 var err=TM.CalculateTextExtent(res.content);
 if (err)
   throw "Error measuring resource: " + res + ": " + TM.messages;

 Print(res + ": Width=" + TM.textWidth + ", Depth=" + TM.textHeight);
 return TM.textWidth > TM.textHeight;
}

Then you can call the function in a graphic rule for the landscape box, like so:

var MyResource = Resource("MyResourceName"); // base this on a field or some other per-record data
if (IsResourceLandscape(MyResource))
 return MyResource;
//else
return NullResource();

For the portrait box, just change the second line to:

if ([color=Red]![/color]IsResourceLandscape(MyResource))

Link to comment
Share on other sites

  • 3 months later...

Sorry, my previous post was in response to the original poster's secondary question:

OR, can I have a rule that detects the orientation and puts the graphic in Box A (portrait shape) or Box B (landscape shape) according to which it would fit best in?

Now, regarding this question:

What if the box orientation is fixed, and you want to rotate the graphic resource to orient to the box?

In this case, I would still use two frames, but rotate one of them with the Frame Properties palette. Ideally, both frames would exactly overlap each other, but the landscape box would be, say, 1 inch wide by 2 inches deep with zero rotation, and the portrait box would be, say, 2 inches wide by 1 inch deep and rotated 90 degrees. So any graphic returned by the rule for the portrait box would be rotated by the frame, while any graphic returned by the rule for the landscape box would be unrotated. Does that make sense?

Link to comment
Share on other sites

Hi Dan, thanks for the reply. It makes perfect sense but I'm not sure how it will work in this case. I'm populating a sheet of 6-up 4x6 graphic holes, and the user can pick or upload any 6 graphics or images, horizontal or vertical. All 6 print on a single sheet. Thanks in advance for your help.

 

Your answer does help me with another project for greeting cards, though. Thank you, and Happy Friday.

Link to comment
Share on other sites

  • 2 years later...

Dan,

 

I have a similar problem as this one with having to decide whether a graphic is landscape or portrait oriented. However the "graphic" in question is not a single .jpg or .tif graphic, it is a two page pdf file. One of our clients purposely creates mailing postcard files that have the front of the card in a vertical format while the back of the card is horizontal oriented. i.e. the front of the card is a 4"x6" page while the back of the card is 6"x4" page. These cards are distilled in this manner so no pages are rotated. They do this to according to instructions from "their clients" to be able to read the fronts of the cards in the same fashion as the backs of the cards on the website.

 

When FusionPro loads this pdf file it appears to only use the first page as the setting for the "entire document" regardless of which page is actually loaded using the "pagenumber" setting. If I read in and use only page 2, the 6"x4" side, FusionPro believes that this page too is still a 4"x6" page instead of the actual dimensions. Is this something that is an Acrobat issue and not really FusionPro (in which case I'm back to square one with this problem) or does this have something similar to do with the fact that FusionPro will always "un-rotate" any page(s) that have been rotated in Acrobat? BTW - I'm still a bit confused about how FusionPro knows that one too.

 

When I used your coding in this thread it worked and chose the proper graphic box based on the first page of the pdf, but when compiled using the second page it still used the dimension of the first page to choose the box orientation.

 

Is there a way for FusionPro to simply use the current page as the "whole document" basing all entries and rules on that page while completely disregarding the rest of the document settings?

.

Link to comment
Share on other sites

Is there a way for FusionPro to simply use the current page as the "whole document" basing all entries and rules on that page while completely disregarding the rest of the document settings?

No. For the purposes of Text Measurement, FusionPro calculates the size of a PDF graphic based on the first page. Other than using two separate PDF graphics, I don't know of any way around this. Nor would I expect any particular changes to the underlying text measurement capabilities, which, quite frankly, weren't really designed for the use case in this thread.

Is this something that is an Acrobat issue and not really FusionPro (in which case I'm back to square one with this problem) or does this have something similar to do with the fact that FusionPro will always "un-rotate" any page(s) that have been rotated in Acrobat? BTW - I'm still a bit confused about how FusionPro knows that one too.
No, this is a composition-time issue which has nothing to do with Acrobat. The calculations regarding rotated pages in the background (template) PDF happen in code specific to the Acrobat plug-in, which sets up the parameters for composition, not in the actual composition code where things like graphic scaling and text measurement happen. Exactly how FusionPro "knows" that a page is rotated is via a call to the Acrobat SDK, but further details about the inner workings of the plug-in get into the "no user servicable parts" territory.
Link to comment
Share on other sites

  • 7 months later...

Hello,

 

I have another wrench to through into this mix. Don't know if this is a bug or not, or even if it belongs in this thread...but this is where I received the original answer as to how to proceed with a pdf file of unknown orientation.

 

The situation...I have a template to create a 7x5 mailing postcard with a variable background provided as a 2 page PDF file.

 

My problem...I don't know if the file will be supplied as a 7x5 - landscape card, or a 5x7 - portrait card.

 

I used the following code in OnRecordStart

function IsResourceLandscape(res)
{
 if (!res instanceof FusionProResource)
   throw "Not a resource: " + res;

 if (!res.exists)
   throw "Resource not found: " + res;

 var TM = new FusionProTextMeasure;
 TM.useTags = true;

 var err=TM.CalculateTextExtent(res.content);
 if (err)
   throw "Error measuring resource: " + res + ": " + TM.messages;

 //Print(res + ": Width=" + TM.textWidth + ", Depth=" + TM.textHeight);
 return TM.textWidth > TM.textHeight;
}


var r = new FusionProResource(Rule("shellName"), "graphic", true);

if (IsResourceLandscape(r))  {
 FindGraphicFrame("Portrait_2").suppress = true;
 FindGraphicFrame("Portrait_3").suppress = true;
}
else  {
 FindGraphicFrame("Landscape_2").suppress = true;
 FindGraphicFrame("Landscape_3").suppress = true;
}

The rule "shellName" simply defines location and name of the uploaded pdf file that is listed as a variable in the data file. The OnRecordStart rule determines what orientation the uploaded card is and turns off the proper boxes when it needs to.

 

When I process my file without any imposition, everything come out fine. The orientation of the uploaded pdf file is properly determined and the output mail cards are rotated and processed correctly.

 

HOWEVER...if I put a simple 4-up imposition, then the process is all gummed up and FusionPro states that it cannot find the input resource.

Preprocessing input record #1
uncaught exception: Error measuring resource: Resource("..\\intermediateDocuments\\7X5 Postcards_16638.PDF"): Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Preprocessing input record #2
uncaught exception: Error measuring resource: Resource("..\\intermediateDocuments\\7X5 Postcards_16638.PDF"): Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Graphic is not found or is not processsed properly: \\Graphicsnt\MISXfer\to_DavidSweet\FireWire 58600\BrandMuscle\NewBalance\NEW-7X5-PCH\intermediateDocuments\7X5 Postcards_16638.PDF.
Check for file existence and that it is of the proper format.
Preprocessing input record #3...and so on to the end of the file.

Why would imposition gumm up the works? Is this a bug? Is this something in the "pre-processing" routine that FusionPro does? I thought that part was a simple counting of the records to determine the imposition break-down and didn't have anything to do with the actual template processing until the second run?

 

Again is this a bug?!? Or have I missed some step?

 

I also tried putting this rule in OnJobStart (where I think it really should be) and tried to determine the orientation of the pdf file only one time instead of repeating it needlessly for every record using up valuable processing time. I was hoping that OnJobStart rules would be processed before the "pre-processing" routines of counting records would be done. However FusionPro would not recognize the rule in that location and could not determine the orientation at all.

Link to comment
Share on other sites

Why would imposition gumm up the works? Is this a bug? Is this something in the "pre-processing" routine that FusionPro does? I thought that part was a simple counting of the records to determine the imposition break-down and didn't have anything to do with the actual template processing until the second run?

Well, yes, it is just trying to count records at preprocessing time. But in order to do that, it needs to invoke OnJobStart and OnRecordStart, as you may be skipping or repeating records based on logic in those rules. And the search path for finding graphics isn't completely set up at preprocessing time. So this isn't really a bug so much as an edge case.

 

However, I'll bet that it did create the output, in spite of the error messages at preprocessing time.

 

At any rate, to prevent it from doing the measurement at preprocessing time, you can just add this at the top of OnRecordStart (or at least after any code which does affect the record count, but before the measurement and frame-specific stuff):

if (FusionPro.Composition.inPreprocessing)
   return;

Link to comment
Share on other sites

Thanks for a quick reply. Yes, it did create an output even with those error/warning labels in the message file. I have two graphic frames on top on one another for either condition - one being rotated 90 degrees while the other is not. Since neither of the frames were turned off via the OnRecordStart rule, the card was super-imposed on top of itself with one image being rotated 90 degrees.

 

I tried your solution and at first it did not work. I just added the command like this...

function IsResourceLandscape(res)
{
 if (!res instanceof FusionProResource)
   throw "Not a resource: " + res;

 if (!res.exists)
   throw "Resource not found: " + res;

 var TM = new FusionProTextMeasure;
 TM.useTags = true;

 var err=TM.CalculateTextExtent(res.content);
 if (err)
   throw "Error measuring resource: " + res + ": " + TM.messages;

 //Print(res + ": Width=" + TM.textWidth + ", Depth=" + TM.textHeight);
 return TM.textWidth > TM.textHeight;
}

if (FusionPro.Composition.inPreprocessing)
   return;

var r = new FusionProResource(Rule("shellName"), "graphic", true);
if (IsResourceLandscape(r))  {
   FindGraphicFrame("Portrait_2").suppress = true;
   FindGraphicFrame("Portrait_3").suppress = true;
}
else  {
   FindGraphicFrame("Landscape_2").suppress = true;
   FindGraphicFrame("Landscape_3").suppress = true;
}

However that returned the exact same result. I had to move the function command into the "Global Javascripts" location and surround the rest of the statements into the if statement like this...

if (FusionPro.Composition.inPreprocessing)  {
   return;
}
else  {
   var r = new FusionProResource(Rule("shellName"), "graphic", true);
   if (IsResourceLandscape(r))  {
       FindGraphicFrame("Portrait_2").suppress = true;
       FindGraphicFrame("Portrait_3").suppress = true;
   }
   else  {
       FindGraphicFrame("Landscape_2").suppress = true;
       FindGraphicFrame("Landscape_3").suppress = true;
   }
}

Then it worked like a charm. Again, thank you.

 

One final question...is there any reason why this rule would not work in OnJobStart instead of OnRecordStart? It seems rather silly to have to check the same pdf file over and over checking to see if it had altered somewhere during the middle of the process. I know I could put some sort of additional code around all of this to only do this for the first record, but I don't know if that would mess up the pre-processing scheme as well.

.

Link to comment
Share on other sites

I tried your solution and at first it did not work. I just added the command like this... However that returned the exact same result. I had to move the function command into the "Global Javascripts" location and surround the rest of the statements into the if statement like this...Then it worked like a charm. Again, thank you.

Simply declaring a function does not cause any code to be run, no matter where you declare it. So there must have been something else going on. It's hard to know just by looking through a keyhole at a couple of rules.

One final question...is there any reason why this rule would not work in OnJobStart instead of OnRecordStart?

You can't call the Rule function from OnJobStart, as rules generally rely on record-specific field values. So a function which calls Rule() won't work outside of the context of a record.

It seems rather silly to have to check the same pdf file over and over checking to see if it had altered somewhere during the middle of the process. I know I could put some sort of additional code around all of this to only do this for the first record, but I don't know if that would mess up the pre-processing scheme as well.

Presumably there are different graphics you're analyzing for different records, so it's not just the same single PDF file over and over again. You could always declare a global object to cache the results, something like this:

// in JavaScript Globals:
var TestedGraphics = {};

// at the start of IsResourceLandscape:
if (typeof TestedGraphic[res] != "undefined")
   return TestedGraphic[res];

// at the end of IsResourceLandscape:
var result = TM.textWidth > TM.textHeight;
TestedGraphic[res] = result;
return result;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...