* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.matco.pdfcutter;
/**
*
* @author prapop
*/
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodeQRCode;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
public class Pdfcutter
{
public static void main(String[] args)
throws IOException, DocumentException
{
String way = args[0];
if (way.equals("0"))
{
System.out.println("splite " + way);
new Pdfcutter().manipulatePdf1(args);
}
if (way.equals("1"))
{
/*remove blank page and add barcode TMT*/
System.out.println("remove blank " + way);
new Pdfcutter().manipulatePdf(args);
}
if (way.equals("2"))
{
/*remove blank page and add barcode TMT Special*/
System.out.println("remove blank " + way);
new Pdfcutter().manipulatePdf2(args);
}
if (way.equals("3"))
{
/*remove blank page and add barcode Nissan*/
System.out.println("remove blank " + way);
new Pdfcutter().manipulatePdf(args);
}
}
public void manipulatePdf(String[] args)
throws DocumentException, IOException
{
String SRC = args[1];
String DEST = args[2];
String textFile = args[3];
Rectangle pagesize = new Rectangle(595.0F, 280.0F);
Document document = new Document(pagesize, 36.0F, 72.0F, 108.0F, 180.0F);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
PdfReader reader = new PdfReader(SRC);
int n = reader.getNumberOfPages();
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
File file = new File(textFile);
int y = 0;
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
int x = 0;
String line;
while ((line = br.readLine()) != null)
{
y+=1;
while (y > 0)
{
x += 1;
TextExtractionStrategy strategy = (TextExtractionStrategy)parser.processContent(x, new SimpleTextExtractionStrategy());
String str = strategy.getResultantText();
str = str.toUpperCase();
int c = str.split("ARRIVAL TIME").length - 1;
if (c != 3)
{
System.out.println("page " + x);
System.out.println("C " + c);
System.out.println("line " + line);
}
if ((x % 3 == 1) && (c == 1)) {
System.out.println("1page " + x);
System.out.println("1C " + c);
System.out.println("1line " + line);
break;
}
if ((x % 3 > 0) && (c == 2)) {
System.out.println("2page " + x);
System.out.println("2C " + c);
System.out.println("2line " + line);
break;
}
if (c == 3) {
break;
}
}
PdfImportedPage template = writer.getImportedPage(reader, x);
Image tmpImage = Image.getInstance(template);
tmpImage.setAbsolutePosition(0.0F, 0.0F);
document.add(Image.getInstance(tmpImage));
BarcodeQRCode barcodeQRCode = new BarcodeQRCode(line, 40, 40, null);
tmpImage = barcodeQRCode.getImage();
tmpImage.setAbsolutePosition(25.0F, 90.0F);
document.add(tmpImage);
document.newPage();
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("liney " + y);
document.close();
}
public void manipulatePdf1(String[] args)
throws IOException, DocumentException
{
String SRC = args[1];
String DEST = args[2];
PdfReader reader = new PdfReader(SRC);
Rectangle pagesize = new Rectangle(595.0F, 280.0F);
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
PdfContentByte content = writer.getDirectContent();
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++)
{
PdfImportedPage page = writer.getImportedPage(reader, j);
float x = 0.0F;
for (int i = 0; i < 3; i++)
{
int month = (5 - i) % 3;
switch (month)
{
case 2:
x = -5.0F;
break;
case 1:
x = 0.0F;
break;
case 0:
x = 4.0F;
}
float y = -(pagesize.getHeight() * ((5 - i) % 3) + x);
content.addTemplate(page, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, y);
document.newPage();
}
}
document.close();
}
public void manipulatePdf2(String[] args)
throws DocumentException, IOException
{
String SRC = args[1];
String DEST = args[2];
String textFile = args[3];
Rectangle pagesize = new Rectangle(595.0F, 280.0F);
Document document = new Document(pagesize, 36.0F, 72.0F, 108.0F, 180.0F);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
PdfReader reader = new PdfReader(SRC);
int n = reader.getNumberOfPages();
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
File file = new File(textFile);
int y = 0;
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
int x = 0;
String line;
while ((line = br.readLine()) != null)
{
y+=1;
while (y > 0)
{
x += 1;
TextExtractionStrategy strategy = (TextExtractionStrategy)parser.processContent(x, new SimpleTextExtractionStrategy());
String str = strategy.getResultantText();
str = str.toUpperCase();
int c = str.split("ECI TAG").length - 1;
if (c != 3)
{
System.out.println("page " + x);
System.out.println("C " + c);
System.out.println("line " + line);
}
if ((x % 3 == 1) && (c == 1)) {
System.out.println("1page " + x);
System.out.println("1C " + c);
System.out.println("1line " + line);
break;
}
if ((x % 3 > 0) && (c == 2)) {
System.out.println("2page " + x);
System.out.println("2C " + c);
System.out.println("2line " + line);
break;
}
if (c == 3) {
break;
}
}
PdfImportedPage template = writer.getImportedPage(reader, x);
Image tmpImage = Image.getInstance(template);
tmpImage.setAbsolutePosition(0.0F, 0.0F);
document.add(Image.getInstance(tmpImage));
BarcodeQRCode barcodeQRCode = new BarcodeQRCode(line, 40, 40, null);
tmpImage = barcodeQRCode.getImage();
tmpImage.setAbsolutePosition(100.0F, 225.0F);
document.add(tmpImage);
document.newPage();
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("liney " + y);
document.close();
}
}