Hope someone can help. I'd like to start by saying I am completely new to Java (never seen it until today), so please excuse my stupidity :)

I am using Lotus Domino (not really relevant but gives context) to pick up an existing PDF, which I'll use as a template, and create a new PDF, which will have some text added.

The PDF "template" file is in landscape.

The code below is code I have taken from an example on a Domino forum. It gets a PDF attachment and puts it into an input stream. i get that bit. It then creates a new document, which is where I'll be putting the imported PDF template content.

The resulting PDF is in landscape, as I want, but the content from the template is in portrait, ie the landscape content rotated, so it's way off the page.

I can make the document portrait too, at which point everything fits, but then I need to rotate the whole thing so I can put some text in.

So I have 2 questions:

either:

1. how can I correctly rotate the initial template contents, so that when I put them into the new document, they are in the correct orientation?
2. how can I place the intial template contents (without rotating) into the portrait document and then rotate the WHOLE thing?

Thanks for any help


Java Code:
import lotus.domino.*;

import java.io.*;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.Font;
import com.lowagie.text.Element;

public class JavaAgent extends AgentBase {

	public void NotesMain() {
		
		try {
			
			Session session = getSession();
			AgentContext agentContext = session.getAgentContext();
			Database db = agentContext.getCurrentDatabase();
			lotus.domino.Document doc = agentContext.getDocumentContext();
			
			String id = doc.getUniversalID();
     		String filename = id+".pdf";
	
			ByteArrayOutputStream os = new ByteArrayOutputStream();
	
			View settings = db.getView("luASK");
			lotus.domino.Document setting = settings.getDocumentByKey("Files\\PDF Template", true);
			
			if (setting!=null){
				String filepath = setting.getItemValueString("ASValue");
				String filesname = filepath.substring(filepath.lastIndexOf("/")+1);
				EmbeddedObject file = setting.getAttachment(filesname);
				InputStream is = file.getInputStream();
			
				//Here's the key part. Let's turn the template in to
				//usable PDF object
				PdfReader reader = new PdfReader(is);
				
 				Rectangle psize = reader.getPageSize(1);
  				float h = psize.getHeight();
 				float w = psize.getWidth(); 
 
 				//now create a new document
 				com.lowagie.text.Document document = new com.lowagie.text.Document(new Rectangle(w, h));
				
				//now create the PDF Writer, based on the document created
 				PdfWriter writer = PdfWriter.getInstance(document, os); 
 				document.open();
 				document.newPage();
 				//now get the imported page into the writer
 				PdfImportedPage page = writer.getImportedPage(reader, 1);
				//Now, add it to the blank PDF document we've opened
				PdfContentByte cb = writer.getDirectContent();
				
				//document.newPage();
				cb.addTemplate(page,0,0);
			
				is.close();
				file.recycle(); //removes it from temp directory!
			
			Phrase phrase = new Phrase();
			
			document.add(phrase);
		
			Paragraph p = new Paragraph( doc.getItemValueString("User") , new Font(Font.HELVETICA, 16, Font.BOLD));
			p.setAlignment(Element.ALIGN_CENTER);
			p.setSpacingAfter(50);
			document.add( p );
			

			Paragraph pp = new Paragraph( doc.getItemValueString("Package") , new Font(Font.HELVETICA, 16, Font.BOLD));
			pp.setAlignment(Element.ALIGN_CENTER);
			document.add( pp );
			
			Paragraph ppp = new Paragraph( doc.getItemValueString("Pass_Date") , new Font(Font.HELVETICA, 16, Font.BOLD));
			ppp.setAlignment(Element.ALIGN_CENTER);
			document.add( ppp );
			
			document.close();
			
		  	//Put the contents in to a Notes Stream!
			Stream stream=session.createStream();
			stream.write( os.toByteArray() );
     
			//Attach file here
			if ( !doc.isNewNote() )
				doc.removeItem("Files");
				
			MIMEEntity m = doc.createMIMEEntity("Files"); //if no param -- creates a field called Body (so one can't already be on form!)

			MIMEHeader header = m.createHeader("content-disposition");
			header.setHeaderVal("attachment;filename=\""+filename+"\"");
	
			m.setContentFromBytes(stream, "application/pdf", MIMEEntity.ENC_IDENTITY_BINARY); //ENC_BASE64);
			m.decodeContent();
 			
 			
 			//Following call to doc.save() is NEEDED to commit the MIME to the doc 
			//even when we're in a WQS doc. Weird?!
			//To allow Anonymous users to do so we need to add them to an Authors field (see Form). Even weirder.
 			doc.save(true, true);

			//Email the PDF?
			if (doc.getItemValueString("Email").equals("1") && !doc.getItemValueString("EmailTo").equals("")){
			
				DEXTLogger log = new DEXTLogger(session);

				session.setConvertMIME(false);
				
				lotus.domino.Document MailDoc = db.createDocument();
				MIMEEntity m2 = MailDoc.createMIMEEntity("Body");
				MIMEHeader hdr = m2.createHeader("Subject");
				hdr.setHeaderValAndParams("The File You Just Created");
				
				MailDoc.replaceItemValue("Form", "Memo");
				
				hdr = m2.createHeader("MIME-Version");
				hdr.setHeaderValAndParams("1.0");
				m2.setPreamble("This is a multipart message in MIME format.");
	
				MIMEEntity tmp1 = m2.createChildEntity( );

				Stream substream = session.createStream();
				
				substream.writeText("<html><body><p>Here's the file you just generated!</p></body></html>");
				
				tmp1.setContentFromText( substream, "text/html", MIMEEntity.ENC_NONE);
				
				tmp1 = m2.createChildEntity( );
				
				hdr = tmp1.createHeader("Content-Disposition");
				hdr.setHeaderValAndParams("attachment; filename=\""+filename+"\"");
				
				tmp1.setContentFromBytes( stream, "application/pdf", MIMEEntity.ENC_IDENTITY_BINARY);
			
				MailDoc.closeMIMEEntities(true, "Body");
			
				MailDoc.replaceItemValue("Principal", "noreply@codestore.net@codestore");
				MailDoc.replaceItemValue("DisplaySent", "noreply@codestore.net@codestore");
				MailDoc.replaceItemValue("ErrorsTo", "noreply@codestore.net@codestore");
				MailDoc.replaceItemValue("INetFrom", "noreply@codestore.net");
			
				MailDoc.replaceItemValue("SendTo", doc.getItemValueString("EmailTo"));
			
				MailDoc.send(false);
				log.logEvent("Sent PDF MIME message to "+ doc.getItemValueString("EmailTo"));
				substream.close();
				
				//Reset email options
				doc.replaceItemValue("Email", "");
				doc.replaceItemValue("EmailTo", "");
				
				session.setConvertMIME(true);		
 			}
 					
			stream.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}