Good afternoon,
I am trying to insert a text and picture into a pdf file. My code so far looks like this
options orientation=portrait
papersize=A4
nodate
topmargin=.001in
bottommargin=.001in
leftmargin=.001in
rightmargin=.001in;
ods noproctitle;
ods escapechar="^";
title;
footnote;
ods pdf file="J:\Learning SAS\Example.pdf";
ods layout start width = 19cm height = 28cm ;
ods region y=2cm x=2cm width=6cm;
ods text="Some Random Information:";
ods region width=0.5cm height=0.25cm;
ods text= '^{style[preimage="C:/Users/WissMit/Desktop/example jpg.jpg"]}';
ods layout end;
ods pdf close;
And the Output looks like this:
Unfortunately, the PDF looks like this:
Somehow the .jpg is missing. Does anyone have a clue what I am missing? In several articles it always looks so easy... Furthermore, I would like to have control over the image size. Is there a way to control the size without adjusting it beforehand?
Give this code below a go, if it works fine. Otherwise we might need some more coding. The image will automatically scale to the size of the region you define. The bordercolor and borderwidth are not really need, it is just to show the size of the region. I would also recommend to only use the ODS PDF desination and no other.
options orientation=portrait
papersize=A4
nodate
topmargin=.001in
bottommargin=.001in
leftmargin=.001in
rightmargin=.001in;
ods noproctitle;
ods escapechar="^";
title;
footnote;
ods pdf file="c:\temp\sas2pdf_example.pdf";
ods layout start width = 19cm height = 28cm ;
ods region y=2cm x=2cm width=6cm;
ods text="Some Random Information:";
ods region y=4cm x=2cm width=2cm height=2cm style={bordercolor=red borderwidth=10px backgroundimage="c:\temp\bird.jpg" };
ods text=" ";
ods layout end;
ods pdf close;
.
Hi:
I notice that you are writing the PDF to your J drive but the image is on your C: drive. Did you have error messages in your log? Perhaps something like this or another warning that indicates output will be suppressed?
WARNING: The absolute region was too small to accommodate the output supplied for the PDF
destination. Output will be suppressed.
That's usually what causes no output to be produced. When I tried a simplified form of your code (switching CM to IN and using my image, I was able to create PDF output as shown below.
When I use ODS TEXT to a specific destination, I like to be sure to use ODS PDF TEXT= or ODS RTF TEXT= to make sure that I am not accidentally writing my text to a destination that's open, but isn't the ultimate output I want. As you can see in the code, I'm writing the output to one folder, but getting the image from another folder. I only had a PNG to use, but it worked for me with simplified and tweaked code. The only other thing I added was a {NEWLINE 1} after the first line of text. I usually do that because I like to have control over the line feeds.
I do not think you can use your ODS statements to resize the image. My experience with using preimage is that I need to make sure the image is the size I want before using the image in the PREIMAGE style override. You might wish to look in the documentation or open a track with Tech Support.
Cynthia
Hi Cynthia,
thank you for your reply! Your code seems to work aswell, but I am not able to customize the image size (as you already stated) so I am sticking with brunos approach. Anyways, thank you for the hints with the ods pdf text statement 🙂
Kind regards,
Jakob
Give this code below a go, if it works fine. Otherwise we might need some more coding. The image will automatically scale to the size of the region you define. The bordercolor and borderwidth are not really need, it is just to show the size of the region. I would also recommend to only use the ODS PDF desination and no other.
options orientation=portrait
papersize=A4
nodate
topmargin=.001in
bottommargin=.001in
leftmargin=.001in
rightmargin=.001in;
ods noproctitle;
ods escapechar="^";
title;
footnote;
ods pdf file="c:\temp\sas2pdf_example.pdf";
ods layout start width = 19cm height = 28cm ;
ods region y=2cm x=2cm width=6cm;
ods text="Some Random Information:";
ods region y=4cm x=2cm width=2cm height=2cm style={bordercolor=red borderwidth=10px backgroundimage="c:\temp\bird.jpg" };
ods text=" ";
ods layout end;
ods pdf close;
.
Thank you so much Bruno! That worked for me 🙂 But I've still got two questions as I am not sure if I understand everything thats going on in the code:
- Why do I need to include the 'ods text = " " ' and 'backgroundimage' statement? I am a bit frustrated that the code didnt work out with 'pre/postimage' and I do not get why it doesn't. I would really like to understand what the ods text statement is doing there (as it doesn't work without it)
- May seem a bit redundant as I was the one putting it there in the first place, but: Do I need the whole 'options' to 'footnote' part? The results appear to be the same and I was just following instructions out of an article (I attached it, in case anyones interested, see p. 4)
Just for curiosity sake, why are your paper margins in inches when all the other measures are in CM?
Make sure the path to your image is correct. Double-check the file location, and maybe try using a relative path. Consider the image format – the code suggests a .jpg, but the file extension in your text might be a typo (".jpg.jpg"). Ensure consistency. As for controlling image size without pre-adjusting, you can try the width and height options in your ods text statement.
For example: scss Copy code ods text='^{style[preimage="C:/Users/WissMit/Desktop/example.jpg" width=200px height=150px]}'; Feel free to experiment with those values to get the desired size.
And when you need to compress image, you can just use JPEG Optimizer tools or functions within SAS.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.