There may be a bug in scaleToFit() when the image is rotated. I'll
investigate.
Paulo
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of Bryan Bunch
> Sent: Wednesday, December 06, 2006 11:10 PM
> To: Post all your questions about iText here
> Subject: Re: [iText-questions] Appended TIFF image problem
>
> Just FYI -- I figured this one out -- I was able to get the TIFF image
> to display properly by just scaling the image with the "scalePercent"
> method of the Image class rather than with the "scaleToFit" method.
>
> The calculation to scale the image to the entire PDF doc area
> of the page is
>
> img.scalePercent(((docWidth/img.scaledWidth()) * 100f),
> ((docHeight/img.scaledHeight()) * 100f));
>
> I'm not quite sure why the Image.scaleToFit method never worked for me
> for landscape TIFF images.
>
> ----Bryan
>
>
> On 12/6/06, Bryan Bunch <[EMAIL PROTECTED]> wrote:
> > The image rotates, but still does not fill the entire PDF page area.
> >
> > I can rotate the Image 90 or 270 degrees and it will fill the
> > available portrait area, but will be compressed
> horizontally. If I do
> > the same thing and also create the PDF page area rotated, I get a
> > horizontally oriented landscape display area with an image that is
> > still constrained/compressed horizontally (it's like it's
> still trying
> > to fit in portrait mode even though the PDF page area is
> horizontally
> > oriented for landscape).
> >
> > It seems like I'm just missing some kind of scaling option, but I'm
> > not sure what that is.
> >
> > If the TIFF image is a regular portrait mode TIFF (i.e.
> something like
> > standard A4 size), everything works fine.
> >
> > ----Bryan
> >
> > On 12/6/06, Paulo Soares <[EMAIL PROTECTED]> wrote:
> > > What happens when you call img.setRotationDegrees()?
> > >
> > > Paulo
> > >
> > > ----- Original Message -----
> > > From: "Bryan Bunch" <[EMAIL PROTECTED]>
> > > To: <[email protected]>
> > > Sent: Wednesday, December 06, 2006 8:00 PM
> > > Subject: [iText-questions] Appended TIFF image problem
> > >
> > >
> > > > My apologies if this has been addressed before (I've
> scanned through
> > > > the FAQ and archives, and nothing popped out at me).
> > > >
> > > > My problem has to do with appending a TIFF image in
> landscape mode
> > > > using the PdfStamper to append a page to an existing PDF file.
> > > >
> > > > Things work great as long as the TIFF image is a
> portrait TIFF. Things
> > > > go south when the appended TIFF image is in landscape
> mode. I've tried
> > > > scaling the TIFF image to fit the page area, but it
> displays with the
> > > > TIFF file compressed into the lower half of the page.
> > > >
> > > > The TIFF file causing the problem is attached, and
> below is the code
> > > > I've been working with to try and solve the problem. If
> anyone else
> > > > has run into this, I'd be VERY appreciative to hear about it.
> > > >
> > > > public static void appendFiles(String workingDir,
> String prefix,
> > > > String mainFile,
> > > > java.util.List pdfAttachments,
> > > > java.util.List tifAttachments)
> > > > throws java.io.IOException
> > > > {
> > > > ArrayList master = new ArrayList();
> > > > File concatPdfFile = File.createTempFile(prefix
> + "PlusPdf",
> > > > "." + FileUtil.getExtension(mainFile));
> > > > if (log.isDebugEnabled())
> > > > log.debug("[appendFiles] mainFile=" + mainFile);
> > > >
> > > > float docWidth=PageSize.A4.width(),
> docHeight=PageSize.A4.height();
> > > > try {
> > > > int attachmentPages = 0;
> > > >
> > > > // Create a reader for the main document
> > > > PdfReader mainReader = new
> PdfReader(workingDir + mainFile);
> > > > mainReader.consolidateNamedDestinations();
> > > >
> > > > int pageOffset = mainReader.getNumberOfPages();
> > > > if (log.isDebugEnabled())
> > > > log.debug("[appendFiles] " + pageOffset
> + " pages in
> > > > main PDF doc " + mainFile);
> > > >
> > > > java.util.List mainBookmarks =
> > > > SimpleBookmark.getBookmark(mainReader);
> > > > if (mainBookmarks != null) {
> > > > master.addAll(mainBookmarks);
> > > > }
> > > >
> > > > // step 1: creation of a document-object
> > > > Rectangle mainPageSize =
> mainReader.getPageSizeWithRotation(1);
> > > > //Rectangle mainPageSize = mainReader.getPageSize(1);
> > > > docWidth = mainPageSize.width();
> > > > docHeight = mainPageSize.height();
> > > > Document document = new Document(new
> Rectangle(docWidth,
> > > > docHeight));
> > > >
> > > > // step 2: we create a writer that listens
> to the document
> > > > PdfCopy copier = new PdfCopy(document, new
> > > > FileOutputStream(concatPdfFile));
> > > >
> copier.setViewerPreferences(PdfWriter.PageModeUseOutlines);
> > > >
> > > > // step 3: we open the document
> > > > document.open();
> > > >
> > > > // step 4: we add content - in this case,
> each page of the main
> > > > doc
> > > > for (int i=1; i <= pageOffset; i++) {
> > > > PdfImportedPage nextPage =
> > > > copier.getImportedPage(mainReader, i);
> > > > copier.addPage(nextPage);
> > > > }
> > > >
> > > > // Part 1: Append PDF attachments
> > > > // Now add each page of each PDF attachment in turn
> > > > for (int k = 0; k < pdfAttachments.size(); k++) {
> > > > String nextPdfFile =
> (String)pdfAttachments.get(k);
> > > > String shortPdfFile =
> > > >
> nextPdfFile.substring(nextPdfFile.lastIndexOf(File.separator) + 1);
> > > > if (shortPdfFile.startsWith(prefix+"_"))
> > > > shortPdfFile =
> > > > shortPdfFile.substring(prefix.length()+1);
> > > > log.info("Processing nextPdfFile " +
> shortPdfFile);
> > > > try {
> > > > // we create a reader for this attachment
> > > > PdfReader reader = new
> PdfReader(nextPdfFile);
> > > >
> > > > // we retrieve the total number of
> pages in this
> > > > attachment
> > > > attachmentPages = reader.getNumberOfPages();
> > > > if (log.isDebugEnabled())
> > > > log.debug("nextPdfFile " +
> shortPdfFile + "
> > > > has " + attachmentPages + " pages");
> > > >
> > > > reader.consolidateNamedDestinations();
> > > > for (int j=1; j <= attachmentPages; j++) {
> > > > pageOffset++;
> > > > // Create bookmark to first page
> of each file
> > > > if (j == 1) {
> > > > HashMap bookmark = new HashMap();
> > > > bookmark.put("Action", "GoTo");
> > > > bookmark.put("Title", shortPdfFile);
> > > > bookmark.put("Page",
> pageOffset + " Fit");
> > > > master.add(bookmark);
> > > > }
> > > >
> > > > PdfImportedPage nextAttachmentPage =
> > > > copier.getImportedPage(reader, j);
> > > > copier.addPage(nextAttachmentPage);
> > > > }
> > > > }
> > > > catch (Throwable e) {
> > > > log.error("Exception while processing " +
> > > > nextPdfFile + ": " + e.getMessage());
> > > > continue;
> > > > }
> > > > if (log.isDebugEnabled())
> > > > log.debug(pageOffset + " cumulative
> pages after
> > > > processing PDF " + shortPdfFile);
> > > > }
> > > > // step 5: we close the document
> > > > document.close();
> > > >
> > > > // Part 2: Append TIFF attachments
> > > > PdfReader concatReader = new PdfReader(new
> > > > FileInputStream(concatPdfFile));
> > > > if (log.isDebugEnabled())
> > > > log.debug("reader pages after appending PDF (" +
> > > > concatReader.getNumberOfPages() + ") should equal pageOffset " +
> > > > pageOffset);
> > > > PdfStamper stamper = new PdfStamper(concatReader, new
> > > > FileOutputStream(workingDir + mainFile));
> > > >
> > > > for (int l = 0; l < tifAttachments.size(); l++) {
> > > > String nextTifFile =
> (String)tifAttachments.get(l);
> > > > String shortTifFile =
> > > >
> nextTifFile.substring(nextTifFile.lastIndexOf(File.separator) + 1);
> > > > if (shortTifFile.startsWith(prefix+"_"))
> > > > shortTifFile =
> > > > shortTifFile.substring(prefix.length()+1);
> > > > log.info("Processing nextTifFile " +
> shortTifFile);
> > > >
> > > > RandomAccessFileOrArray ra = null;
> > > > try {
> > > > ra = new
> RandomAccessFileOrArray(nextTifFile);
> > > > attachmentPages =
> TiffImage.getNumberOfPages(ra);
> > > > if (log.isDebugEnabled())
> > > > log.debug("nextTifFile " +
> shortTifFile + "
> > > > has " + attachmentPages + " pages");
> > > > for (int c = 1; c <= attachmentPages; c++) {
> > > > Image img =
> TiffImage.getTiffImage(ra, c);
> > > > //Image img =
> Image.getInstance(nextTifFile);
> > > > if (img != null) {
> > > > pageOffset++;
> > > >
> > > >
> > > > //stamper.addFileAttachment("AttachedFile#" + c, null,
> nextTifFile,
> > > > shortTifFile);
> > > >
> > > > stamper.insertPage(pageOffset, new
> > > > Rectangle(img.scaledHeight(), img.scaledWidth()));
> > > > /*
> > > > Rectangle(new
> Rectangle(img.plainHeight(),
> > > > img.plainWidth()))
> > > > if (img.scaledWidth() >
> img.scaledHeight())
> > > > {
> > > > stamper.insertPage(pageOffset,
> > > > mainPageSize.rotate()); // PageSize.A4);
> > > > }
> > > > else
> > > > {
> > > > stamper.insertPage(pageOffset,
> > > > mainPageSize);
> > > > }
> > > > */
> > > > // Create bookmark to first
> page of each file
> > > > if (c == 1) {
> > > > HashMap bookmark = new HashMap();
> > > > bookmark.put("Action", "GoTo");
> > > > bookmark.put("Title",
> shortTifFile);
> > > > bookmark.put("Page",
> pageOffset + " Fit");
> > > > master.add(bookmark);
> > > > }
> > > >
> > > > // Create header to first
> page of each file
> > > > PdfContentByte cb =
> > > > stamper.getOverContent(pageOffset);
> > > > //if ( img.scaledHeight() >
> img.scaledWidth() )
> > > > //if (img.scaledWidth() >
> img.scaledHeight() )
> > > > //{
> > > > //
> img.setRotationDegrees( (long ) 270.0 );
> > > > // //log.debug ( "Rotated img " +
> > > > tiff_file + " in pdf file " + pdf_file );
> > > > //}
> > > >
> > > > //img.setRotationDegrees(
> (long ) 270.0 );
> > > > /*
> > > > if (img.scaledWidth() > docWidth ||
> > > > img.scaledHeight() > docHeight) {
> > > > //img.setDpi(200, 200);
> > > >
> System.out.println("scaling image");
> > > >
> //img.scaleToFit(docWidth, docHeight);
> > > >
> > > > //if (img.scaledWidth() >
> > > > img.scaledHeight() )
> > > > //{
> > > > //
> img.scaleToFit(docHeight, docWidth);
> > > > //}
> > > > //else
> > > > //{
> > > > //
> img.scaleToFit(docWidth, docHeight);
> > > > //}
> > > > }
> > > > */
> > > > img.scaleToFit(img.scaledHeight(),
> > > > img.scaledWidth());
> > > > img.setAbsolutePosition(0, 0);
> > > > cb.addImage(img);
> > > >
> > > > }
> > > > }
> > > > }
> > > > catch (Throwable e) {
> > > > log.error("Error while processing "
> + nextTifFile
> > > > + ": " + e.getMessage());
> > > > continue;
> > > > }
> > > > ra.close();
> > > > if (log.isDebugEnabled())
> > > > log.debug(pageOffset + " cumulative
> pages after
> > > > processing TIFF " + shortTifFile);
> > > > }
> > > >
> > > > if (log.isDebugEnabled())
> > > > log.debug("[appendFiles] setOutlines: "
> + master);
> > > > if (!master.isEmpty()) {
> > > > stamper.setOutlines(master);
> > > > }
> > > >
> > > > concatPdfFile.delete();
> > > > stamper.close();
> > > > }
> > > > catch (Throwable e) {
> > > > e.printStackTrace();
> > > > }
> > > > }
> > > >
> > > > ----Bryan
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta transmissão desta
mensagem não significa a perca de confidencialidade. Se esta mensagem for
recebida por engano, por favor envie-a de volta para o remetente e apague-a do
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de
usar, revelar ou distribuir qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may contain
confidential or legally protected information. The incorrect transmission of
this message does not mean the loss of its confidentiality. If this message is
received by mistake, please send it back to the sender and delete it from your
system immediately. It is forbidden to any person who is not the intended
receiver to use, distribute or copy any part of this message.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/