How do I add a rotated image to a page?

39 views
Skip to first unread message

Ryan

unread,
Nov 16, 2017, 5:03:19 PM11/16/17
to PDFTron PDFNet SDK
Question:

I want to manually add an image, with some rotation, to a page, but I am having some trouble doing this.

Answer:

The following code shows how to load and position an image.

using (PDFDoc doc = new PDFDoc())
using (ElementBuilder eb = new ElementBuilder())
using (ElementWriter writer = new ElementWriter())
{
    Page page = doc.PageCreate(new Rect(0, 0, 612, 794));

    writer.Begin(page); 
    Image img = Image.Create(doc, input_path + "peppers.jpg");

    Matrix2D origin = new Matrix2D(1, 0, 0, 1, -page.GetPageWidth() / 2, -page.GetPageHeight() / 2);
    Matrix2D scale = new Matrix2D(72, 0, 0, 72, 0, 0); // size of image.. e.g. 1 inch
    Matrix2D rot = Matrix2D.RotationMatrix(Math.PI / 2); // rotation in radians, e.g. 90deg
    Matrix2D trans = new Matrix2D(1, 0, 0, 1, 144, 144); // position, e.g. 2 inches from the left and bottom edges
    Element element = eb.CreateImage(img, trans * rot * scale);
    writer.WritePlacedElement(element);

    writer.End();
    doc.PagePushBack(page);
    
    doc.Save(output_path + "element_builder.pdf", SDFDoc.SaveOptions.e_linearized);
    Console.WriteLine("Done. Result saved in element_builder.pdf...");
}
Reply all
Reply to author
Forward
0 new messages