Add a filled rectangle to a PDF document in C# using Aspose.PDF for .NET

Hi,

I wonder whether anybody here can help me? I am simply trying to add a couple of filled rectangles to a PDF document. One, ideally filed with a gradient, the other just a solid colour. I can see that you can add rectangles using the graph tools, but is it necessary to add a graph per shape when building a small diagram?

Hopefully I’m just being stupid and am missing the controls.

Thanks in advance,

Alan

Hi there,


Thanks for your inquiry. You can add a filled rectangle with the help of PolygonAnnotation. Please check following code snippet for the purpose. Please note gradient fill is not supported at the moment.

Add Rectangle to PDF Document using Aspose.PDF for .NET

Document pdf = new Document(myDir + "HelloWorld.pdf");

Page page=pdf.Pages[1];

LineInfo lineinfo = new LineInfo();

lineinfo.VerticeCoordinate = new float[] { 300, 700, 300, 750, 400, 750,400,700,300,700 };

lineinfo.Visibility = true;

int length = lineinfo.VerticeCoordinate.Length / 2;

Aspose.Pdf.Point[] gesture = new Aspose.Pdf.Point[length];

for (int i = 0; i < length; i++)

{

gesture[i] = new Aspose.Pdf.Point(lineinfo.VerticeCoordinate[2 * i], lineinfo.VerticeCoordinate[2 * i + 1]);

}

PolygonAnnotation polyAnnot = new PolygonAnnotation(page, new Aspose.Pdf.Rectangle(0, 0, 0, 0), gesture);

polyAnnot.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

polyAnnot.InteriorColor = Aspose.Pdf.Color.YellowGreen;

Border border = new Border(polyAnnot);

// border.Dash = new Dash(2, 1);

// border.Style = Aspose.Pdf.InteractiveFeatures.Annotations.BorderStyle.Dashed;

border.Width = 2;

polyAnnot.Border = border;

polyAnnot.Opacity = .8;

page.Annotations.Add(polyAnnot);

pdf.Save(myDir+"polygon_out.pdf");

Please feel free to contact us for any further assistance.


Best Regards,