private void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); //Set margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //Title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Part List", format1).Height; y = y + 2; //Table top PdfDestination tableTopDest = new PdfDestination(page); tableTopDest.Location = new PointF(0, y); tableTopDest.Mode = PdfDestinationMode.Location; tableTopDest.Zoom = 1f; //Draw table PdfTrueTypeFont buttonFont = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); float buttonWidth = 70; float buttonHeight = buttonFont.Height * 1.5f; float tableTop = y; PdfLayoutResult tableLayoutResult = DrawTable(page, y + buttonHeight + 5); //Table bottom PdfDestination tableBottomDest = new PdfDestination(tableLayoutResult.Page); tableBottomDest.Location = new PointF(0, tableLayoutResult.Bounds.Bottom); tableBottomDest.Mode = PdfDestinationMode.Location; tableBottomDest.Zoom = 1f; //Go to table bottom float x = page.Canvas.ClientSize.Width - buttonWidth; PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); RectangleF buttonBounds = new RectangleF(x, tableTop, buttonWidth, buttonHeight); page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action1 = new PdfGoToAction(tableBottomDest); PdfActionAnnotation annotation1 = new PdfActionAnnotation(buttonBounds, action1); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.Color = Color.LightGray; (page as PdfNewPage).Annotations.Add(annotation1); //Go to table top float tableBottom = tableLayoutResult.Bounds.Bottom + 5; buttonBounds = new RectangleF(x, tableBottom, buttonWidth, buttonHeight); tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action2 = new PdfGoToAction(tableTopDest); PdfActionAnnotation annotation2 = new PdfActionAnnotation(buttonBounds, action2); annotation2.Border = new PdfAnnotationBorder(0.75f); annotation2.Color = Color.LightGray; (tableLayoutResult.Page as PdfNewPage).Annotations.Add(annotation2); //Go to last page PdfNamedAction action3 = new PdfNamedAction(PdfActionDestination.LastPage); doc.AfterOpenAction = action3; //Script String script = "app.alert({" + " cMsg: \"Oh no, you want to leave me.\"," + " nIcon: 3," + " cTitle: \"JavaScript Action\"" + "});"; PdfJavaScriptAction action4 = new PdfJavaScriptAction(script); doc.BeforeCloseAction = action4; //Save pdf file doc.SaveToFile("Action.pdf"); doc.Close(); //Launch the Pdf file PDFDocumentViewer("Action.pdf"); }
private static void CreateNamedActions(PdfFixedDocument document, PdfFont font) { PdfPen blackPen = new PdfPen(new PdfRgbColor(0, 0, 0), 1); PdfBrush blackBrush = new PdfBrush(); font.Size = 12; PdfStringAppearanceOptions sao = new PdfStringAppearanceOptions(); sao.Brush = blackBrush; sao.Font = font; PdfStringLayoutOptions slo = new PdfStringLayoutOptions(); slo.HorizontalAlign = PdfStringHorizontalAlign.Center; slo.VerticalAlign = PdfStringVerticalAlign.Middle; for (int i = 0; i < document.Pages.Count; i++) { document.Pages[i].Graphics.DrawString("Named actions:", font, blackBrush, 400, 20); ///////////// // First page ///////////// document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 40, 200, 20); slo.X = 500; slo.Y = 50; document.Pages[i].Graphics.DrawString("Go To First Page", sao, slo); // Create a link annotation on top of the widget. PdfLinkAnnotation link = new PdfLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.VisualRectangle = new PdfVisualRectangle(400, 40, 200, 20); // Create a named action and attach it to the link. PdfNamedAction namedAction = new PdfNamedAction(); namedAction.NamedAction = PdfActionName.FirstPage; link.Action = namedAction; ///////////// // Prev page ///////////// document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 80, 200, 20); slo.Y = 90; document.Pages[i].Graphics.DrawString("Go To Previous Page", sao, slo); // Create a link annotation on top of the widget. link = new PdfLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.VisualRectangle = new PdfVisualRectangle(400, 80, 200, 20); // Create a named action and attach it to the link. namedAction = new PdfNamedAction(); namedAction.NamedAction = PdfActionName.PrevPage; link.Action = namedAction; ///////////// // Next page ///////////// document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 120, 200, 20); slo.Y = 130; document.Pages[i].Graphics.DrawString("Go To Next Page", sao, slo); // Create a link annotation on top of the widget. link = new PdfLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.VisualRectangle = new PdfVisualRectangle(400, 120, 200, 20); // Create a named action and attach it to the link. namedAction = new PdfNamedAction(); namedAction.NamedAction = PdfActionName.NextPage; link.Action = namedAction; ///////////// // Last page ///////////// document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 160, 200, 20); slo.Y = 170; document.Pages[i].Graphics.DrawString("Go To Last Page", sao, slo); // Create a link annotation on top of the widget. link = new PdfLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.VisualRectangle = new PdfVisualRectangle(400, 160, 200, 20); // Create a named action and attach it to the link. namedAction = new PdfNamedAction(); namedAction.NamedAction = PdfActionName.LastPage; link.Action = namedAction; ///////////// // Print document ///////////// document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 200, 200, 20); slo.Y = 210; document.Pages[i].Graphics.DrawString("Print Document", sao, slo); // Create a link annotation on top of the widget. link = new PdfLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.VisualRectangle = new PdfVisualRectangle(400, 200, 200, 20); // Create a named action and attach it to the link. namedAction = new PdfNamedAction(); namedAction.NamedAction = PdfActionName.Print; link.Action = namedAction; } }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Part List", format1).Height; y = y + 2; //table top PdfDestination tableTopDest = new PdfDestination(page); tableTopDest.Location = new PointF(0, y); tableTopDest.Mode = PdfDestinationMode.Location; tableTopDest.Zoom = 1f; //Draw table PdfTrueTypeFont buttonFont = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); float buttonWidth = 70; float buttonHeight = buttonFont.Height * 1.5f; float tableTop = y; PdfLayoutResult tableLayoutResult = DrawTable(page, y + buttonHeight + 5); //table bottom PdfDestination tableBottomDest = new PdfDestination(tableLayoutResult.Page); tableBottomDest.Location = new PointF(0, tableLayoutResult.Bounds.Bottom); tableBottomDest.Mode = PdfDestinationMode.Location; tableBottomDest.Zoom = 1f; //go to table bottom float x = page.Canvas.ClientSize.Width - buttonWidth; PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); RectangleF buttonBounds = new RectangleF(x, tableTop, buttonWidth, buttonHeight); page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action1 = new PdfGoToAction(tableBottomDest); PdfActionAnnotation annotation1 = new PdfActionAnnotation(buttonBounds, action1); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.Color = Color.LightGray; (page as PdfNewPage).Annotations.Add(annotation1); //go to table top float tableBottom = tableLayoutResult.Bounds.Bottom + 5; buttonBounds = new RectangleF(x, tableBottom, buttonWidth, buttonHeight); tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action2 = new PdfGoToAction(tableTopDest); PdfActionAnnotation annotation2 = new PdfActionAnnotation(buttonBounds, action2); annotation2.Border = new PdfAnnotationBorder(0.75f); annotation2.Color = Color.LightGray; (tableLayoutResult.Page as PdfNewPage).Annotations.Add(annotation2); //goto last page PdfNamedAction action3 = new PdfNamedAction(PdfActionDestination.LastPage); doc.AfterOpenAction = action3; //script String script = "app.alert({" + " cMsg: \"Oh no, you want to leave me.\"," + " nIcon: 3," + " cTitle: \"JavaScript Action\"" + "});"; PdfJavaScriptAction action4 = new PdfJavaScriptAction(script); doc.BeforeCloseAction = action4; //Save pdf file. doc.SaveToFile("Action.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Action.pdf"); }