'How can i add an image into an existing and specific pdf page (Itext)?
I'm developing a "program" that create barcode and insert them on the original pdf.
This is my code:
package com.mycompany.barcodeinserter;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.imageio.ImageIO;
import net.sourceforge.barbecue.output.OutputException;
import org.krysalis.barcode4j.impl.int2of5.Interleaved2Of5Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
/**
*
* @author ale
*/
public class main {
public static void main(String[] args) throws OutputException {
try {
PrintWriter out = new PrintWriter("codici.txt");
PdfWriter writer = new PdfWriter("/Users/ale/Documents/natale.pdf");
PdfReader reader = new PdfReader("/Users/ale/Documents/natale.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
int z = reader.getNumberOfPages();
String page;
ArrayList<String> codici = new ArrayList<String>();
for (int i = 1; i <= z; i++) {
countchar contatore = new countchar();
page = PdfTextExtractor.getTextFromPage(reader, i);
Scanner scanner = new Scanner(page);
System.out.println("Pagina:" + i + "\n");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("cod")) {
String lineasucces = scanner.nextLine();
System.out.println(lineasucces.replaceAll("[^0-9]", ""));
//countchar contatoredue = new countchar(lineasucces.replaceAll("[^0-9]", ""));
switch (contatore.conta(line.replaceAll("[^0-9]", ""))) {
case 12:
System.out.println(contatore.usingSplitMethod(line.replaceAll("[^0-9]", ""), 6));
for (String element : contatore.usingSplitMethod(line.replaceAll("[^0-9]", ""), 6)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
default:
System.out.println(contatore.usingSplitMethod(line.replaceAll("[^0-9]", ""), 13));
for (String element : contatore.usingSplitMethod(line.replaceAll("[^0-9]", ""), 13)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
}
switch (contatore.conta(lineasucces.replaceAll("[^0-9]", ""))) {
case 12:
for (String element : contatore.usingSplitMethod(lineasucces.replaceAll("[^0-9]", ""), 6)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
default:
for (String element : contatore.usingSplitMethod(lineasucces.replaceAll("[^0-9]", ""), 13)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
}
/*
if (line.endsWith("")) {
System.out.println(contatore.usingSplitMethod(scanner.nextLine().replaceAll("[^0-9]", "").replaceAll("[^0-9]", ""), 13));
try {
switch (contatore.conta()) {
case 12:
System.out.println(contatore.usingSplitMethod(scanner.nextLine().replaceAll("[^0-9]", ""), 6));
for (String element : contatore.usingSplitMethod(scanner.nextLine().replaceAll("[^0-9]", ""), 6)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
default:
System.out.println(contatore.usingSplitMethod(scanner.nextLine().replaceAll("[^0-9]", ""), 13));
for (String element : contatore.usingSplitMethod(scanner.nextLine().replaceAll("[^0-9]", ""), 13)) {
if ((element != "") && (element.length() > 4)) {
codici.add(element);
}
}
break;
}
} catch (NoSuchElementException e) {
System.out.println(e);
}
}
*/
}
}
}
//BarcodeGenerator generator;
int i = 0;
for (String element : codici) {
//generator = new BarcodeGenerator(EncodeTypes.EAN_13, "" + element);
//generator.getParameters().setResolution(400);
//generator.save("" + element + ".png");
out.println(element);
i++;
}
out.close();
File file2 = new File("codici.txt");
BufferedReader br = new BufferedReader(new FileReader(file2));
String st;
while ((st = br.readLine()) != null) {
Interleaved2Of5Bean bean = new Interleaved2Of5Bean();
bean.setHeight(10d);
bean.doQuietZone(false);
OutputStream out2 = new java.io.FileOutputStream(new File(st + ".png"));
BitmapCanvasProvider provider = new BitmapCanvasProvider(out2, "image/x-png", 110, BufferedImage.TYPE_BYTE_GRAY, false, 0);
bean.generateBarcode(provider, st);
provider.finish();
BufferedImage barcodeImage = provider.getBufferedImage();
ImageIO.write(barcodeImage, "png", out2);
}
System.out.println("\n\n");
System.out.println(codici);
System.out.println(i);
while ((st = br.readLine()) != null) { //here i tried to insert that but i'm stuck
for (int j = 1; j <= z ; j++) {
String page2 = PdfTextExtractor.getTextFromPage(reader, j);
if (page2.contains(st)) {
ImageData imgdata = ImageDataFactory.create(st+".png");
Image image1 = new Image(imgdata);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I figured out how to create them but i didn't found anything on internet that helps me to add it into a specific page but only into all of the pages or only into a new page (which is not my purpose). Thank you for the helping (sorry if my english is not perfect :) ).
Solution 1:[1]
Here is an example how you can add either a barcode or an image to the existing page
import com.itextpdf.barcodes.BarcodePDF417;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.wmf.WmfImageData;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
String filename = "barcodeOnCustomPage.pdf";
PdfReader reader = new PdfReader(filename);
PdfWriter writer = new PdfWriter("out_" + filename);
PdfDocument document = new PdfDocument(reader, writer);
//Add barcode to the existing page
String text = "Sample text for barcode";
BarcodePDF417 barcode = new BarcodePDF417();
barcode.setCode(text);
barcode.placeBarcode(new PdfCanvas(document.getPage(1)), null);
//Add image to the existing page
new PdfCanvas(document.getPage(1)).addImageFittedIntoRectangle(new WmfImageData("example.wmf"),
new Rectangle(200, 0, 0.1f, 0.1f), false);
document.close();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dmitry Radchuk |