Adding text to a document

Using Print2Flash Document API
Post Reply
franciscus
Posts: 4
Joined: Sun Jul 22, 2012 11:55 am

Adding text to a document

Post by franciscus » Sun Jul 22, 2012 1:28 pm

At the time of displaying/printing the document, I'd like to add some custom text to the footer of the document. I will use this to put the name or login-id of the user who prints the document. The purpose is to discourage people from sharing printed documents with other people who haven't paid for access to my website.

Is there an API that allows me to do that? If I could add a custom watermark at the time my Flash file is displayed, that would also allow me to do this. Or if I could reuse and change the string that the unregistered version Print2Flash prints out.

Any ideas?

staff
Posts: 281
Joined: Sat Dec 15, 2007 4:48 pm

Re: Adding text to a document

Post by staff » Mon Jul 23, 2012 1:03 pm

There is a watermark feature in Print2Flash. You may add an image to all document pages at document conversion time. See this help topic for more information: http://print2flash.com/help/Watermark.php

franciscus
Posts: 4
Joined: Sun Jul 22, 2012 11:55 am

Re: Adding text to a document

Post by franciscus » Wed Jul 25, 2012 9:21 pm

Yes, at conversion time. I would like to add it dynamically, when the document is being displayed by a user.

Could I do something like this:
- Generate my document without template (i.e. for "External viewer")
- Load the document into some kind of "envelope" swf (which I would write) and which would add the extra text
- Make DocViewer read that envelope swf

Or, if that can't be done:
- Generate my document with a custom template
- Design that template to not be a viewer, but an envelope, as defined above

Am I out to lunch?

staff
Posts: 281
Joined: Sat Dec 15, 2007 4:48 pm

Re: Adding text to a document

Post by staff » Thu Jul 26, 2012 4:04 am

You may employ the following approach:
1. Load Print2Flash documents in your own envelope Flash movie as described in this help topic: http://print2flash.com/help/UsingDocume ... ashCS3.php
2. Obtain a reference to the page represented by MovieClip class instance using getPage Document API function (see this help topic: http://print2flash.com/help/DocumentAPIFunctions.php)
3. Use properties and methods of Graphics class (the reference to which you may obtain from graphics property of MovieClip class) to draw any additional content on document pages (see this help topic: http://http://help.adobe.com/en_US/Flas ... phics.html)

You may also develop your own document viewer template as described here: http://print2flash.com/help/DesigningDocumentViewer.php

franciscus
Posts: 4
Joined: Sun Jul 22, 2012 11:55 am

Re: Adding text to a document

Post by franciscus » Sat Jul 28, 2012 2:37 pm

Thanks! Whereas I tried to put code between the viewer and the document, you are saying I should write an envelope around the document-with-integrated-viewer. Sounds like a good idea and I almost got it to work.

That is, the added text displays in the viewer but when I print it, it is not included in the printout. Am I missing something?

Here is my code:

Code: Select all

import Print2Flash.*;

var P2FDocLoader:Print2FlashDoc3=new Print2FlashDoc3("FlashDoc.swf",0,0,500,350,this);
P2FDocLoader.addEventListener(Print2FlashDoc3.ONLOADEVENT, OnLoaded);

var P2FDoc:MovieClip;

function OnLoaded(e:Event) {
  	P2FDoc=P2FDocLoader.getDoc();
	P2FDoc.addEventListener("onPageLoaded", OnPageLoaded);
}

function OnPageLoaded(e:Object)
{
	var page:MovieClip = P2FDoc.getPage(e.page);

	var fmt:TextFormat = new TextFormat();
	fmt.size = 100;
	
	var text:TextField = new TextField(); 
	text.text = "hello world"; 
	text.setTextFormat(fmt);	
	text.width = 500;
	text.height = 300;
	text.x = 100;
	text.y = 100;
	
	page.addChild(text); 
}
I also tried adding the text to the first child of the page, because I noticed that each page object has a single child called PageX. But the results are the same

Code: Select all

	// Replaced  page.addChild(text);  with the following
	var c:MovieClip = page.getChildAt(0) as MovieClip;	
	c.addChild(text);

staff
Posts: 281
Joined: Sat Dec 15, 2007 4:48 pm

Re: Adding text to a document

Post by staff » Sun Jul 29, 2012 4:00 am

If you want to include additional content on printed pages, you need to develop your own printing algorithm. You need to add your own Print button invoking this algorithm beside the document. The standard Print button can be hidden by clearing Print Button option in the Interface tab of Document Options window. Your algorithm may be like the following:

Code: Select all

function Print(e:MouseEvent) {
	var printJob:PrintJob = new PrintJob();		
	if (printJob.start()) {
		for (var i:int=1;i<=totalPages;i++) {
			var page:MovieClip=AttachPageMovie(i);
                        // Draw additional content on the page here
                        ...
			var pageAdded:Boolean=PrintPage(printJob,page);
			if (!pageAdded) break;
		}
		printJob.send();
	}
}

function PrintPage(printJob:PrintJob, page:MovieClip):Boolean {
	var pageAdded:Boolean=false

	var paWidth:Number=printJob.pageWidth/72*P2FDoc.Resolution
	var paHeight:Number=printJob.pageHeight/72*P2FDoc.Resolution

	// Create white "paper" corresponding to printable area size
	var pageMovie:MovieClip=new MovieClip()
	addChild(pageMovie) 
	DrawRect(pageMovie,0,0,paWidth*1.1,paHeight*1.1,0xFFFFFF,0xFFFFFF)
	
	// Put page on "paper" centering it
	pageMovie.addChild(page)
	page.x=(paWidth-page.width)/2
	page.y=(paHeight-page.height)/2
	
	var printArea:Rectangle=new Rectangle(0,0,paWidth,paHeight)				  
	pageMovie.scaleX=pageMovie.scaleY=72/P2FDoc.Resolution			
	
	try {
	  printJob.addPage(pageMovie,printArea,new PrintJobOptions(P2FDoc.PrintAsBitmap))
	  pageAdded=true;
	}
	catch(e:Error) {
	}	
	
	removeChild(pageMovie) 
	return pageAdded
}

function AttachPageMovie(pageNum:int):MovieClip {
	var movie:MovieClip
	try {
		var AssetClass:Class = P2FDocLoader.contentLoaderInfo.applicationDomain.getDefinition("Page"+pageNum) as Class;
		movie = new AssetClass();
	 }
	 catch(e:Error) {
	 }
	 return movie	
}

function DrawRect(movie:Sprite,x1:Number,y1:Number,x2:Number,y2:Number,fillColor:uint,lineColor:Number) {
	var gr:Graphics=movie.graphics;
	gr.beginFill(fillColor);
	gr.lineStyle(0,lineColor);
	gr.drawRect(x1,y1,x2-x1,y2-y1);
	gr.endFill();
}
Print function should be invoked when a user presses your Print button. Other variables used here have the following meanings:
P2FDocLoader - the value returned by the Print2FlashDoc3 class constructor;
P2FDoc - reference to the document returned by getDoc() function;
totalPages - total number of pages as returned by the getNumberOfPages() function.

You should place any additional content on pages at the place indicated by "Draw additional content on the page here" comment in the code.

franciscus
Posts: 4
Joined: Sun Jul 22, 2012 11:55 am

Re: Adding text to a document

Post by franciscus » Wed Aug 01, 2012 9:12 pm

Thanks!!! I got it working.

You have been very very helpful. I am impressed by the support you give even before I purchase. I'll definitely buy a license before my trail expires.

Thanks again.

Post Reply