Results 1 to 1 of 1
Thread: Rotate Existing PDF Document
- 10-28-2011, 05:52 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Rotate Existing PDF Document
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(); } } }
Similar Threads
-
How to rotate an image
By ChipChamp in forum New To JavaReplies: 4Last Post: 06-20-2012, 07:22 PM -
How to Convert Excel document to word document?
By sudheer.v47 in forum Advanced JavaReplies: 1Last Post: 10-07-2011, 12:32 PM -
Image rotate
By Micromani in forum AWT / SwingReplies: 0Last Post: 03-17-2010, 01:33 PM -
Rotate Image!
By Moncleared in forum AWT / SwingReplies: 1Last Post: 02-11-2009, 05:22 PM -
JFrame rotate
By etheralthougt in forum AWT / SwingReplies: 1Last Post: 10-24-2008, 06:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks