private void button1_Click(object sender, EventArgs e) { //create a pdf document PdfDocument doc = new PdfDocument(); //add a new page PdfPageBase page = doc.Pages.Add(); //create a layer named "red line" PdfPageLayer layer = page.PageLayers.Add("red line"); layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Red, 1), new PointF(0, 100), new PointF(100, 100)); //create a layer named "blue line" layer = page.PageLayers.Add("blue line"); layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 150), new PointF(100, 150)); //create a layer named "green line" layer = page.PageLayers.Add("green line"); layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Green, 1), new PointF(0, 200), new PointF(100, 200)); string output = "AddLayers.pdf"; //save pdf document doc.SaveToFile(output); //Launching the Pdf file PDFDocumentViewer(output); }
private void button2_Click(object sender, EventArgs e) { //create a pdf document string pdfFile = @"E:\HTTPPost\Example.pdf"; PdfDocument doc = new PdfDocument(); doc.LoadFromFile(pdfFile); //add a new page PdfPageBase page = doc.Pages[0]; //create a layer named "red line" PdfPageLayer layer = page.PageLayers.Add(); layer.Graphics.DrawLine(new PdfPen(PdfBrushes.Red, 1), new PointF(50, 50), new PointF(100, 100)); PdfImage image = new PdfBitmap(@"E:\HTTPPost\name.bmp"); layer.Graphics.DrawImage(image, new RectangleF(100, 100, 40, 20)); //save pdf document string output = @"E:\HTTPPost\AddLayers.pdf"; doc.SaveToFile(output); System.Diagnostics.Process.Start(output); }
/// <summary> /// Create a simple PDF document /// </summary> /// <returns>Return the created PDF document as stream</returns> public MemoryStream LayersPDF() { //Create a new PDF document PdfDocument doc = new PdfDocument(); doc.PageSettings = new PdfPageSettings(new SizeF(350, 300)); PdfPage page = doc.Pages.Add(); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16); page.Graphics.DrawString("Layers", font, PdfBrushes.DarkBlue, new PointF(150, 10)); //Add the first layer PdfPageLayer layer = page.Layers.Add("Layer1"); PdfGraphics graphics = layer.Graphics; graphics.TranslateTransform(100, 60); //Draw Arc PdfPen pen = new PdfPen(Color.Red, 50); RectangleF rect = new RectangleF(0, 0, 50, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer on the page layer = page.Layers.Add("Layer2"); graphics = layer.Graphics; graphics.TranslateTransform(100, 180); graphics.SkewTransform(0, 50); //Draw another set of elements pen = new PdfPen(Color.Red, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer layer = page.Layers.Add("Layer3"); graphics = layer.Graphics; graphics.TranslateTransform(160, 120); //Draw another set of elements. pen = new PdfPen(Color.Red, 50); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); MemoryStream stream = new MemoryStream(); //Save the PDF document doc.Save(stream); stream.Position = 0; //Close the PDF document doc.Close(true); return(stream); }
private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { //Create a new instance of PdfDocument class. doc = new PdfDocument(); // Set page size. doc.PageSettings = new PdfPageSettings(new SizeF(350, 300)); // Add a new page to the document. page = doc.Pages.Add(); page.Graphics.DrawString("Layers", new PdfStandardFont(PdfFontFamily.Helvetica, 16), PdfBrushes.DarkBlue, new PointF(150, 10)); //Add first layer PdfPageLayer layer = page.Layers.Add("Layer1"); PdfGraphics graphics = layer.Graphics; graphics.TranslateTransform(100, 60); //Draw Arc PdfPen pen = new PdfPen(redColor, 50); RectangleF rect = new RectangleF(0, 0, 50, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(blueColor, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(yellowColor, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(greenColor, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer on the page layer = page.Layers.Add("Layer2"); graphics = layer.Graphics; graphics.TranslateTransform(100, 180); //Draw another set of elements pen = new PdfPen(redColor, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(blueColor, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(yellowColor, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(greenColor, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add third layer. layer = page.Layers.Add("Layer3"); graphics = layer.Graphics; graphics.TranslateTransform(160, 120); //Draw another set of elements. pen = new PdfPen(redColor, 50); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(blueColor, 30); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); pen = new PdfPen(yellowColor, 20); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(greenColor, 10); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); // Save and close the document. MemoryStream stream = new MemoryStream(); await doc.SaveAsync(stream); doc.Close(true); Save(stream, "Layers.pdf"); }
public ActionResult Layers(string InsideBrowser) { //Create a new PDF document PdfDocument doc = new PdfDocument(); doc.PageSettings = new PdfPageSettings(new SizeF(350, 300)); PdfPage page = doc.Pages.Add(); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16); page.Graphics.DrawString("Layers", font, PdfBrushes.DarkBlue, new PointF(150, 10)); //Add the first layer PdfPageLayer layer = page.Layers.Add("Layer1"); PdfGraphics graphics = layer.Graphics; graphics.TranslateTransform(100, 60); //Draw Arc PdfPen pen = new PdfPen(Color.Red, 50); RectangleF rect = new RectangleF(0, 0, 50, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer on the page layer = page.Layers.Add("Layer2"); graphics = layer.Graphics; graphics.TranslateTransform(100, 180); graphics.SkewTransform(0, 50); //Draw another set of elements pen = new PdfPen(Color.Red, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer layer = page.Layers.Add("Layer3"); graphics = layer.Graphics; graphics.TranslateTransform(160, 120); //Draw another set of elements. pen = new PdfPen(Color.Red, 50); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); pen = new PdfPen(Color.Yellow, 20); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(Color.Green, 10); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); //Stream the output to the browser. if (InsideBrowser == "Browser") { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open)); } else { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } }