How can I add an image to given coordinates (x,y) on a PDF page, using IronPdf?
Using the samples, I managed easily to add a "stamp", but that is placed in the center of the page.
I would need to be able to pass (x,y) as parameters when adding the image to each page, and I find this info nowhere in the class documentation.
var stamper = new IronPdf.Editing.ImageStamper(Image);
pdf.ApplyStamp(stamper, pageNumber);
See code above that adds the image in the page center.
You can set HorizontalOffset and VerticalOffset by IronPdf.Editing.Length .
I wrote sample code as below, hope it helps:
var pdf = new PdfDocument("./test.pdf");
ImageStamper logoImageStamper = new ImageStamper("[YOUR IMAGE PATH]/cat.jpg");
logoImageStamper.HorizontalOffset = new Length(300, MeasurementUnit.Pixel);
logoImageStamper.VerticalOffset = new Length(-500, MeasurementUnit.Pixel);
pdf.ApplyStamp(logoImageStamper);
pdf.SaveAs("test_new.pdf");
Code running effect:
Related
I run my own fantasy baseball yearly projections and I'm looking to automatically add player images to a sheet I created.
Test Page
I use a formula to create a link to the player's image page on ESPN.
https://a.espncdn.com/combiner/i?img=/i/headshots/mlb/players/full/33859.png
Is there a way to automatically add the player image to the page?
=image("Link") is the formula, but i cant use the cell location as the link.
Any advise or guidance will be helpful and appreciated
I created an Apps Script method to achieve your target as it is not possible to do it using formula.
Here's the script I did with comments:
function SpecialOnEdit(e) {
//Get the edited cell
var range = e.range;
//Get the active sheet
var sheet = SpreadsheetApp.getActiveSheet();
//Download the image. (e.value is the URL in Google sheet)
var response = UrlFetchApp.fetch(e.value);
//Get the binary data of the image and create a blob object
var binaryData = response.getContent();
var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
//Insert the image into the same row that the user place the image URL link
var image = sheet.insertImage(blob, range.getHeight(), range.getRow());
//Set the height of the row, show that the image can fit the row
sheet.setRowHeight(range.getRow(), image.getHeight() + 10);
}
You should add triggers next after pasting the script. Go to your Script Editor and click Edit->Current project's triggers->Add Trigger->under Select event type, select On edit->click Save
Next, to test the trigger and script, paste a URL of a PNG image format in the spreadsheet to validate the result.
Here's a sample output of the script and trigger:
I try to set my main tile with the templatew TileWide310x150SmallImageAndText02. I had a look at the sample code and took this out of it:
ITileSquare310x310SmallImagesAndTextList04 tileContent =
TileContentFactory.CreateTileSquare310x310SmallImagesAndTextList04();
tileContent.Image1.Src = "ms-appx:///Assets/Logo.png";
tileContent.TextHeading1.Text = "Cashflow";
tileContent.TextWrap1.Text = "Income: 500";
tileContent.TextWrap2.Text = "Spending: 400";
tileContent.TextWrap3.Text = "Earnings: 200";
// Create a notification for the Wide310x150 tile using one of the available templates for the size.
ITileWide310x150SmallImageAndText02 wide310x150Content =
TileContentFactory.CreateTileWide310x150SmallImageAndText02();
wide310x150Content.Image.Src = "ms-appx:///Assets/Logo.png";
wide310x150Content.TextHeading.Text = "Cashflow";
wide310x150Content.TextBody1.Text = "Income: 500";
wide310x150Content.TextBody2.Text = "Spending: 400";
wide310x150Content.TextBody3.Text = "Earnings: 200";
// Create a notification for the Square150x150 tile using one of the available templates for the size.
ITileSquare150x150PeekImageAndText01 square150x150Content =
TileContentFactory.CreateTileSquare150x150PeekImageAndText01();
square150x150Content.Image.Src = "ms-appx:///Assets/Logo.png";
square150x150Content.TextHeading.Text = "Cashflow";
square150x150Content.TextBody1.Text = "Income: 500";
square150x150Content.TextBody2.Text = "Spending: 300";
square150x150Content.TextBody3.Text = "Earnings: 200";
// Attach the Square150x150 template to the Wide310x150 template.
wide310x150Content.Square150x150Content = square150x150Content;
// Attach the Wide310x150 template to the Square310x310 template.
tileContent.Wide310x150Content = wide310x150Content;
// Send the notification to the application? tile.
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
This will show the tile correctly in the 150x150 size, but not in the wide. Then only the text is shown but not the image.
Here's a screenshot:
Thanks for your help!
NPadrutt
The SmallImageAndText Templates only show an Image on Windows, not on Phone.
Please refer to the template catalog for the direfences here.
Tipp: Template catalog is available at aka.ms/tiletemplates
I'm working with Bokeh and I want to add a circle on a specific position on my image.
For the moment, I create my image like this :
img = image(image=[data],
x_range=[0, x_range],
y_range=[0, y_range],
x=x,
y=y,
dw=dw,
dh=dh,
tools=TOOLS,
palette=["Greys-9"],
title=title,
plot_width=plot_width,
plot_height=plot_height,
)
circle(x=10,y=10,radius=100,fill_color="#df1c1c",line_color="#df1c1c")
resources = Resources("inline")
plot_script, plot_div = components(img, resources)
html_script = encode_utf8(plot_script)
html_div = encode_utf8(plot_div)
hold()
figure()
return html_script, html_div
and send this to my HTML page.
The problem is that the circle is not on the final display. Maybe on background ? I don't know...
I tryed add function, add_glyph function, add_layout... None of these are functionnal!
Thanks for helping guys
The above code did not work due to a bug in Bokeh. However, the bug has since been fixed, and that code and code similar to it will function as expected.
I have a phantomjs script that is stepping through the pages of my site.
For each page, I use page = new WebPage() and then page.close() after finishing with the page. (This is a simplified description of the process, and I'm using PhantomJS version 1.9.7.)
While on each page, I use page.renderBase64('PNG') one or more times, and add the results to an array.
When I'm all done, I build a new page and cycle through the array of images, adding each to the page using <img src="data:image/png;base64,.......image.data.......">.
When done, I use page.render(...) to make a PDF file.
This is all working great... except that the images stop appearing in the PDF after about the 20th image - the rest just show as 4x4 pixel black dots
For troubleshooting this...
I've changed the render to output a PNG file, and have the same
problem after the 19th or 20th image.
I've outputted the raw HTML. I
can open that in Chrome, and all the images are visible.
Any ideas why the rendering would be failing?
Solved the issue. Turns out that PhantomJS was still preparing the images when the render was executed. Moving the render into the onLoadFinished handler, as illustrated below, solved the issue. Before, the page.render was being called immediately after the page.content = assignment.
For those interested in doing something similar, here's the gist of the process we are doing:
var htmlForAllPages = [];
then, as we load each page in PhantomJS:
var img = page.renderBase64('PNG');
...
htmlForAllPages.push('<img src="data:image/png;base64,' + img + '">');
...
When done, the final PDF is created... We have a template file ready, with all the required HTML and CSS etc. and simply insert our generated HTML into it:
var fs = require('fs');
var template = fs.read('DocumentationTemplate.html');
var finalHtml = template.replace('INSERTBODYHERE', htmlForAllPages.join('\n'));
var pdfPage = new WebPage();
pdfPage.onLoadFinished = function() {
pdfPage.render('Final.pdf');
pdfPage.close();
};
pdfPage.content = finalHtml;
When I use:
loader = new MovieClipLoader();
_root.createEmptyMovieClip("level1",getNextHighestDepth());
_root.level1.createEmptyMovieClip("image",getNextHighestDepth());
loader.loadClip("http://someimage.jpg",_root.level1.image);
...it works and the image shows up.
But when I use:
loader = new MovieClipLoader();
_root.createEmptyMovieClip("level1",getNextHighestDepth());
_root.level1.createEmptyMovieClip("level2",getNextHighestDepth());
_root.level1.level2.createEmptyMovieClip("image",getNextHighestDepth());
loader.loadClip("http://someimage.jpg",_root.level1.level2.image);
...the image doesn't show up. Can anyone tell me why? How can I make this work?
the depth you are supplying is going to be '1'. is that what you are expecting?
each movie clip has it's own set of depths, so the nextHighestDepth() of the newly create 'image' mc, will be 1. it should not prevent loading the image though.
As gthmb says, you should call getNextHighestDepth() on the same MovieClip as you do the createEmptyMovieClip() on. So your code example should be more like:
loader = new MovieClipLoader();
_root.createEmptyMovieClip("level1",_root.getNextHighestDepth());
_root.level1.createEmptyMovieClip("level2",_root.level1.getNextHighestDepth());
_root.level1.level2.createEmptyMovieClip("image",_root.level1.level2.getNextHighestDepth());
loader.loadClip("http://someimage.jpg",_root.level1.level2.image);
Also, I would recommend storing references to the created MovieClips, so you won't have to use the full path in each occurrence in the code, something in the lines of this:
loader = new MovieClipLoader();
var level1:MovieClip = _root.createEmptyMovieClip("level1",_root.getNextHighestDepth());
var level2:MovieClip = level1.createEmptyMovieClip("level2",level1.getNextHighestDepth());
var image:MovieClip = level2.createEmptyMovieClip("image",level2.getNextHighestDepth());
loader.loadClip("http://someimage.jpg",image);