iText Image Resize

106,624

Solution 1

You could use another approach : resize the image "manually" (i.e. through an image processing software) instead of programmatically through iText.

Since the final dimension seems hardcoded, you could use an already resized image and save yourself some processing time every time you watermark PDF documents.

Solution 2

I do it like that:

//if you would have a chapter indentation
int indentation = 0;
//whatever
Image image = coolPic;

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - indentation) / image.getWidth()) * 100;

image.scalePercent(scaler);

Solution 3

use

watermark_image.scaleAbsolute(826, 1100);

instead of

watermark_image.scaleToFit(826, 1100);

Solution 4

Just in case if the image height exceeds the document height:

float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);

Solution 5

you can use

imageInstance.scaleAbsolute(requiredWidth, requiredHeight);
Share:
106,624

Related videos on Youtube

Failsafe
Author by

Failsafe

Languages: C# JavaScript, HTML5, CSS C++ Java COBOL C Environments: CICS LINUX(CentOS, RedHat Enterprise, Kali) Windows MAC OSX Most IBM facilities(zOS, CICS, Rational)

Updated on July 17, 2022

Comments

  • Failsafe
    Failsafe almost 2 years

    I have a watermark that I would like to put into my pdf. The watermark is a .bmp image, and is 2290 x 3026. I am having a lot of trouble trying to resize this picture to fit the page, does anyone have any suggestions?

    Document document = new Document(); 
    PdfWriter.getInstance(document, new FileOutputStream("result.pdf")); 
    document.open(); 
    document.add(new Paragraph("hello")); 
    document.close(); 
    PdfReader reader = new PdfReader("result.pdf"); 
    int number_of_pages = reader.getNumberOfPages(); 
    PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream("result_watermark.pdf")); 
    // Get the PdfContentByte type by pdfStamper. 
    Image watermark_image = Image.getInstance("abstract(0307).bmp"); 
    int i = 0; 
    watermark_image.setAbsolutePosition(0, 0);
    watermark_image.scaleToFit(826, 1100);
    System.out.println(watermark_image.getScaledWidth());
    System.out.println(watermark_image.getScaledHeight()); 
    PdfContentByte add_watermark; 
    while (i < number_of_pages) { 
        i++; 
        add_watermark = pdfStamper.getUnderContent(i); 
        add_watermark.addImage(watermark_image); 
    } 
    pdfStamper.close();
    

    Here is the output for the getScaled() methods.

    826.0 - Width
    1091.4742 - Height
    

    I would share the picture of the pdf with you guys but unfortunately I can't.

    Should I try using a .jpg instead? I don't really know how well iText handles different image extensions.

    • Ritesh
      Ritesh almost 12 years
    • Failsafe
      Failsafe almost 12 years
      That's not the problem. The watermark I am adding is a company watermark, and I can't just be passing it around.. Well I was told not to anyway.
    • Alexis Pigeon
      Alexis Pigeon almost 12 years
      have you tried scaling the image manually, instead of programmatically, and use the manually scaled image in your code? since you seem to hardcode the scaled dimension, that would save you some processing every time you watermark PDF documents.
    • Failsafe
      Failsafe almost 12 years
      @AlexisPigeon - No I haven't, but I'll try it and get back to you..
    • Failsafe
      Failsafe almost 12 years
      @AlexisPigeon - The manual resize worked, it's a little blurry, but I can deal. Add your comment as an answer and I'll give you a check mark.
    • MGDroid
      MGDroid over 11 years
      @Failsafe its too late to answer I think but you can use scaleAbsolute(float x, float y) method. See my answere below.
  • Failsafe
    Failsafe almost 12 years
    Unfortunately that doesn't work for me. The closest I have ever gotten was to do: watermark_image.scalePercent(110 * 72 / 300);
  • Franz Ebner
    Franz Ebner almost 12 years
    can you describe exactly what's your problem!? the scaling doesn't work- ok but what's your output with the scaler you use at the time!?
  • Ted
    Ted over 10 years
    This approach worked for me better than the idea proposed by Alexis because the default scaling has a pretty low resolution, so if you resize the original image to fit the pdf document, you get a pixelated low resolution image at maybe 72 dpi equivalent. But with scaling, you can use a much larger image resized to fit but preserving the high resolution.
  • Magno C
    Magno C about 9 years
    Worked like a charm. Thanks.
  • Oleksandr Firsov
    Oleksandr Firsov over 6 years
    This is good. Simply writing image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); can lead bigger images to go out of page border
  • Paresh Jain
    Paresh Jain over 6 years
    sorry for answering late, but i had started working professionally in this 5 months, and i had got lot help from this site, so, i taught, i should pay back something to this site, till my capability.
  • M.A.Naseer
    M.A.Naseer about 6 years
    .height() and .width() don't seem to be valid method names. I had to change to document.getPageSize().getWidth() and document.getPageSize().getHeight() to make it work.
  • Allan Andrade
    Allan Andrade almost 5 years
    Works perfect for me! Thanks a lot!
  • Sidney
    Sidney over 4 years
    @Failsafe You at least owe it to Paresh to try his solution and see if it works better. He did go through the effort of writing it.
  • Failsafe
    Failsafe over 4 years
    @Sidney CS0246 The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?) help!? How do i solve this?
  • Fred
    Fred over 4 years
    What is the difference between these two methods?
  • Markiian Benovskyi
    Markiian Benovskyi over 3 years
    This solution worked for me. Though I would say it's rather a workaround and there might be a proper way to set the image to fit the width
  • Siddharth Shakya
    Siddharth Shakya about 2 years
    didn't work for images with larger height
  • alrts
    alrts about 2 years
    From the iText in Action 2nd ed: The width and height parameters of scaleToFit() define the maximum dimensions of the image. If the width/height ratio differs from the aspect ratio of the image, either the width, or the height, will be smaller than the corresponding parameter of this method. The width and height parameters will be respected when using scaleAbsolute(). The resulting image risks being stretched in the X or Y direction if you don’t choose the parameters wisely. You can also use ScaleAbsoluteWidth() and scaleAbsoluteHeight().
  • alrts
    alrts about 2 years
    This helped me a lot, thank you! As iText almost never works as you think it works at the first attempt, such modules save time and give you more time to think about the real problem at hand.