Hi

I am using the class below to shrink every page in a PDF in order to make room 
for an 15mm margin on both left and right edge.

When I call this in a Windows-environment things work fine, and the resulting 
PDF is consisting of a set of 'shrinked' pages from the original with each page 
'stamped' with the file-name I supply.
But when running the same code with the same PDF in a Solaris environment the 
result is different, and I get an error-message when trying open the PDF 
(Insufficent data for image).

I use binary transfer when transporting the pdf's to/from Solaris, and I am 
sure that the byte[] used when calling this class is identical, as I control 
the length of it just before calling this class.




import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfEditor {

      private static float points15mm = Utilities.millimetersToPoints(15);
      private static Log log = LogFactory.getLog(PdfEditor.class);

      public static byte[] schrinkImages(byte[] src, String docname) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                  Document document = new Document(PageSize.A4);
                  PdfWriter writer = PdfWriter.getInstance(document, baos);
                  document.open();
                  PdfReader reader = new PdfReader(src);
                  int n = reader.getNumberOfPages();
                  log.info("Antall sider : " + n + " Rot 
"+reader.getPageRotation(1) );
                  PdfImportedPage page;
                  float maxwidth = document.getPageSize().getWidth() - 
points15mm * 2;
                  for (int i = 1; i <= n; i++) {
                        document.add(new Paragraph("       " + docname));

                        page = writer.getImportedPage(reader, i);
                        Image image = Image.getInstance(page);

                        image.scaleToFit(maxwidth, PageSize.A4.getHeight());
                        image.setAbsolutePosition(points15mm, points15mm);
                        
image.setRotationDegrees(reader.getPageRotation(i)==270?90:0);
                        image.setCompressionLevel(9);
                        image.simplifyColorspace();
                        document.add(image);
                        document.newPage();

                  }
                  document.close();

            } catch (DocumentException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            return baos.toByteArray();
      }



}
------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to