Last active
March 28, 2022 15:10
-
-
Save lharland/34be6e6087e394249db2d07ec6917966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PdfReader reader = new PdfReader(path); | |
Document document = new Document(); | |
*PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));* | |
PdfImportedPage page = writer.getImportedPage(reader, i); | |
*PdfImage img = Image.getInstance(page);* | |
reader.close(); | |
https://kb.itextpdf.com/home/it5kb/faq/how-to-reuse-a-page-from-one-pdf-document-into-another-pdf-document | |
http://what-when-how.com/itext-5/making-images-transparent-itext-5/ | |
http://javadox.com/com.itextpdf/itextpdf/5.5.8/com/itextpdf/text/pdf/PdfStamper.html | |
https://javadoc.io/doc/com.itextpdf/itextpdf/5.5.9/com/itextpdf/text/pdf/PdfStamper.html | |
https://www.programcreek.com/java-api-examples/?api=com.itextpdf.text.Image | |
https://stackoverflow.com/questions/29951821/itext-image-set-absolute-position | |
https://www.tutorialspoint.com/itext/itext_setting_font.htm | |
https://riptutorial.com/itext/example/20395/helloworldtable-java--itext-5- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ks = KeyStore.getInstance(KeyStore.getDefaultType()); | |
DataFolderDto dataFolder = dataFolderService.selectDataFolder(ServiceCenterConstants.NVRAM); | |
String filePath = dataFolder.getDataFolderMountedPath(); | |
File dir = new File(filePath); | |
String fileName = "ks.jks"; | |
File file = new File(filePath + File.separator + fileName); | |
char[] pwdArray = "password".toCharArray(); | |
if (file.exists()) { | |
ks.load(new FileInputStream(file), pwdArray); | |
} else { | |
ks.load(null, pwdArray); | |
} | |
PrivateKey key = (PrivateKey) ks.getKey("password", "password".toCharArray()); | |
Certificate[] chain = ks.getCertificateChain("password");*/ | |
// Creating the reader and the stamper | |
// PdfReader reader = new PdfReader(src); | |
// | |
// PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '\0', true); | |
// | |
// String fileName = rmaRecordForm.getRmaNumber() + "_" + DateUtility.formatDateForFileName(new Date()) + ".jpg"; | |
// String filePath = GaneshaConstants.EMPTY_STRING; | |
RMACustomerSignature rmaSignature = rmaInformationLogic.findRMACustomerSignature(rmaRecordForm.getRmaNumber()); | |
// if (rmaSignature != null) { | |
// logger.info("SIG " + rmaSignature.getFileLocation()); | |
// logger.info("FILENAME " + fileName); | |
// Image image = Image.getInstance(rmaSignature.getFileLocation()); | |
// PdfImage stream = new PdfImage(image, "", null); | |
// stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789")); | |
// PdfIndirectObject ref = stamper.getWriter().addToBody(stream); | |
// image.setDirectReference(ref.getIndirectReference()); | |
// image.setAbsolutePosition(36, 400); | |
// PdfContentByte over = stamper.getOverContent(1); | |
// over.addImage(image); | |
// stamper.close(); | |
// reader.close(); | |
// } | |
//Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI()); | |
// PdfDocument pdfDoc = | |
// | |
// new PdfDocument(new PdfReader(src), new PdfWriter(new Document(), new FileOutputStream(dest))); | |
//Document document = new Document(); | |
//PdfWriter.getInstance(document, new FileOutputStream(dest)); | |
//document.open(); | |
// Image img = Image.getInstance(rmaSignature.getFileLocation()); | |
// img.setAbsolutePosition(80, 800); | |
// pdfDoc.add(img); | |
// | |
// pdfDoc.close(); | |
/*PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); | |
PdfFormField field = PdfFormField.createSignature(stamper.getWriter()); | |
field.setFieldName(GaneshaConstants.SIGNATORY_NAME); | |
// set the widget properties | |
field.setWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_OUTLINE); | |
field.setFlags(PdfAnnotation.FLAGS_PRINT); | |
// add the annotation | |
stamper.addAnnotation(field, 1); | |
// close the stamper | |
PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); | |
// Creating the appearance | |
appearance.setVisibleSignature(GaneshaConstants.SIGNATORY_NAME); | |
appearance.setReason(""); | |
appearance.setLocation(""); | |
RmaLoadHelper rmaLoad = new RmaLoadHelper(); | |
// Creating the appearance for layer 0 | |
PdfTemplate n0 = appearance.getLayer(0); | |
float x = n0.getBoundingBox().getLeft(); | |
float y = n0.getBoundingBox().getBottom(); | |
float width = n0.getBoundingBox().getWidth(); | |
float height = n0.getBoundingBox().getHeight(); | |
n0.setColorFill(BaseColor.LIGHT_GRAY); | |
n0.rectangle(x, y, width, height); | |
n0.fill(); | |
// Creating the appearance for layer 2 | |
PdfTemplate n2 = appearance.getLayer(2); | |
ColumnText ct = new ColumnText(n2); | |
ct.setSimpleColumn(750,36,780,559); | |
Paragraph p = new Paragraph(rmaForm.getCustomerSignatoryNameCRTD()); | |
ct.addElement(p); | |
ct.go(); | |
appearance.setCrypto(privateKey, certificateChain, null, | |
PdfSignatureAppearance.WINCER_SIGNED); | |
appearance.setImage(Image.getInstance(rmaLoad.getSignatureImage(this, rmaForm))); | |
appearance.setImageScale(-1); | |
stamper.close(); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://api.itextpdf.com/iText5/java/5.5.9/com/itextpdf/text/pdf/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://pastebin.com/hN6CTCRP | |
String src = "input.pdf"; | |
String dest = "output.pdf"; | |
PdfReader reader = new PdfReader(src); | |
int n = reader.getXrefSize(); | |
PdfObject object; | |
PRStream stream; | |
// Look for image and manipulate image stream | |
for (int i = 0; i < n; i++) { | |
object = reader.getPdfObject(i); | |
if (object == null || !object.isStream()) | |
continue; | |
stream = (PRStream) object; | |
if (!PdfName.IMAGE.equals(stream.getAsName(PdfName.SUBTYPE))) | |
continue; | |
if (!PdfName.DCTDECODE.equals(stream.getAsName(PdfName.FILTER))) | |
continue; | |
PdfImageObject image = new PdfImageObject(stream); | |
BufferedImage bi = image.getBufferedImage(); | |
double factor = .5; | |
if (bi == null) | |
continue; | |
int width = (int) (bi.getWidth() * factor); | |
int height = (int) (bi.getHeight() * factor); | |
if (width <= 0 || height <= 0) | |
continue; | |
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
AffineTransform at = AffineTransform.getScaleInstance(factor, factor); | |
Graphics2D g = img.createGraphics(); | |
g.drawRenderedImage(bi, at); | |
System.out.println(image.getFileType()); | |
ByteArrayOutputStream imgBytes = new ByteArrayOutputStream(); | |
ImageIO.write(img, image.getFileType(), imgBytes); | |
stream.clear(); | |
stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION); | |
stream.put(PdfName.TYPE, PdfName.XOBJECT); | |
stream.put(PdfName.SUBTYPE, PdfName.IMAGE); | |
stream.put(PdfName.FILTER, PdfName.DCTDECODE); | |
stream.put(PdfName.WIDTH, new PdfNumber(width)); | |
stream.put(PdfName.HEIGHT, new PdfNumber(height)); | |
stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); | |
stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB); | |
} | |
reader.removeUnusedObjects(); | |
// Save altered PDF | |
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); | |
stamper.setFullCompression(); | |
stamper.close(); | |
reader.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.baeldung.com/java-pdf-creation | |
Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI()); | |
Document document = new Document(); | |
PdfWriter.getInstance(document, new FileOutputStream("iTextImageExample.pdf")); | |
document.open(); | |
Image img = Image.getInstance(path.toAbsolutePath().toString()); | |
document.add(img); | |
document.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment