Hi,
I am using windows 10, JDK 11, with the latest aspose pdf version (23.1).
I have a problem when i try to draw rectangles in a pdf. The rectangles are not rendered if i do more operation on the pdf document.
Currently i am trying to redact a text and then convert the pdf page into an image.
If i only add the rectangle and save the pdf file i can see the rectangle, but if i add the rectangle and convert the page into an image the rectangle is not rendered.
This is the file i used: redactionDemo.pdf (374.5 KB)
In my code i have made two TODO entries. If you comment in the first TODO and comment out the last TODO, a pdf is generated with the rectangle. Now if you comment out the first TODO and comment in the last TODO, no rectangle is rendered.
Thanks for your help.
This is the code i am currently using:
try (FileOutputStream redactionOnly = new FileOutputStream(new File("C:\\test\\redactionOnly.pdf"));
FileOutputStream redactionAndConversion = new FileOutputStream(
new File("C:\\test\\redactionAndConversion.pdf")); //
final Document targetDocument = new Document())
{
final Document asposeDocument = new Document(pdfStream);
// 1. Draw rectangles
final Page page = asposeDocument.getPages().get_Item(1);
page.getPageInfo().setMargin(new MarginInfo(0, 0, 0, 0));
final float pageWidth = (float) page.getPageInfo().getWidth();
final float pageHeight = (float) page.getPageInfo().getHeight();
final com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph(pageWidth, pageHeight);
page.getParagraphs().add(graph);
final float x = 59.69323f;
final float y = 251.07489f;
final float width = 485.14178f;
final float height = 103.81027f;
System.out.println("x: " + x + " y: " + y + " width: " + width + " height: " + height);
final com.aspose.pdf.drawing.Rectangle rectangle = new com.aspose.pdf.drawing.Rectangle(x, y, width,
height);
rectangle.getGraphInfo().setFillColor(Color.getBlack());
graph.getShapes().add(rectangle);
// TODO If commented in, the rectangle is drawn
// asposeDocument.save(redactionOnly);
// Convert each page in the pdf into a jpg image
final Resolution res = new Resolution(100);
final ImageDevice imageDevice = new JpegDevice(res);
for (int i = 1; i <= asposeDocument.getPages().size(); i++)
{
final Page srcPage = asposeDocument.getPages().get_Item(i);
Page targetPage = targetDocument.getPages().add();
try (ByteArrayOutputStream convertedPageStream = new ByteArrayOutputStream())
{
// Konvertiere Source Page in ein Bild
imageDevice.process(srcPage, convertedPageStream);
InputStream imageInputStream = new ByteArrayInputStream(convertedPageStream.toByteArray());
final com.aspose.pdf.Rectangle imageRectangle = new com.aspose.pdf.Rectangle(0,
targetPage.getPageInfo().getHeight(), targetPage.getPageInfo().getWidth(), 0);
targetPage.addImage(imageInputStream, imageRectangle);
}
}
// TODO With this save the rectangle is not drawn
targetDocument.save(redactionAndConversion);
} catch (IOException e)
{
e.printStackTrace();
}