Skip to content

Instantly share code, notes, and snippets.

@thara
Created March 19, 2013 06:02
Show Gist options
  • Save thara/5194013 to your computer and use it in GitHub Desktop.
Save thara/5194013 to your computer and use it in GitHub Desktop.
iTextを使用した、既存PDFに対する編集
// PdfStamperのコンストラクタに渡したOutputStreamは、PdfStamper#close内でcloseメソッドが呼ばれてしまうため、
// 一時領域としてByteArrayOutputStreamを渡し、編集結果のバイト文字列を引数のOutputStreamに書き込む。
ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
PdfStamper stamp;
try {
stamp = new PdfStamper(original, tempOut);
} catch (IOException e) {
throw new DocumentException(e);
}
int pageSize = original.getNumberOfPages();
for (int importPageNo = 1; importPageNo <= pageSize; importPageNo++) {
PdfContentByte underContent = stamp.getUnderContent(importPageNo);
edit(underContent);
}
try {
stamp.close();
out.write(tempOut.toByteArray());
} catch (IOException e) {
throw new DocumentException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment