Advertisement
ballchaichana

PDFmanipulate

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