Added rectangle not showing up

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();
	}

@zesman2,

Qhat is the value of imageDeviceResolution?

The code is missing that.

Hi,

sry the resolution value is 100. I updated my code.

@zesman2,

Sorry for the late response.

I run the following code and it works:

public void Logic(Document doc) throws Exception
{
    // 1. Draw rectangles
    final Page page = doc.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);

    doc.save(PartialPath + "_output.pdf");

    // Convert each page in the pdf into a jpg image
    final Resolution res = new Resolution(100);
    final ImageDevice imageDevice = new JpegDevice(res);

    Document targetDocument = new Document();
    for (int i = 1; i <= doc.getPages().size(); i++)
    {
        final Page srcPage = doc.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, 0,
                    targetPage.getPageInfo().getWidth(), targetPage.getPageInfo().getHeight());
            targetPage.addImage(imageInputStream, imageRectangle);
        }
    }

    // TODO With this save the rectangle is not drawn
    targetDocument.save(PartialPath + "New_output.pdf");
}

The input and output files:
RedactionNotDrawedAfterSave_input.pdf (374.5 KB)
RedactionNotDrawedAfterSave_output.pdf (375.7 KB)
RedactionNotDrawedAfterSaveNew_output.pdf (374.7 KB)

I am also able to run the code with the save commented out and it works too. I obviously get only one output.

Related to your post. If you comment both TODOs then there is no save, so the document will never have the rectangles that where only drawn in memory.

Hi,

i know that this is working, because you are temporary saving the pdf after drawing the rectangle. But thats not what i want. I want to draw a rectangle and convert to an image all in memory.

I resolved the issue in a different way, i just changed the two code parts. I now first convert to an image and then draw the rectangle, without temporary saving the PDF to a file.
This is working for me.

But anyway thanks for your help.