How to bring image to the front(of the text/image) or send the image to the back((of the text/image)) in IText7 using java?









up vote
2
down vote

favorite












How to bring image to the front(of the text/image) or send the image to the back((of the text/image)) in IText7(7.0.8) using Java?



import java.io.FileNotFoundException;
import java.io.IOException;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

public class AddImageUnderlayToPDF
public static void main(String args) throws FileNotFoundException, IOException
PdfDocument pdfDoc = new PdfDocument(new PdfReader("c:\Development\test.pdf"),
new PdfWriter("c:\Development\test_result.pdf"));
ImageData img = ImageDataFactory.create("c:\Development\kishore signature.png");
PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
under.addImage(img, 100, 0f, 0f, 100, 100, 300, false);
under.saveState();
pdfDoc.close();




..but it doesn't work, it doesn't show the image in the pdf. I also I noticed an error while opening the pdf:



Acrobat pdf error



Similar approach is working fine for the text but not the images. Any help is appreciated.










share|improve this question























  • "but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
    – mkl
    Nov 10 at 7:13










  • Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
    – Kishore Penmetsa
    Nov 13 at 15:31














up vote
2
down vote

favorite












How to bring image to the front(of the text/image) or send the image to the back((of the text/image)) in IText7(7.0.8) using Java?



import java.io.FileNotFoundException;
import java.io.IOException;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

public class AddImageUnderlayToPDF
public static void main(String args) throws FileNotFoundException, IOException
PdfDocument pdfDoc = new PdfDocument(new PdfReader("c:\Development\test.pdf"),
new PdfWriter("c:\Development\test_result.pdf"));
ImageData img = ImageDataFactory.create("c:\Development\kishore signature.png");
PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
under.addImage(img, 100, 0f, 0f, 100, 100, 300, false);
under.saveState();
pdfDoc.close();




..but it doesn't work, it doesn't show the image in the pdf. I also I noticed an error while opening the pdf:



Acrobat pdf error



Similar approach is working fine for the text but not the images. Any help is appreciated.










share|improve this question























  • "but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
    – mkl
    Nov 10 at 7:13










  • Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
    – Kishore Penmetsa
    Nov 13 at 15:31












up vote
2
down vote

favorite









up vote
2
down vote

favorite











How to bring image to the front(of the text/image) or send the image to the back((of the text/image)) in IText7(7.0.8) using Java?



import java.io.FileNotFoundException;
import java.io.IOException;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

public class AddImageUnderlayToPDF
public static void main(String args) throws FileNotFoundException, IOException
PdfDocument pdfDoc = new PdfDocument(new PdfReader("c:\Development\test.pdf"),
new PdfWriter("c:\Development\test_result.pdf"));
ImageData img = ImageDataFactory.create("c:\Development\kishore signature.png");
PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
under.addImage(img, 100, 0f, 0f, 100, 100, 300, false);
under.saveState();
pdfDoc.close();




..but it doesn't work, it doesn't show the image in the pdf. I also I noticed an error while opening the pdf:



Acrobat pdf error



Similar approach is working fine for the text but not the images. Any help is appreciated.










share|improve this question















How to bring image to the front(of the text/image) or send the image to the back((of the text/image)) in IText7(7.0.8) using Java?



import java.io.FileNotFoundException;
import java.io.IOException;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

public class AddImageUnderlayToPDF
public static void main(String args) throws FileNotFoundException, IOException
PdfDocument pdfDoc = new PdfDocument(new PdfReader("c:\Development\test.pdf"),
new PdfWriter("c:\Development\test_result.pdf"));
ImageData img = ImageDataFactory.create("c:\Development\kishore signature.png");
PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
under.addImage(img, 100, 0f, 0f, 100, 100, 300, false);
under.saveState();
pdfDoc.close();




..but it doesn't work, it doesn't show the image in the pdf. I also I noticed an error while opening the pdf:



