'iText7 : com.itextpdf.kernel.exceptions.PdfException: Invalid page structure 1
I am trying to shrink my pdf file using itext7-core:7.1.2, but getting com.itextpdf.kernel.PdfException: Invalid page structure 1.
Below code is working fine for other files, but just for one file its throwing this exception. I opened the file manually and its opening correctly.
Below is my code snippet and its throwing exception at PdfPage pdfPage = pdfDoc.getPage(p) line in below code:
public ByteBuffer multipartToFile(MultipartFile multipart, String fileName) throws Exception {
ByteBuffer byteBuffer = null;
File originalFile = new File(System.getProperty("java.io.tmpdir") + "/" +"original_" +fileName);
multipart.transferTo(originalFile);
originalFile.deleteOnExit();
try(InputStream is = new FileInputStream(originalFile)){
//Shrink original file and make a pdf out of it
File shrunkFile = new File(System.getProperty("java.io.tmpdir") + "/" +fileName);
byteBuffer = shrinkOriginalFilByRequiredRatio(originalFile, shrunkFile, is, byteBuffer);
//File Cleanup
if(shrunkFile.exists())
Files.delete(shrunkFile);
}
//File Cleanup
if(originalFile.exists())
Files.delete(originalFile);
return byteBuffer;
}
protected ByteBuffer shrinkOriginalFilByRequiredRatio(File originalFile, File shrunkFile, InputStream is, ByteBuffer byteBuffer) throws Exception {
LOGGER.info("started to Shrink originalFile={}", originalFile);
try (PdfWriter pdrWriter = new PdfWriter(shrunkFile.getAbsoluteFile())){
PdfDocument pdfDoc = new PdfDocument(new PdfReader(is), pdrWriter);
float percentage = 0.9f;
for (int p = 1; p <= pdfDoc.getNumberOfPages(); p++) {
PdfPage pdfPage = pdfDoc.getPage(p);
Rectangle pageSize = pdfPage.getPageSize();
float offsetX = (pageSize.getWidth() * (1 - percentage)) / 2;
float offsetY = (pageSize.getHeight() * (1 - percentage)) / 2;
new PdfCanvas(pdfPage.newContentStreamBefore(), pdfPage.getResources(), pdfDoc)
.writeLiteral(String.format(Locale.ENGLISH, "\nq %s 0 0 %s %s %s cm\nq\n",
percentage, percentage, offsetX, offsetY));
new PdfCanvas(pdfPage.newContentStreamAfter(), pdfPage.getResources(), pdfDoc)
.writeLiteral("\nQ\nQ\n");
}
pdfDoc.close();
try (InputStream isShrunk = new FileInputStream(shrunkFile)){
byte[] byteArray = ByteStreams.toByteArray(isShrunk);
byteBuffer = ByteBuffer.wrap(byteArray);
}
} catch (Exception e) {
LOGGER.error("Exception : file Shrink exception {}", ExceptionUtils.getStackTrace(e));
throw e;
}
return byteBuffer;
}
And the output exception is:
com.itextpdf.kernel.exceptions.PdfException: Invalid page structure 1.
at com.itextpdf.kernel.pdf.PdfPagesTree.loadPage(PdfPagesTree.java:386)
at com.itextpdf.kernel.pdf.PdfPagesTree.loadPage(PdfPagesTree.java:343)
at com.itextpdf.kernel.pdf.PdfPagesTree.getPage(PdfPagesTree.java:120)
at com.itextpdf.kernel.pdf.PdfDocument.getPage(PdfDocument.java:416)
Solution 1:[1]
The issue was with the file, I recreated the pdf and the upload was successful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Amrit raj singh |