[ https://issues.apache.org/jira/browse/PDFBOX-5462?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17555936#comment-17555936 ]
Marian Ion edited comment on PDFBOX-5462 at 6/18/22 4:36 PM: ------------------------------------------------------------- Here's what *Eclipse Memory Analyzer* finds on a heap dump: {code:java} main at java.lang.OutOfMemoryError.<init>()V (OutOfMemoryError.java:48) at java.nio.HeapByteBuffer.duplicate()Ljava/nio/ByteBuffer; (HeapByteBuffer.java:131) at org.apache.pdfbox.io.RandomAccessReadBuffer.<init>(Lorg/apache/pdfbox/io/RandomAccessReadBuffer;)V (RandomAccessReadBuffer.java:135) at org.apache.pdfbox.io.RandomAccessReadBuffer.createView(JJ)Lorg/apache/pdfbox/io/RandomAccessReadView; (RandomAccessReadBuffer.java:363) at org.apache.pdfbox.pdfparser.COSParser.createRandomAccessReadView(JJ)Lorg/apache/pdfbox/io/RandomAccessReadView; (COSParser.java:585) at org.apache.pdfbox.cos.COSDocument.createCOSStream(Lorg/apache/pdfbox/cos/COSDictionary;JJ)Lorg/apache/pdfbox/cos/COSStream; (COSDocument.java:185) at org.apache.pdfbox.pdfparser.COSParser.parseCOSStream(Lorg/apache/pdfbox/cos/COSDictionary;)Lorg/apache/pdfbox/cos/COSStream; (COSParser.java:914) at org.apache.pdfbox.pdfparser.COSParser.parseFileObject(Ljava/lang/Long;Lorg/apache/pdfbox/cos/COSObjectKey;)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:693) at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(Lorg/apache/pdfbox/cos/COSObjectKey;Z)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:615) at org.apache.pdfbox.pdfparser.COSParser.dereferenceCOSObject(Lorg/apache/pdfbox/cos/COSObject;)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:573) at org.apache.pdfbox.cos.COSObject.getObject()Lorg/apache/pdfbox/cos/COSBase; (COSObject.java:121) at org.apache.pdfbox.cos.COSDictionary.getDictionaryObject(Lorg/apache/pdfbox/cos/COSName;)Lorg/apache/pdfbox/cos/COSBase; (COSDictionary.java:183) at org.apache.pdfbox.pdmodel.PDPage.hasContents()Z (PDPage.java:218) at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(Lorg/apache/pdfbox/pdmodel/PDDocument;Lorg/apache/pdfbox/pdmodel/PDPage;Lorg/apache/pdfbox/pdmodel/PDPageContentStream$AppendMode;ZZLorg/apache/pdfbox/pdmodel/common/PDStream;Lorg/apache/pdfbox/pdmodel/PDResources;)V (PDPageContentStream.java:140) at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(Lorg/apache/pdfbox/pdmodel/PDDocument;Lorg/apache/pdfbox/pdmodel/PDPage;Lorg/apache/pdfbox/pdmodel/PDPageContentStream$AppendMode;ZZ)V (PDPageContentStream.java:123) at PdfboxWatermark.addWatermark(Ljava/lang/String;Lorg/apache/pdfbox/pdmodel/PDDocument;)I (PdfboxWatermark.java:57) at PdfboxWatermark.setWatermarkOnContent(Ljava/lang/String;Ljava/io/InputStream;Ljava/io/OutputStream;)I (PdfboxWatermark.java:37) at WatermarkPDF.main([Ljava/lang/String;)V (WatermarkPDF.java:29) {code} was (Author: JIRAUSER291146): Here's what *Eclipse Memory Analyzer *finds on a heap dump: {code:java} main at java.lang.OutOfMemoryError.<init>()V (OutOfMemoryError.java:48) at java.nio.HeapByteBuffer.duplicate()Ljava/nio/ByteBuffer; (HeapByteBuffer.java:131) at org.apache.pdfbox.io.RandomAccessReadBuffer.<init>(Lorg/apache/pdfbox/io/RandomAccessReadBuffer;)V (RandomAccessReadBuffer.java:135) at org.apache.pdfbox.io.RandomAccessReadBuffer.createView(JJ)Lorg/apache/pdfbox/io/RandomAccessReadView; (RandomAccessReadBuffer.java:363) at org.apache.pdfbox.pdfparser.COSParser.createRandomAccessReadView(JJ)Lorg/apache/pdfbox/io/RandomAccessReadView; (COSParser.java:585) at org.apache.pdfbox.cos.COSDocument.createCOSStream(Lorg/apache/pdfbox/cos/COSDictionary;JJ)Lorg/apache/pdfbox/cos/COSStream; (COSDocument.java:185) at org.apache.pdfbox.pdfparser.COSParser.parseCOSStream(Lorg/apache/pdfbox/cos/COSDictionary;)Lorg/apache/pdfbox/cos/COSStream; (COSParser.java:914) at org.apache.pdfbox.pdfparser.COSParser.parseFileObject(Ljava/lang/Long;Lorg/apache/pdfbox/cos/COSObjectKey;)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:693) at org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(Lorg/apache/pdfbox/cos/COSObjectKey;Z)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:615) at org.apache.pdfbox.pdfparser.COSParser.dereferenceCOSObject(Lorg/apache/pdfbox/cos/COSObject;)Lorg/apache/pdfbox/cos/COSBase; (COSParser.java:573) at org.apache.pdfbox.cos.COSObject.getObject()Lorg/apache/pdfbox/cos/COSBase; (COSObject.java:121) at org.apache.pdfbox.cos.COSDictionary.getDictionaryObject(Lorg/apache/pdfbox/cos/COSName;)Lorg/apache/pdfbox/cos/COSBase; (COSDictionary.java:183) at org.apache.pdfbox.pdmodel.PDPage.hasContents()Z (PDPage.java:218) at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(Lorg/apache/pdfbox/pdmodel/PDDocument;Lorg/apache/pdfbox/pdmodel/PDPage;Lorg/apache/pdfbox/pdmodel/PDPageContentStream$AppendMode;ZZLorg/apache/pdfbox/pdmodel/common/PDStream;Lorg/apache/pdfbox/pdmodel/PDResources;)V (PDPageContentStream.java:140) at org.apache.pdfbox.pdmodel.PDPageContentStream.<init>(Lorg/apache/pdfbox/pdmodel/PDDocument;Lorg/apache/pdfbox/pdmodel/PDPage;Lorg/apache/pdfbox/pdmodel/PDPageContentStream$AppendMode;ZZ)V (PDPageContentStream.java:123) at PdfboxWatermark.addWatermark(Ljava/lang/String;Lorg/apache/pdfbox/pdmodel/PDDocument;)I (PdfboxWatermark.java:57) at PdfboxWatermark.setWatermarkOnContent(Ljava/lang/String;Ljava/io/InputStream;Ljava/io/OutputStream;)I (PdfboxWatermark.java:37) at WatermarkPDF.main([Ljava/lang/String;)V (WatermarkPDF.java:29) {code} > OutOfMemoryError when watermaking in 3.0.0-RC1 > ---------------------------------------------- > > Key: PDFBOX-5462 > URL: https://issues.apache.org/jira/browse/PDFBOX-5462 > Project: PDFBox > Issue Type: Bug > Affects Versions: 3.0.0 PDFBox > Reporter: Marian Ion > Priority: Major > > I am using the Maven *3.0.0-RC1* version and I encounter the following error > when watermarking a 5120 pages file: > {quote} java.lang.OutOfMemoryError: Java heap space: failed reallocation of > scalar replaced objects > {quote} > > However, the *2.0.26* version code works without problem! > The code is basically this : > {code:java} > private static final PDFont PDF_FONT = PDType1Font.HELVETICA; > memoryUsageSetting = MemoryUsageSetting.setupMixed(2 * ONE_GIGA, 40 * > ONE_GIGA); > //try (PDDocument pdfDocument = PDDocument.load(is, memoryUsageSetting)) { > // 2.0.26 > try (PDDocument pdfDocument = Loader.loadPDF(inputStream, > memoryUsageSetting)) { // 3.0.0-RC1 > int nbPages = addWatermark(watermarkText, pdfDocument); > pdfDocument.save(os); > } > ... > private int addWatermark(String watermarkText, PDDocument document) throws > IOException { > int numberOfPages = document.getNumberOfPages(); > System.out.printf("Start adding watermark on a %d pages PDF > document%n", numberOfPages); > long start = System.nanoTime(); > int pageIndex = 0; > for(PDPage page : document.getPages()) { > ++pageIndex; > try (PDPageContentStream cs = new PDPageContentStream(document, > page, PDPageContentStream.AppendMode.APPEND, true, true)) { > float width = page.getMediaBox().getWidth(); > float height = page.getMediaBox().getHeight(); > int rotation = page.getRotation(); > switch(rotation) { > case 90: > width = page.getMediaBox().getHeight(); > height = page.getMediaBox().getWidth(); > > cs.transform(Matrix.getRotateInstance(Math.toRadians(90), height, 0)); > break; > case 180: > > cs.transform(Matrix.getRotateInstance(Math.toRadians(180), width, height)); > break; > case 270: > width = page.getMediaBox().getHeight(); > height = page.getMediaBox().getWidth(); > > cs.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, width)); > break; > default: > break; > } > double stringWidth = > (double)PDF_FONT.getStringWidth(watermarkText) / 1000 * FONT_HEIGHT; > double diagonalLength = Math.sqrt((double)width * width + > (double)height * height); > double angle = Math.atan2(height, width); > cs.transform(Matrix.getRotateInstance(angle, 0, 0)); > cs.setFont(PDF_FONT, (float)FONT_HEIGHT); > //cs.setRenderingMode(RenderingMode.STROKE); // for "hollow" > effect > PDExtendedGraphicsState gs = new PDExtendedGraphicsState(); > gs.setNonStrokingAlphaConstant(0.2f); > gs.setStrokingAlphaConstant(0.2f); > gs.setBlendMode(BlendMode.MULTIPLY); > cs.setGraphicsStateParameters(gs); > // some API weirdness here. When int, range is 0..255. > // when float, this would be 0..1f > cs.setNonStrokingColor(0f, 0, 0); > cs.setStrokingColor(0f, 0, 0); // black > float x = (float)((diagonalLength - stringWidth) / 2); // > "horizontal" position in rotated world > float y = (float)(-FONT_HEIGHT / 4); // 4 is a trial-and-error > thing, this lowers the text a bit > cs.beginText(); > cs.newLineAtOffset(x, y); > cs.showText(watermarkText); > cs.endText(); > } finally { > ... > } > return numberOfPages; > } > {code} -- This message was sent by Atlassian Jira (v8.20.7#820007) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org For additional commands, e-mail: dev-h...@pdfbox.apache.org