Advertisement
Guest User

Untitled

a guest
May 27th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package th.in.oneauthen;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.util.Base64;
  10. import java.util.Random;
  11.  
  12. import com.itextpdf.text.DocumentException;
  13. import com.itextpdf.text.Image;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfReader;
  16. import com.itextpdf.text.pdf.PdfStamper;
  17.  
  18. public class PDFmanipulate {
  19.  
  20.     public static final String DEST = "C:\\Users\\BallZaR5R5\\Desktop\\java-project\\testItext\\";
  21.  
  22.     public String signPDF(int page, byte[] pdfData, byte[] pictureData, float llx, float lly, float urx, float ury)
  23.             throws IOException, DocumentException {
  24.  
  25.         String fileName = new PDFmanipulate().manipulatePdf(pdfData, pictureData, page, llx, lly, urx, ury);
  26.         File file = new File(DEST + fileName);
  27.         file.getParentFile().mkdirs();
  28.  
  29.         // byte[] input_file = Files.readAllBytes(Paths.get(DEST));
  30.         // byte[] encodedBytes = Base64.getEncoder().encode(input_file);
  31.         //
  32.         // String pdfInBase64 = new String(encodedBytes);
  33.  
  34.         // System.out.println("originalString : " + pdfInBase64);
  35.  
  36.         // byte[] decodedBytes = Base64.getDecoder().decode(pdfInBase64.getBytes());
  37.         // FileOutputStream fos = new
  38.         // FileOutputStream("C:\\Users\\BallZaR5R5\\Desktop\\resume4.pdf");
  39.         // fos.write(decodedBytes);
  40.         // fos.flush();
  41.         // fos.close();
  42.         //
  43.  
  44.         byte[] pdfSign = Files.readAllBytes(file.toPath());
  45.         byte[] encodedBytes = Base64.getEncoder().encode(pdfSign);
  46.         String pdfInBase64 = new String(encodedBytes);
  47.  
  48. //      String Del = DEST + fileName;
  49. //      Files.delete(Paths.get(Del));
  50.  
  51.         return pdfInBase64;
  52.  
  53.     }
  54.  
  55.     public String manipulatePdf(byte[] pdfData, byte[] pictureData, int page, float llx, float lly, float urx, float ury)
  56.             throws IOException, DocumentException {
  57.         PdfReader reader = new PdfReader(pdfData);
  58.         reader.getNumberOfPages();
  59.         float hight = reader.getPageSize(page).getHeight();
  60.         float width = reader.getPageSize(page).getWidth();
  61.         String filename = "Document" + "_" + new Random().nextInt(100000) + ".pdf";
  62.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST + filename));
  63.         Image image = Image.getInstance(pictureData);
  64.         image.setAbsolutePosition((float) width * llx, (float) hight * lly);
  65.         // image.scalePercent(50, 50);
  66.         image.scaleToFit((float) width * urx, (float) hight * ury);
  67.         PdfContentByte over = stamper.getOverContent(page);// pageNum
  68.         over.addImage(image);
  69.         stamper.close();
  70.         reader.close();
  71.         return filename;
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement