I have added rectangle to pdf, How to remove rectangle border?

Dear Support,

How to remove rectangle border?Filled_Rect.pdf (51.0 KB)

Code samples

import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.License;
import com.aspose.pdf.Page;
import com.aspose.pdf.drawing.Graph;
import com.aspose.pdf.drawing.Rectangle;

public class CreateFilledRectangleObject {

	public static void main(String[] args) {
		// Create Document instance
		Document doc = new Document("XXX/Downloads/bond_copy.pdf");
		// Add page to pages collection of PDF file
		Page page = doc.getPages().get_Item(1);
		// Create Graph instance
		Graph graph = new Graph(400, 700);
		// Add graph object to paragraphs collection of page instance
		page.getParagraphs().add(graph);
		// Create Rectangle instance
		Rectangle rect = new Rectangle(88, (int)(page.getPageInfo().getHeight()-368), 232, 68);
		// Specify fill color for Graph object
		rect.getGraphInfo().setFillColor(Color.getWhite());
		//rect.getText().setText("");
		// Add rectangle object to shapes collection of Graph object
		graph.getShapes().add(rect);
		// save resultant PDF file
		doc.save("XXX/Downloads/Filled_Rect.pdf");
	}
	
	static{
		License license = new License();
		try {
			license.setLicense("XXX/licenses/Aspose.Total.Java.lic");// No I18N
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@ganesh.sv

Thank you for contacting support.

We would like to request you to upgrade to Aspose.PDF for Java 18.8 because it includes more features and bug fixes. Also, the support is provided based on latest available version. The code snippet shared by you is not compatible with latest version because Rectangle class does not expose getGraphInfo method in latest version. We will review respective articles accordingly.

Furthermore, you may use FloatingBox instance to add a filled rectangle without the border. Its position can be specified with setTop and setLeft methods as in the code snippet below.

Document document = new Document();
Page page = document.getPages().add();
page.getPageInfo().setMargin(new MarginInfo(0, 0, 0, 0));
FloatingBox box = new FloatingBox((float)222, (float)333);
box.setLeft(0f);
box.setTop(0f);
box.setBackgroundColor(Color.getTransparent());
box.setVerticalAlignment(VerticalAlignment.Top);
box.setBorder(new BorderInfo(BorderSide.None));
TextFragment fragment = new TextFragment("Sample Text added in FloatingBox");
fragment.setHorizontalAlignment(HorizontalAlignment.Center); 
fragment.setMargin(new MarginInfo(36, 0, 36, 0));
box.getParagraphs().add(fragment);
page.getParagraphs().add(box);
document.save("XXX\\Downloads\\FloatingBox.pdf");

Please note that the basic unit in Aspose.PDF for Java API is Point, where 72 points are equal to 1 inch.

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

It is working now.
My requirements is to remove text inside a given rectangle. How Can it be achieved? I thought of adding a rectangle as a layer to hide text.:thinking: But text is still available to copy. :grinning:
Please share the some code example. Please refer the attachment in the first thread.

@ganesh.sv

We are glad to know that suggested approach worked in your environment. Moreover, in order to remove text in given rectangle, you can follow the approach explained in Redact certain page region with RedactionAnnotation.

We hope this will be helpful. Feel free to contact us if you need any further assistance.

@Farhan.Raza Even RedactionAnnotation is not stopping me from copying the text.
Please see the attachments.Filled_Rect.pdf (92.0 KB)

I need to erase the content of the given rectangle.

@ganesh.sv

Please redact the annotation before adding it on the PDF page and then underlying text will be removed as per your requirements.

...
annot.redact();
doc.getPages().get_Item(1).getAnnotations().add(annot);
...

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

@Farhan.Raza
I followed the way you suggested. For few documents it worked and for other documents it throws java.lang.NullPointerException.
I have attached the file, code and the exception threads. Please let me know the root cause or andy changes required in code.
Test pdf file is attached here.Raja_G.pdf (83.5 KB)

  1. Also, for the documents redaction worked, page content shrinks to left from right. Is it expected behavior? If not, can we avoid it?
  2. I tried redaction for second page onwards, it never worked. Filled_Rect.pdf (45.3 KB)

It is re-producing with latest version of aspose.pdf.

public static void main(String[] args) {
	try{
		// Create Document instance
		Document doc = new Document(folderPath+"/Raja_G.pdf");
		Page page = doc.getPages().get_Item(1);
		Rectangle rect = new Rectangle(44.15723270440251d, 486.1850533807828d, 205.06918238993708d, 603.414590747331d);
		RedactionAnnotation annot = new RedactionAnnotation(page, rect);
		annot.setFillColor(Color.getGray());
		annot.setBorderColor(Color.getGray());
		annot.setColor(Color.getGray());
		annot.setTextAlignment(HorizontalAlignment.Center);
		//annot.flatten();
		annot.redact();
		page.getAnnotations().add(annot);
		
		doc.save(folderPath+"/Raja_GRect.pdf");
	}catch(Exception e){
		e.printStackTrace();
	}
}

Exception trace is as follows:

java.lang.NullPointerException
at com.aspose.pdf.AnnotationCollection.delete(Unknown Source)
at com.aspose.pdf.Annotation.flatten(Unknown Source)
at com.aspose.pdf.RedactionAnnotation.flatten(Unknown Source)
at com.aspose.pdf.RedactionAnnotation.redact(Unknown Source)
at xxx.xxx.xxx.xxx.CreateFilledRectangleObject.main(CreateFilledRectangleObject.java:99)

@ganesh.sv

We have been able to reproduce NullPointerException while redacting attached PDF files. A ticket with ID PDFJAVA-37993 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

About content shrinking, please create a separate post while sharing the source and generated PDF files along with the code snippet so that we may try to reproduce and investigate it further.

We are sorry for the inconvenience.

@Farhan.Raza
If it is going to take time, Do you have any workaround ?
Or is it document specific?
Could You let me know the scenario where all NullPointerException would come?

@ganesh.sv

Apparently the issue is file specific which occurs for specific documents only, so a work around may not be available in this case. Please let us know if the exception is thrown frequently for many of your documents, share such files with us. Moreover, the reason for NullPointerException can not be specified until the issue is investigated in detail. We will let you know as soon as any significant update will be available in this regard.