Acrobat pdf error



Similar approach is working fine for the text but not the images. Any help is appreciated.







java image itext itext7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 17:17









mkl

51.7k1165142




51.7k1165142










asked Nov 9 at 19:07









Kishore Penmetsa

204




204











  • "but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
    – mkl
    Nov 10 at 7:13










  • Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
    – Kishore Penmetsa
    Nov 13 at 15:31
















  • "but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
    – mkl
    Nov 10 at 7:13










  • Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
    – Kishore Penmetsa
    Nov 13 at 15:31















"but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
– mkl
Nov 10 at 7:13




"but it doesn't work." - in which way does it not work? Does the image not show up at all? Or does it show up but not under everything? Or does it fail some other way?
– mkl
Nov 10 at 7:13












Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
– Kishore Penmetsa
Nov 13 at 15:31




Image doesn't show up. Also I see an error while opening in Acrobat. I updated my post with more detailed info.
– Kishore Penmetsa
Nov 13 at 15:31












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










The error is the same as in your earlier question: You use a throw-away resources object, so the image resource is missing in the result.



You can fix this by using the actual page resources. Simply replace



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);


by



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), pdfDoc.getFirstPage().getResources(), pdfDoc);


Furthermore, drop the



under.saveState();


line as saveState only makes sense if you later use a matching restoreState.






share|improve this answer




















  • It is working now. I appreciate your immediate help.
    – Kishore Penmetsa
    Nov 14 at 17:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231903%2fhow-to-bring-image-to-the-frontof-the-text-image-or-send-the-image-to-the-back%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










The error is the same as in your earlier question: You use a throw-away resources object, so the image resource is missing in the result.



You can fix this by using the actual page resources. Simply replace



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);


by



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), pdfDoc.getFirstPage().getResources(), pdfDoc);


Furthermore, drop the



under.saveState();


line as saveState only makes sense if you later use a matching restoreState.






share|improve this answer




















  • It is working now. I appreciate your immediate help.
    – Kishore Penmetsa
    Nov 14 at 17:10














up vote
0
down vote



accepted










The error is the same as in your earlier question: You use a throw-away resources object, so the image resource is missing in the result.



You can fix this by using the actual page resources. Simply replace



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);


by



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), pdfDoc.getFirstPage().getResources(), pdfDoc);


Furthermore, drop the



under.saveState();


line as saveState only makes sense if you later use a matching restoreState.






share|improve this answer




















  • It is working now. I appreciate your immediate help.
    – Kishore Penmetsa
    Nov 14 at 17:10












up vote
0
down vote



accepted







up vote
0
down vote



accepted






The error is the same as in your earlier question: You use a throw-away resources object, so the image resource is missing in the result.



You can fix this by using the actual page resources. Simply replace



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);


by



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), pdfDoc.getFirstPage().getResources(), pdfDoc);


Furthermore, drop the



under.saveState();


line as saveState only makes sense if you later use a matching restoreState.






share|improve this answer












The error is the same as in your earlier question: You use a throw-away resources object, so the image resource is missing in the result.



You can fix this by using the actual page resources. Simply replace



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);


by



PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), pdfDoc.getFirstPage().getResources(), pdfDoc);


Furthermore, drop the



under.saveState();


line as saveState only makes sense if you later use a matching restoreState.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 17:36









mkl

51.7k1165142




51.7k1165142











  • It is working now. I appreciate your immediate help.
    – Kishore Penmetsa
    Nov 14 at 17:10
















  • It is working now. I appreciate your immediate help.
    – Kishore Penmetsa
    Nov 14 at 17:10















It is working now. I appreciate your immediate help.
– Kishore Penmetsa
Nov 14 at 17:10




It is working now. I appreciate your immediate help.
– Kishore Penmetsa
Nov 14 at 17:10

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231903%2fhow-to-bring-image-to-the-frontof-the-text-image-or-send-the-image-to-the-back%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo