Image on rotated page
Example that shows how to position an image on a rotated page.
imageonrotatedpage
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.images;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
public class ImageOnRotatedPage {
public static final String DEST = "./target/sandbox/images/image_on_rotated_page.pdf";
public static final String IMAGE = "./src/main/resources/img/cardiogram.png";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ImageOnRotatedPage().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4.rotate());
Image img = new Image(ImageDataFactory.create(IMAGE));
img.scaleToFit(770, 523);
float offsetX = (770 - img.getImageScaledWidth()) / 2;
float offsetY = (523 - img.getImageScaledHeight()) / 2;
img.setFixedPosition(36 + offsetX, 36 + offsetY);
doc.add(img);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Images
{
public class ImageOnRotatedPage
{
public static readonly String DEST = "results/sandbox/images/image_on_rotated_page.pdf";
public static readonly String IMAGE = "../../../resources/img/cardiogram.png";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ImageOnRotatedPage().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4.Rotate());
Image img = new Image(ImageDataFactory.Create(IMAGE));
img.ScaleToFit(770, 523);
float offsetX = (770 - img.GetImageScaledWidth()) / 2;
float offsetY = (523 - img.GetImageScaledHeight()) / 2;
img.SetFixedPosition(36 + offsetX, 36 + offsetY);
doc.Add(img);
doc.Close();
}
}
}