Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Base64;
- import java.util.Random;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Image;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.PdfStamper;
- public class PDFmanipulate {
- public static final String DEST = "C:\\Users\\BallZaR5R5\\Desktop\\java-project\\testItext\\";
- public String signPDF(int page, byte[] pdfData, byte[] pictureData, float llx, float lly, float urx, float ury)
- throws IOException, DocumentException {
- String fileName = new PDFmanipulate().manipulatePdf(pdfData, pictureData, page, llx, lly, urx, ury);
- File file = new File(DEST + fileName);
- file.getParentFile().mkdirs();
- // byte[] input_file = Files.readAllBytes(Paths.get(DEST));
- // byte[] encodedBytes = Base64.getEncoder().encode(input_file);
- //
- // String pdfInBase64 = new String(encodedBytes);
- // System.out.println("originalString : " + pdfInBase64);
- // byte[] decodedBytes = Base64.getDecoder().decode(pdfInBase64.getBytes());
- // FileOutputStream fos = new
- // FileOutputStream("C:\\Users\\BallZaR5R5\\Desktop\\resume4.pdf");
- // fos.write(decodedBytes);
- // fos.flush();
- // fos.close();
- //
- byte[] pdfSign = Files.readAllBytes(file.toPath());
- byte[] encodedBytes = Base64.getEncoder().encode(pdfSign);
- String pdfInBase64 = new String(encodedBytes);
- // String Del = DEST + fileName;
- // Files.delete(Paths.get(Del));
- return pdfInBase64;
- }
- public String manipulatePdf(byte[] pdfData, byte[] pictureData, int page, float llx, float lly, float urx, float ury)
- throws IOException, DocumentException {
- PdfReader reader = new PdfReader(pdfData);
- reader.getNumberOfPages();
- float hight = reader.getPageSize(page).getHeight();
- float width = reader.getPageSize(page).getWidth();
- String filename = "Document" + "_" + new Random().nextInt(100000) + ".pdf";
- PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST + filename));
- Image image = Image.getInstance(pictureData);
- image.setAbsolutePosition((float) width * llx, (float) hight * lly);
- // image.scalePercent(50, 50);
- image.scaleToFit((float) width * urx, (float) hight * ury);
- PdfContentByte over = stamper.getOverContent(page);// pageNum
- over.addImage(image);
- stamper.close();
- reader.close();
- return filename;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement