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:
Similar approach is working fine for the text but not the images. Any help is appreciated.
java image itext itext7
add a comment |
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:
Similar approach is working fine for the text but not the images. Any help is appreciated.
java image itext itext7
"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
add a comment |
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:
Similar approach is working fine for the text but not the images. Any help is appreciated.
java image itext itext7
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:
Similar approach is working fine for the text but not the images. Any help is appreciated.
java image itext itext7
java image itext itext7
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
add a comment |
"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
add a comment |
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
.
It is working now. I appreciate your immediate help.
– Kishore Penmetsa
Nov 14 at 17:10
add a comment |
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
.
It is working now. I appreciate your immediate help.
– Kishore Penmetsa
Nov 14 at 17:10
add a comment |
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
.
It is working now. I appreciate your immediate help.
– Kishore Penmetsa
Nov 14 at 17:10
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
"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