Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active November 15, 2021 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/e929a4caba1bc968da0767d9dcac90eb to your computer and use it in GitHub Desktop.
Save conholdate-gists/e929a4caba1bc968da0767d9dcac90eb to your computer and use it in GitHub Desktop.
Add Graphs in PDF Documents using C#
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Drawing object with certain dimensions
Graph graph = new Graph(400, 200);
// create circle
Circle circle = new Circle(100, 100, 40);
circle.GraphInfo.Color = Color.Green;
circle.GraphInfo.FillColor = Color.GreenYellow;
graph.Shapes.Add(circle);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\FilledCircle_out.pdf");
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Drawing object with certain dimensions
Graph graph = new Graph(400, 400);
// Define text
TextFragment textFragment = new TextFragment("Ellipse");
textFragment.TextState.Font = FontRepository.FindFont("Helvetica");
textFragment.TextState.FontSize = 24;
// Draw ellipse
Ellipse ellipse = new Ellipse(100, 100, 120, 180);
ellipse.GraphInfo.FillColor = Color.GreenYellow;
ellipse.GraphInfo.Color = Color.Red;
ellipse.Text = textFragment;
// Add ellipse to shapes collection of Graph object
graph.Shapes.Add(ellipse);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\EclipseWithText_out.pdf");
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Create Graph instance
Graph graph = new Graph(100, 400);
// Add graph object to paragraphs collection of page instance
page.Paragraphs.Add(graph);
// Create Rectangle instance
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120);
// Specify fill color for Graph object
rect.GraphInfo.FillColor = Color.Gray;
// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(rect);
// Save PDF file
doc.Save(@"C:\Files\PDF\FilledRectangle_out.pdf");
// Create Document instance
Document doc = new Document();
// Add page to pages collection of PDF file
Page page = doc.Pages.Add();
// Set page margin on all sides as 0
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
// Create Graph object with Width and Height equal to page dimensions
Graph graph = new Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
// Create first line object starting from Lower-Left to Top-Right corner of page
Line line = new Line(new float[] { (float) page.Rect.LLX, 0, (float) page.PageInfo.Width,
(float) page.Rect.URY });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line);
// Draw line from Top-Left corner of page to Bottom-Right corner of page
Line line2 = new Line(new float[] { 0, (float) page.Rect.URY, (float) page.PageInfo.Width,
(float) page.Rect.LLX });
// Add line to shapes collection of Graph object
graph.Shapes.Add(line2);
// Add Graph object to paragraphs collection of page
page.Paragraphs.Add(graph);
// Save PDF file
doc.Save(@"C:\Files\PDF\DrawLineAcrossPage_out.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment