private static void CreateUriActions(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("Uri actions:", font, blackBrush, 400, 420); document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 440, 200, 20); slo.X = 500; slo.Y = 450; document.Pages[i].Graphics.DrawString("Go to xfiniumpdf.com", 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, 440, 200, 20); // Create an uri action and attach it to the link. PdfUriAction uriAction = new PdfUriAction(); uriAction.URI = "http://www.xfiniumpdf.com"; link.Action = uriAction; } }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PdfFixedDocument document = new PdfFixedDocument(); document.DisplayMode = PdfDisplayMode.UseOutlines; PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 216); PdfBrush blackBrush = new PdfBrush(); for (int i = 0; i < 10; i++) { PdfPage page = document.Pages.Add(); page.Graphics.DrawString((i + 1).ToString(), helvetica, blackBrush, 50, 50); } PdfOutlineItem root = new PdfOutlineItem(); root.Title = "Contents"; root.VisualStyle = PdfOutlineItemVisualStyle.Bold; root.Color = new PdfRgbColor(255, 0, 0); document.Outline.Add(root); for (int i = 0; i < document.Pages.Count; i++) { // Create a destination to target page. PdfPageDirectDestination pageDestination = new PdfPageDirectDestination(); pageDestination.Page = document.Pages[i]; pageDestination.Top = 0; pageDestination.Left = 0; // Inherit current zoom pageDestination.Zoom = 0; // Create a go to action to be executed when the outline is clicked, // the go to action goes to specified destination. PdfGoToAction gotoPage = new PdfGoToAction(); gotoPage.Destination = pageDestination; // Create the outline in the table of contents PdfOutlineItem outline = new PdfOutlineItem(); outline.Title = string.Format("Go to page {0}", i + 1); outline.VisualStyle = PdfOutlineItemVisualStyle.Italic; outline.Action = gotoPage; root.Items.Add(outline); } root.Expanded = true; // Create an outline that will launch a link in the browser. PdfUriAction uriAction = new PdfUriAction(); uriAction.URI = "http://www.xfiniumsoft.com/"; PdfOutlineItem webOutline = new PdfOutlineItem(); webOutline.Title = "http://www.xfiniumsoft.com/"; webOutline.Color = new PdfRgbColor(0, 0, 255); webOutline.Action = uriAction; document.Outline.Add(webOutline); SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") }; return output; }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. using (PdfDocument pdf = new PdfDocument()) { PdfUriAction uriAction = pdf.CreateHyperlinkAction(new Uri("http://www.google.com")); PdfActionArea annotation = pdf.Pages[0].AddActionArea(10, 50, 100, 100, uriAction); annotation.BorderColor = new PdfRgbColor(0, 0, 0); annotation.BorderDashPattern = new PdfDashPattern(new float[] { 3, 2 }); pdf.Save("UriAction.pdf"); } Process.Start("UriAction.pdf"); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. using (PdfDocument pdf = new PdfDocument()) { PdfUriAction uriAction = pdf.CreateHyperlinkAction(new Uri("http://www.google.com")); PdfActionArea annotation = pdf.Pages[0].AddActionArea(10, 50, 100, 100, uriAction); annotation.Border.Color = new PdfRgbColor(0, 0, 0); annotation.Border.DashPattern = new PdfDashPattern(new float[] { 3, 2 }); annotation.Border.Width = 1; annotation.Border.Style = PdfMarkerLineStyle.Dashed; pdf.Save("UriAction.pdf"); } Console.WriteLine($"The output is located in {Environment.CurrentDirectory}"); }
/// <summary> /// Converts simple XHTML to a formatted content object. /// </summary> /// <param name="html"></param> /// <returns></returns> private PdfFormattedContent ConvertHtmlToFormattedContent(Stream html) { PdfFormattedContent fc = new PdfFormattedContent(); PdfFormattedParagraph currentParagraph = null; PdfUriAction currentLinkAction = null; PdfFormattedTextBlock bullet = null; float currentIndent = 0; // Create a default font. fonts.Push(new PdfStandardFont(PdfStandardFontFace.Helvetica, 10)); // Create a default text color. textColors.Push(new PdfBrush(PdfRgbColor.Black)); System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(html); xmlReader.MoveToContent(); while (xmlReader.Read()) { switch (xmlReader.NodeType) { case System.Xml.XmlNodeType.Element: switch (xmlReader.Name) { case "p": currentParagraph = new PdfFormattedParagraph(); //currentParagraph.SpacingBefore = 3; //currentParagraph.SpacingAfter = 3; fc.Paragraphs.Add(currentParagraph); break; case "br": if (currentParagraph.Blocks.Count > 0) { PdfFormattedTextBlock textBlock = currentParagraph.Blocks[currentParagraph.Blocks.Count - 1] as PdfFormattedTextBlock; textBlock.Text = textBlock.Text + "\r\n"; } else { PdfFormattedTextBlock textBlock = new PdfFormattedTextBlock("\r\n", ActiveFont); currentParagraph.Blocks.Add(textBlock); } break; case "a": while (xmlReader.MoveToNextAttribute()) { if (xmlReader.Name == "href") { currentLinkAction = new PdfUriAction(); currentLinkAction.URI = xmlReader.Value; } } break; case "font": while (xmlReader.MoveToNextAttribute()) { if (xmlReader.Name == "color") { PdfBrush color = ActiveTextColor; string colorCode = xmlReader.Value; // #RRGGBB if (colorCode.StartsWith("#") && (colorCode.Length == 7)) { byte r = byte.Parse(colorCode.Substring(1, 2), System.Globalization.NumberStyles.HexNumber); byte g = byte.Parse(colorCode.Substring(3, 2), System.Globalization.NumberStyles.HexNumber); byte b = byte.Parse(colorCode.Substring(5, 2), System.Globalization.NumberStyles.HexNumber); color = new PdfBrush(new PdfRgbColor(r, g, b)); } textColors.Push(color); } } break; case "ul": //bullet = new PdfFormattedTextBlock("\x95 ", ActiveFont); currentIndent += 18; break; case "li": currentParagraph = new PdfFormattedParagraph(); currentParagraph.SpacingBefore = 3; currentParagraph.SpacingAfter = 3; currentParagraph.Bullet = bullet; currentParagraph.LeftIndentation = currentIndent; fc.Paragraphs.Add(currentParagraph); break; case "b": case "strong": PdfFont boldFont = CopyFont(ActiveFont); boldFont.Bold = true; fonts.Push(boldFont); break; case "i": case "em": PdfFont italicFont = CopyFont(ActiveFont); italicFont.Italic = true; fonts.Push(italicFont); break; case "u": PdfFont underlineFont = CopyFont(ActiveFont); underlineFont.Underline = true; fonts.Push(underlineFont); break; case "span": if (currentParagraph == null) { currentParagraph = new PdfFormattedParagraph(); fc.Paragraphs.Add(currentParagraph); } break; case "h1": case "h2": currentParagraph = new PdfFormattedParagraph(); currentParagraph.SpacingBefore = 6; currentParagraph.SpacingAfter = 6; fc.Paragraphs.Add(currentParagraph); fonts.Push(CopyFont(new PdfStandardFont(PdfStandardFontFace.Helvetica, 17))); break; case "h3": case "h4": currentParagraph = new PdfFormattedParagraph(); currentParagraph.SpacingBefore = 6; //currentParagraph.SpacingAfter = 6; fc.Paragraphs.Add(currentParagraph); fonts.Push(CopyFont(new PdfStandardFont(PdfStandardFontFace.Helvetica, 14))); break; case "h5": case "h6": currentParagraph = new PdfFormattedParagraph(); currentParagraph.SpacingBefore = 3; currentParagraph.SpacingAfter = 3; fc.Paragraphs.Add(currentParagraph); fonts.Push(CopyFont(new PdfStandardFont(PdfStandardFontFace.Helvetica, 11))); break; } break; case System.Xml.XmlNodeType.EndElement: switch (xmlReader.Name) { case "a": currentLinkAction = null; break; case "ul": //bullet = null; currentIndent -= 18; break; case "b": case "strong": case "i": case "em": case "u": case "h1": case "h2": case "h3": case "h4": case "h5": case "h6": currentParagraph = new PdfFormattedParagraph(); currentParagraph.SpacingBefore = 0; currentParagraph.SpacingAfter = 0; fc.Paragraphs.Add(currentParagraph); fonts.Pop(); break; case "font": if (textColors.Count > 1) { textColors.Pop(); } break; } break; case System.Xml.XmlNodeType.Text: string text = xmlReader.Value; if (currentParagraph != null && text != null && text.Length > 0) { // Remove spaces from text that do not have meaning in HTML. text = text.Replace("\r", ""); text = text.Replace("\n", ""); text = text.Replace("\t", " "); PdfFormattedTextBlock block = new PdfFormattedTextBlock(text, ActiveFont); block.TextColor = ActiveTextColor; if (currentLinkAction != null) { block.Action = currentLinkAction; // Make the links blue. block.TextColor = new PdfBrush(PdfRgbColor.Blue); } currentParagraph.Blocks.Add(block); } break; } } return(fc); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PdfFixedDocument document = new PdfFixedDocument(); document.DisplayMode = PdfDisplayMode.UseOutlines; PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 216); PdfBrush blackBrush = new PdfBrush(); for (int i = 0; i < 10; i++) { PdfPage page = document.Pages.Add(); page.Graphics.DrawString((i + 1).ToString(), helvetica, blackBrush, 50, 50); } PdfOutlineItem root = new PdfOutlineItem(); root.Title = "Contents"; root.VisualStyle = PdfOutlineItemVisualStyle.Bold; root.Color = new PdfRgbColor(255, 0, 0); document.Outline.Add(root); for (int i = 0; i < document.Pages.Count; i++) { // Create a destination to target page. PdfPageDirectDestination pageDestination = new PdfPageDirectDestination(); pageDestination.Page = document.Pages[i]; pageDestination.Top = 0; pageDestination.Left = 0; // Inherit current zoom pageDestination.Zoom = 0; // Create a go to action to be executed when the outline is clicked, // the go to action goes to specified destination. PdfGoToAction gotoPage = new PdfGoToAction(); gotoPage.Destination = pageDestination; // Create the outline in the table of contents PdfOutlineItem outline = new PdfOutlineItem(); outline.Title = string.Format("Go to page {0}", i + 1); outline.VisualStyle = PdfOutlineItemVisualStyle.Italic; outline.Action = gotoPage; root.Items.Add(outline); } root.Expanded = true; // Create an outline that will launch a link in the browser. PdfUriAction uriAction = new PdfUriAction(); uriAction.URI = "http://www.xfiniumsoft.com/"; PdfOutlineItem webOutline = new PdfOutlineItem(); webOutline.Title = "http://www.xfiniumsoft.com/"; webOutline.Color = new PdfRgbColor(0, 0, 255); webOutline.Action = uriAction; document.Outline.Add(webOutline); SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") }; return(output); }