Javascript required
Skip to content Skip to sidebar Skip to footer

Upload High Quality Photo Programmatically Stack Overflow

Of all the methods for adding an image to a pdf document that I tried (and I tired a lot of them, including most of the residuum of those mentioned on this page), the merely 1 that consistently produced high-quality output is a method using LaTeX, which I describe below. You don't demand to know anything about LaTeX beyond what I will say here. You lot don't even have to have a working LaTeX installation on your computer: y'all tin use the completely free online LaTeX editor Overleaf.

This is a feasible solution provided you are willing to get rid of all interactive features in your pdf document: fillable grade fields, highlights, comments, links, bookmarks, etc. If your original pdf document already has some interactive content, you will commencement need to produce a 'plain' version of information technology, i.e. a version that doesn't have any interactive features.

Y'all can produce a 'plain' pdf by e.g. press your original pdf document to a new pdf file (see here). This way, whatsoever e.g. interactive forms you filled out will exist present equally regular text in the printed, 'plain' pdf. Now y'all will be able to use LaTeX to add a picture to this 'plain' pdf. The resulting pdf file (produced by LaTex) volition besides be 'plain'. (It might be possible to restore the interactive features using something chosen pax, see e.g. here, but I haven't tried it myself.)

Why LaTeX-based solutions cannot preserve interactive features of the original pdf document is explained hither.
If your input pdf is not 'manifestly', all the content in the interactive features will exist ignored past LaTeX. So if e.g. yous take put some text into a fillable class field, the document produced past LaTeX would not show information technology—information technology would bear witness a blank.

Autonomously from the limitation of not being able to include interactive features in the output, this solution is potentially very powerful, assuasive e.thou. rescaling and rotating of images, and programmatic insertion of multiple images on multiple pages. (Though I will not provide details for the last one, I will requite an indication of how it can be washed).

In add-on, this solution is otherwise unlimited, completely free, and open source. And, as I said, information technology should innovate no degradation in quality compared to your input files.

(Likewise the method described here, there are really many means to exercise this in LaTeX; come across here.)

Procedure

The goal is to accept a 'plainly' ten-folio pdf file, in our case one called 'sample_pdf_letter_format.pdf', and add a certain png prototype in the center of page 5. That image will be this png of a 'blue globe', which has a transparent groundwork:

enter image description here

(I've downloaded that png from here.)

I will draw the procedure assuming you lot are working with pdf in letter format. Adapting this to A4 format requires only minimal modification; at any charge per unit, I will be providing scripts for both.

I will explicate everything using Overleaf, but of form everything can be done using a LaTeX installation on your own figurer, or using some other online LaTeX editor.

I have enabled sharing for a 'project' on Overleaf. This project contains sample files on which I will explain what to do. You will exist able to view and download all the files, and also to 'compile' any files whose names end with`.tex'.

However, you will not be able to upload whatever files or to alter the existing ones.

In guild to add your ain images to your own pdf files, all you demand to do is sign up for a free Overleaf account and upload all the relevant files to your project (including the relevant .tex file from my project). Alternatively, utilize a LaTeX installation on your ain calculator.

So, click here.

Once you click on that link, you should be seeing the Overleaf workspace, like this:

enter image description here

On the left you can see a bunch of files. Ane of them should be highlighted. Make sure that information technology is 'main_letter_format.tex' (click on it if it is not). In one case that file is highlighted, its contents should appear in the middle part of the screen. On the right function of the screen, you lot should come across a nicely formatted certificate with the championship 'A sample pdf document'; if you lot do not, press the push 'Recompile' that'due south higher up this office of the screen. In the end, the folio should wait like the to a higher place screencap.

On the right side of the screen, scroll the document down to page five. You should run across this:

enter image description here

The bluish world,

enter image description here,

is the png that is superimposed on the pdf.

You tin can download the whole output pdf (with the png added to it) past clicking on the following icon, to the right of the 'Recompile' button:

enter image description here

Now let'south see what this 'project' is really doing.

Our original pdf, to which the image is to be added, is 'sample_pdf_letter_format.pdf'. The prototype to be added is called 'blue_globe.png'. That's the 'blue world' I showed above, with a transparent background.

These 2 files were uploaded to Overleaf using the upload button on the left-paw role of the screen (you lot won't see it correct now—you would need to be logged on to your own Overleaf project to exist able to upload files):

enter image description here

At present let'due south expect at the file 'main_letter_format.tex', which contains the actual LaTeX code that accomplishes the addition of the image to the pdf document. (For completeness, I accept too reproduced the content of that file here; meet below.)

Note that the names of the original pdf certificate ('sample_pdf_letter_format.pdf') and of the image file ('blue_globe.png') are entered in lines 12 and 13:

          \newcommand{\originalPDFfilename}{sample_pdf_letter_format.pdf} \newcommand{\imagename}{blue_globe.png}                  

Presume nosotros want to add together the image file on page 5 of the pdf document. Then we first include, without modification, pages i-4 of the original pdf file. This is washed in line 16:

\includepdf[pages=ane-4]{\originalPDFfilename}.

Similarly, we volition include pages 6-10 at the end, in line 27:

\includepdf[pages=six-last]{\originalPDFfilename}.

(Note that y'all can use the keyword last instead of explicitly proverb which folio is last.)

Now we deal with page five in lines 17-26.

The page number is specified in line 26:

,pages=5]{\originalPDFfilename}.

Note that line 20 is 'commented out' by starting information technology with '%':

%\includegraphics[scale=one]{gridlines_letter_bp.png} %comment out before producing the final version

If y'all remove the initial '%' and recompile, y'all will see grid lines on page 5. (To exist able to actually remove the initial '%', you need to be logged into your own Overleaf account and practise it in your own copy of this file.) Such grid lines may exist helpful to position the image on the page. This is what they look similar:

enter image description here

At present, the spacing of the filigree lines is in units of 'bp', which are PostScript points (1/72 of an inch). I take also produced (see below for an explanation of how) grid line files with spacing in inches ('gridlines_letter_in.png') and millimeters ('gridlines_letter_mm.png'). For A4 format, I produced grid line files with spacings in bp and in millimeters. If you prefer some of these other filigree lines, but substitute their respective file names in line 20. Only brand sure to over again comment out line twenty (and recompile) earlier y'all download the final certificate, then that the filigree lines practise non announced in it.

The bodily insertion of the image happens in lines 22-24:

          \begin{textblock*}{0in}[0,1](-15pt+300bp,11in-500bp) \includegraphics[width=1in,origin=c,angle=0]{\imagename} \finish{textblock*}                  

If you expect at the screenshot with filigree lines (above), you will see that the png containing the 'blue globe' is 72 bp ( = 1 in) wide, extending from 300 bp to 372 bp. The width is specified in width=1in in line 23, and the height is scaled so that the aspect ratio is the same as in the original prototype. Alternatively, yous could provide the height (tiptop=1in), and so the width will be scaled. You lot tin can besides provide both width and height (generally, this will distort the image, but sometimes that is useful). Another fashion to resize the epitome is by using the command 'scale='; for example, 'scale=0.1' makes the image size 10% of its original size. Finally, one can rotate the image by specifying angle in degrees (the rotation is about the center of the image). For fifty-fifty more than details, see here.

That takes care of the size and rotation of the image. Its placement on the page is done in line 22:

\begin{textblock*}{0in}[0,1](-15pt+300bp,11in-500bp).

The {0in} is irrelevant for us. The [0,ane] part says that what we volition exist positioning—the 'reference point' of the image—is the lower-left corner of the image.

The default reference indicate is the upper-left corner. The 2 numbers in brackets specify by how much, in units of width and top of the paradigm, you desire to displace the reference bespeak. Then [0.ii,0.7] would mean that the reference point is 20% of the prototype width to the right, and 70% of the prototype height beneath, the upper-left corner.

Now comes -15pt+300bp,11in-500bp. The -15pt and 11in are offsets. The meaning of the latter ane is clear: that's the height of letter-sized paper. On the other hand, I don't know where the -15pt comes from; I found out its value by experimenting.

The unit 'pt' is the 'TeX signal', equal to 1/72.27 inch, whereas the 'bp' is 1/72 inch. In Adobe InDesign, the pt is called the 'traditional printer point', encounter hither.

In the stop, if you put

\begin{textblock*}{0in}[0,1](-15pt+0bp,11in-0bp)

so the bluish world will appear in the lower-left corner of the page.

Therefore, -15pt+300bp,11in-500bp means that the reference point of the image (which nosotros accept set to be its lower-left corner) is 300 bp to the correct from the left edge of the page, and 500 bp in a higher place the lower border of the page (as you tin verify on the screenshot with filigree lines).

Why '11in-500bp'? LaTeX likes to measure things from the upper left corner, merely the grid lines that I accept all have their origin at the lower left. Thus, if nosotros want to be 500 bp higher up the lower edge, we demand to put 11in-500bp; putting just 500bp would instead place the reference betoken 500 bp below the upper border of the page.

More images on more pages

If you want to include more than than ane paradigm on page 5, for each extra prototype yous would need to add, starting in line 25, another block like that in lines 22-24 (one block for each actress image). Of course, instead of \imagename you'd use the names of the other images, instead of 300bp and 500bp y'all'd use the appropriate coordinates for the new images, and instead of 'width=1in' and 'bending=0' you'd employ the widths and angles advisable for those images.

If y'all want to add the same prototype(s) to east.g. pages 5-seven, then yous'd alter line 26 to read
,pages=5-vii]{\originalPDFfilename}
and line 27 to read
includepdf[pages=viii-final]{\originalPDFfilename}.

If the pages on which y'all want to add images are not consecutive, then yous'd need to produce carve up commands for each page. For case, imagine you lot want to add together the same bluish earth (in the same place) to all even-numbered pages. At nowadays, inside LaTex, I only know how to exercise it using 'fauna force', i.east.

          \includepdf[pages=1]{\originalPDFfilename} \includepdf[pagecommand= {…},pages=ii]{\originalPDFfilename}  \includepdf[pages=iii]{\originalPDFfilename} \includepdf[pagecommand= {…},pages=4]{\originalPDFfilename}  …                  

Here in pagecommand= {…} stands for the block in lines 22-24.

Of course, ane can certainly write a script (in unix fustigate, or in python, or in perl, or whatsoever) that will programmatically make an appropriate .tex file.

A more than elegant (and potentially more powerful) option is to switch the compiler from pdfLaTeX to LuaLaTex, which enables one to add scripts in the Lua scripting language directly to the .tex file. In Overleaf, the alter of the compiler is done like this: Menu->Compiler->LuaLaTeX. The LuaLaTeX will compile all standard LaTeX files normally (in particular, our 'main_letter_format.tex' will compile normally, albeit more than slowly), but it additionally allows you to (amid other things) add together code in the Lua scripting linguistic communication. Using Lua lawmaking, one should exist able to do programmatically things like the above. (At to the lowest degree I assume Lua scripting would enable one to do this, based on examples such as those hither; I actually have zero feel with Lua scripts.)

Modifications for A4 format

Click on 'main_A4_format.tex'. You should exist able to compile information technology past clicking on the 'Recompile' button. The but differences are that 1 should supercede letterpaper by a4paper in lines i and two, and one should supersede 11in past 297mm in lines 19 and 22.

And that'southward it: all yous have to do is supply your own source certificate, your own image, and your own specifications for the epitome size, orientation and position.

The LaTeX codes

For completeness, here is the content of the file 'main_letter_format.tex'. The modifications for A4 format are every bit explained above.

          \documentclass[letterpaper]{commodity} \usepackage[letterpaper,left=0in,correct=0in,elevation=0in,bottom=0in]{geometry} \usepackage{graphicx} \usepackage{pdfpages} \usepackage[absolute,overlay]{textpos} \usepackage{calc}   \begin{certificate}   \newcommand{\originalPDFfilename}{sample_pdf_letter_format.pdf} \newcommand{\imagename}{blue_globe.png}   \includepdf[pages=1-iv]{\originalPDFfilename} \includepdf[pagecommand= { \begin{textblock*}{0in}[0,1](-15pt+0in,11in) %\includegraphics[scale=one]{gridlines_letter_bp.png} %annotate out before producing the final version \end{textblock*} \begin{textblock*}{0in}[0,1](-15pt+300bp,11in-500bp) \includegraphics[width=1in,origin=c,angle=0]{\imagename} \end{textblock*} } ,pages=5]{\originalPDFfilename} \includepdf[pages=6-last]{\originalPDFfilename} \cease{document}                  

The grid lines were produced using this code (which I found here):

          \documentclass[letterpaper]{article} \usepackage{xcolor} \usepackage[grid, gridunit=bp, gridcolor=bluish!40, subgridcolor=blue!twenty]{eso-picture}  \brainstorm{document} \thispagestyle{empty} \mbox{} %need to have *something* on a page to return it, so nosotros put an empty box of zero width \finish{document}                  

One time the pdf with the grid lines is produced, you lot should convert it to a png prototype file with white replaced by transparency. This can exist done using the free program GIMP (GNU Image Manipulation Program):

  1. Open the pdf folio containing the filigree lines in GIMP. You tin can right-click on it and select 'Open With', and then click on 'GNU Image Manipulation Program'.
  2. You volition exist prompted to import pdf. Click the 'Import' button.
  3. Make sure in that location is a transparent layer: Layer->Transparency->Add Alpha Channel. In fact, 'Add Alpha Aqueduct' should be greyed out, which ways that the alpha aqueduct (i.e. the transparent layer) is already present. If so, exit this dialog past clicking anywhere else on the screen. If 'Add together Blastoff Aqueduct' is not greyed out, click on it.
  4. Colors->Color to Alpha… . In the new window dialog, make certain the 'Color' field is set to white, that the 'Transparency threshold' is 0, and that the 'Opacity threshold' is 1. If so, click 'OK'.
  5. File->Consign Equally… . Make sure you change the extension of the name to '.png'. Click 'Consign' button. A new dialog window will appear; click 'Consign' in that window, also.

earsmanheary1944.blogspot.com

Source: https://askubuntu.com/questions/167274/how-can-i-edit-a-picture-into-an-existing-pdf-file