I try to get card with slightly round edge but in my output the card is rectangle.
Card.zip (45.3 KB)
Creating a card with slightly rounded corners in Aspose.PDF for .NET
Below is a minimal, self‑contained example that shows how to draw a rounded‑corner rectangle (the “card”) and save it as a PDF.
The same approach works whether you start from a blank document or add the shape to an existing PDF.
1. Add the required NuGet package
dotnet add package Aspose.PDF
2. Sample code
using System.Drawing; // for Color
using Aspose.Pdf; // Document, Page
using Aspose.Pdf.Drawing; // RoundedRectangle, GraphInfo, FillInfo
class RoundedCardDemo
{
static void Main()
{
// ----------------------------------------------------
// 1️⃣ Create a new PDF document (or load an existing one)
// ----------------------------------------------------
Document pdfDoc = new Document();
Page page = pdfDoc.Pages.Add(); // add a page where the card will be placed
// ----------------------------------------------------
// 2️⃣ Define the card’s bounding box (lower‑left & upper‑right)
// ----------------------------------------------------
// Values are in points (1 pt = 1/72 inch)
float llx = 100; // left (X)
float lly = 500; // bottom (Y)
float urx = 400; // right (X)
float ury = 700; // top (Y)
// ----------------------------------------------------
// 3️⃣ Create a RoundedRectangle shape
// ----------------------------------------------------
RoundedRectangle card = new RoundedRectangle(page, llx, lly, urx, ury)
{
// Radius of the four corners – adjust to get the "slightly rounded" look
CornerRadius = 12, // 12 points ≈ 4 mm
};
// ----------------------------------------------------
// 4️⃣ Set border (stroke) and fill appearance
// ----------------------------------------------------
card.GraphInfo = new GraphInfo
{
Color = Color.Black, // border colour
LineWidth = 1.5f // border thickness
};
card.FillInfo = new FillInfo
{
Color = Color.LightGray // interior colour (optional)
};
// ----------------------------------------------------
// 5️⃣ Add the shape to the page
// ----------------------------------------------------
page.Paragraphs.Add(card);
// ----------------------------------------------------
// 6️⃣ (Optional) Add some text inside the card
// ----------------------------------------------------
TextFragment tf = new TextFragment("My Rounded Card")
{
TextState = { FontSize = 14, FontStyle = FontStyles.Bold, ForegroundColor = Color.Black },
Position = new Position(llx + 15, ury - 30) // a little padding from the edges
};
page.Paragraphs.Add(tf);
// ----------------------------------------------------
// 7️⃣ Save the PDF
// ----------------------------------------------------
pdfDoc.Save("RoundedCard.pdf");
}
}
What the code does
Step | Purpose |
---|---|
1 | Creates a fresh PDF document (or you could replace new Document() with new Document("input.pdf") ). |
2 | Sets the rectangular area that will become the card. Adjust llx , lly , urx , ury to change size/position. |
3 | Instantiates RoundedRectangle and assigns a CornerRadius . Larger values give more curvature. |
4 | Configures the border (GraphInfo ) and the fill colour (FillInfo ). Both are optional. |
5 | Adds the shape to the page’s content stream. |
6 | Demonstrates how to place text inside the card (optional). |
7 | Writes the final PDF to disk. |
3. Key properties to tweak for a “slightly rounded” look
Property | Typical range | Effect |
---|---|---|
CornerRadius |
5 – 15 points | Smaller → almost rectangular; larger → more rounded. |
LineWidth (in GraphInfo ) |
0.5 – 2 pts | Controls the thickness of the border. |
FillInfo.Color |
Any System.Drawing.Color |
Background colour of the card; set to Color.Transparent if you do not want a fill. |
4. Frequently asked follow‑up questions
-
I’m using the older “Generator” API (Aspose.Pdf.Generator).
The equivalent class isAspose.Pdf.Generator.RoundedRectangle
. The usage pattern is the same – just replace the namespace and constructor signature. -
How do I export the card as an image (PNG/JPEG) instead of PDF?
After saving the PDF, you can render it with Aspose.PDF for .NET’sPdfConverter
orPdfPageEditor
. Example:using Aspose.Pdf; using Aspose.Pdf.Devices; Document doc = new Document("RoundedCard.pdf"); using (FileStream imageStream = new FileStream("RoundedCard.png", FileMode.Create)) { PngDevice png = new PngDevice(300, 300); // DPI png.Process(doc.Pages[1], imageStream); }
-
Can I apply a drop‑shadow or other effects?
Yes – useGraphInfo
to setDashArray
,LineJoin
, etc., or draw additional shapes (e.g., a semi‑transparent offset rectangle) before the card to simulate a shadow.
5. Reference documentation
- RoundedRectangle class – https://docs.aspose.com/pdf/net/rounded-rectangle/
- Working with graphics (shapes) in PDF – https://docs.aspose.com/pdf/net/graphics/
- Adding text fragments – https://docs.aspose.com/pdf/net/text-fragment/
If you need any further adjustments (different dimensions, colors, embedding images, or converting the result to another format), just let me know and I’ll provide the exact snippet you need. Happy coding!