private static bool TryReadExplicitDestination(IToken value, Catalog catalog, IPdfTokenScanner pdfScanner, ILog log, out ExplicitDestination destination) { destination = null; if (DirectObjectFinder.TryGet(value, pdfScanner, out ArrayToken valueArray) && TryGetExplicitDestination(valueArray, catalog, log, out destination)) { return(true); } if (DirectObjectFinder.TryGet(value, pdfScanner, out DictionaryToken valueDictionary) && valueDictionary.TryGet(NameToken.D, pdfScanner, out valueArray) && TryGetExplicitDestination(valueArray, catalog, log, out destination)) { return(true); } return(false); }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments(); // instantiate new Document object Document doc = new Document(dataDir + "SetZoomFactor.pdf"); //Set Left Right and Z factors double[] parameters = { 0, 0, 10 }; //Set Explicit Destination ExplicitDestination ED = ExplicitDestination.CreateDestination(1, ExplicitDestinationType.XYZ, parameters); //Set Action GoToAction action = new GoToAction(ED); //Set Open action of document doc.OpenAction = action; //Save the document doc.Save(dataDir + "Zoomed_pdf.pdf"); }
public static void Main(string[] args) { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); // instantiate new Document object Document doc = new Document(dataDir + "input.pdf"); //Set Left Right and Z factors double[] parameters = { 0, 0, 10 }; //Set Explicit Destination ExplicitDestination ED = ExplicitDestination.CreateDestination(1, ExplicitDestinationType.XYZ, parameters); //Set Action GoToAction action = new GoToAction(ED); //Set Open action of document doc.OpenAction = action; //Save the document doc.Save(dataDir + "Zoomed_pdf.pdf"); }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text); int goToPageNumber = int.Parse(pageNumberTextBox.Text); if (goToPageNumber > pdfDocument.Pages.Count) { return; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(xLocationTextBox.Text); float goToY = float.Parse(yLocationTextBox.Text); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) { goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text); } // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Go_To_Page_Open_Action.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
protected void createPdfButton_Click(object sender, EventArgs e) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Display the bookmarks panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseOutlines; try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 12, FontStyle.Regular, GraphicsUnit.Point)); // Add a new PDF page to PDF document PdfPage page1 = pdfDocument.AddPage(); TextElement pageText = new TextElement(0, 0, "Page 1. Destination of a Top Bookmark with Fit Width View Mode.", titleFont); page1.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page2 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 2. Destination of a Top Bookmark with Custom Zoom Level.", titleFont); page2.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page3 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 3. Destination of a Child Bookmark with Fit Width and Height View Mode.", titleFont); page3.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page4 = pdfDocument.AddPage(); pageText = new TextElement(0, page4.PageSize.Height / 2 - 20, "Page 4. Destination of a Top Bookmark for the Middle of the Page.", titleFont); page4.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page5 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 5. Destination of a Child Bookmark with Colored Title.", titleFont); page5.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page6 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 6. Destination of a Child Bookmark with Italic Style Title.", titleFont); page6.AddElement(pageText); // Add a top level bookmark for first page setting destination view mode to fit viewer window horizontally ExplicitDestination page1Destination = new ExplicitDestination(page1, new PointF(0, 0), DestinationViewMode.FitH); Bookmark page1TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark with Fit Width View Mode", page1Destination); page1TopBookmark.Style = PdfBookmarkStyle.Bold; // Add a top level bookmark for second page setting the zoom level to 125% ExplicitDestination page2Destination = new ExplicitDestination(page2, new PointF(0, 0), DestinationViewMode.XYZ); page2Destination.ZoomPercentage = 125; Bookmark page2TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark with Custom Zoom Level", page2Destination); page2TopBookmark.Style = PdfBookmarkStyle.Normal; // Add a child bookmark for third page setting destination view mode to fit viewer window horizontally and vertically ExplicitDestination page3Destination = new ExplicitDestination(page3, new PointF(0, 0), DestinationViewMode.Fit); Bookmark page3ChildBookmark = page2TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Fit Width and Height View Mode", page3Destination); // Add a top level bookmark for fourth page with destination point in the middle of the PDF page ExplicitDestination page4Destination = new ExplicitDestination(page4, new PointF(0, page4.PageSize.Height / 2 - 20)); Bookmark page4TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark for the Middle of the Page", page4Destination); page4TopBookmark.Style = PdfBookmarkStyle.Bold; page4TopBookmark.Color = Color.Blue; // Add a child bookmark with colored text ExplicitDestination page5Destination = new ExplicitDestination(page5, new PointF(0, 0)); Bookmark page5ChildBookmark = page4TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Colored Title", page5Destination); page5ChildBookmark.Color = Color.Red; // Add a child bookmark with italic style text ExplicitDestination page6Destination = new ExplicitDestination(page6, new PointF(0, 0)); Bookmark page6ChildBookmark = page4TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Italic Colored Title", page6Destination); page6ChildBookmark.Style = PdfBookmarkStyle.Italic; page6ChildBookmark.Color = Color.Green; // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Bookmarks.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document pdfDocument.Close(); } }
protected void createPdfButton_Click(object sender, EventArgs e) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add second page to PDF document PdfPage secondPdfPage = pdfDocument.AddPage(); // Add third page to PDF document PdfPage thirdPdfPage = pdfDocument.AddPage(); try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point)); PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Regular, GraphicsUnit.Point)); // The links text font PdfFont linkTextFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); linkTextFont.IsUnderline = true; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create Internal Links in PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add a text in second page TextElement secondPageTextElement = new TextElement(5, 5, "This text is the target of an internal text link", subtitleFont); secondPdfPage.AddElement(secondPageTextElement); // Add a text in third page TextElement thirdPageTextElement = new TextElement(5, 5, "This text is the target of an internal image link", subtitleFont); thirdPdfPage.AddElement(thirdPageTextElement); // Make a text in PDF an internal link to the second page of the PDF document // Add the text element string text = "Click this text to go to the second page of this document!"; float textWidth = linkTextFont.GetTextWidth(text); TextElement linkTextElement = new TextElement(xLocation, yLocation, text, linkTextFont); linkTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(linkTextElement); // Make the text element an internal link to the second page of this document RectangleF linkRectangle = new RectangleF(xLocation, yLocation, textWidth, addElementResult.EndPageBounds.Height); // Create the destination in second page ExplicitDestination secondPageDestination = new ExplicitDestination(secondPdfPage, new PointF(5, 5)); // Create the internal link from text element to second page InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, secondPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Make an image in PDF an internal link to the third page of the PDF document TextElement subtitleTextElement = new TextElement(xLocation, yLocation, "Click the image below to go to the third page of this document:", subtitleFont); addElementResult = pdfPage.AddElement(subtitleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 5; // Add the image element ImageElement linkImageElement = new ImageElement(xLocation, yLocation, 120, Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg")); addElementResult = pdfPage.AddElement(linkImageElement); // Make the image element an internal link to the third page of this document linkRectangle = addElementResult.EndPageBounds; // Create the destination in third page ExplicitDestination thirdPageDestination = new ExplicitDestination(thirdPdfPage, new PointF(5, 5)); // Create the internal link from image element to third page internalLink = new InternalLinkElement(linkRectangle, thirdPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Internal_Links.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document pdfDocument.Close(); } }
private static bool TryGetAction(DictionaryToken actionDictionary, Catalog catalog, IPdfTokenScanner pdfScanner, IReadOnlyDictionary <string, ExplicitDestination> namedDestinations, ILog log, out (bool isExternal, string externalFileName, ExplicitDestination destination) result)
private static bool TryGetExplicitDestination(ArrayToken explicitDestinationArray, Catalog catalog, ILog log, out ExplicitDestination destination) { destination = null; if (explicitDestinationArray == null || explicitDestinationArray.Length == 0) { return(false); } int pageNumber; var pageToken = explicitDestinationArray[0]; if (pageToken is IndirectReferenceToken pageIndirectReferenceToken) { var page = catalog.GetPageByReference(pageIndirectReferenceToken.Data); if (page?.PageNumber == null) { return(false); } pageNumber = page.PageNumber.Value; } else if (pageToken is NumericToken pageNumericToken) { pageNumber = pageNumericToken.Int + 1; } else { var errorMessage = $"{nameof(TryGetExplicitDestination)} No page number given in 'Dest': '{explicitDestinationArray}'."; log.Error(errorMessage); return(false); } var destTypeToken = explicitDestinationArray[1] as NameToken; if (destTypeToken == null) { var errorMessage = $"Missing name token as second argument to explicit destination: {explicitDestinationArray}."; log.Error(errorMessage); destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitPage, ExplicitDestinationCoordinates.Empty); return(true); } if (destTypeToken.Equals(NameToken.XYZ)) { // [page /XYZ left top zoom] var left = explicitDestinationArray[2] as NumericToken; var top = explicitDestinationArray[3] as NumericToken; destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.XyzCoordinates, new ExplicitDestinationCoordinates(left?.Data, top?.Data)); return(true); } if (destTypeToken.Equals(NameToken.Fit)) { // [page /Fit] destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitPage, ExplicitDestinationCoordinates.Empty); return(true); } if (destTypeToken.Equals(NameToken.FitH)) { // [page /FitH top] var top = explicitDestinationArray[2] as NumericToken; destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitHorizontally, new ExplicitDestinationCoordinates(null, top?.Data)); return(true); } if (destTypeToken.Equals(NameToken.FitV)) { // [page /FitV left] var left = explicitDestinationArray[2] as NumericToken; destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitVertically, new ExplicitDestinationCoordinates(left?.Data)); return(true); } if (destTypeToken.Equals(NameToken.FitR)) { // [page /FitR left bottom right top] var left = explicitDestinationArray[2] as NumericToken; var bottom = explicitDestinationArray[3] as NumericToken; var right = explicitDestinationArray[4] as NumericToken; var top = explicitDestinationArray[5] as NumericToken; destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitRectangle, new ExplicitDestinationCoordinates(left?.Data, top?.Data, right?.Data, bottom?.Data)); return(true); } if (destTypeToken.Equals(NameToken.FitB)) { // [page /FitB] destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitBoundingBox, ExplicitDestinationCoordinates.Empty); return(true); } if (destTypeToken.Equals(NameToken.FitBH)) { // [page /FitBH top] destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitBoundingBoxHorizontally, new ExplicitDestinationCoordinates(null, (explicitDestinationArray[2] as NumericToken)?.Data)); return(true); } if (destTypeToken.Equals(NameToken.FitBV)) { // [page /FitBV left] destination = new ExplicitDestination(pageNumber, ExplicitDestinationType.FitBoundingBoxVertically, new ExplicitDestinationCoordinates((explicitDestinationArray[2] as NumericToken)?.Data)); return(true); } return(false); }
public ActionResult CreatePdf(IFormCollection collection) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add second page to PDF document PdfPage secondPdfPage = pdfDocument.AddPage(); // Add third page to PDF document PdfPage thirdPdfPage = pdfDocument.AddPage(); try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 10, FontStyle.Bold, GraphicsUnit.Point)); PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Regular, GraphicsUnit.Point)); // The links text font PdfFont linkTextFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point)); linkTextFont.IsUnderline = true; float xLocation = 5; float yLocation = 5; // Add document title TextElement titleTextElement = new TextElement(xLocation, yLocation, "Create Internal Links in PDF Document", titleFont); AddElementResult addElementResult = pdfPage.AddElement(titleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 15; // Add a text in second page TextElement secondPageTextElement = new TextElement(5, 5, "This text is the target of an internal text link", subtitleFont); secondPdfPage.AddElement(secondPageTextElement); // Add a text in third page TextElement thirdPageTextElement = new TextElement(5, 5, "This text is the target of an internal image link", subtitleFont); thirdPdfPage.AddElement(thirdPageTextElement); // Make a text in PDF an internal link to the second page of the PDF document // Add the text element string text = "Click this text to go to the second page of this document!"; float textWidth = linkTextFont.GetTextWidth(text); TextElement linkTextElement = new TextElement(xLocation, yLocation, text, linkTextFont); linkTextElement.ForeColor = Color.Navy; addElementResult = pdfPage.AddElement(linkTextElement); // Make the text element an internal link to the second page of this document RectangleF linkRectangle = new RectangleF(xLocation, yLocation, textWidth, addElementResult.EndPageBounds.Height); // Create the destination in second page ExplicitDestination secondPageDestination = new ExplicitDestination(secondPdfPage, new PointF(5, 5)); // Create the internal link from text element to second page InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, secondPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); yLocation = addElementResult.EndPageBounds.Bottom + 10; // Make an image in PDF an internal link to the third page of the PDF document TextElement subtitleTextElement = new TextElement(xLocation, yLocation, "Click the image below to go to the third page of this document:", subtitleFont); addElementResult = pdfPage.AddElement(subtitleTextElement); yLocation = addElementResult.EndPageBounds.Bottom + 5; // Add the image element ImageElement linkImageElement = new ImageElement(xLocation, yLocation, 120, m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Images/logo.jpg"); addElementResult = pdfPage.AddElement(linkImageElement); // Make the image element an internal link to the third page of this document linkRectangle = addElementResult.EndPageBounds; // Create the destination in third page ExplicitDestination thirdPageDestination = new ExplicitDestination(thirdPdfPage, new PointF(5, 5)); // Create the internal link from image element to third page internalLink = new InternalLinkElement(linkRectangle, thirdPageDestination); // Add the internal link to PDF document pdfPage.AddElement(internalLink); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Internal_Links.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); try { // Create a HTML to PDF element to add to document HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(collection["urlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish htmlToPdfElement.ConversionDelay = 2; // Add the HTML to PDF element to document pdfPage.AddElement(htmlToPdfElement); int goToPageNumber = int.Parse(collection["pageNumberTextBox"]); if (goToPageNumber > pdfDocument.Pages.Count) { goToPageNumber = 1; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(collection["xLocationTextBox"]); float goToY = float.Parse(collection["yLocationTextBox"]); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(collection["viewModeComboBox"]); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) goToDestination.ZoomPercentage = int.Parse(collection["zoomLevelTextBox"]); // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Go_To_Page_Open_Action.pdf"; return fileResult; } finally { // Close the PDF document pdfDocument.Close(); } }
public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; Document pdfDocument = null; try { // Convert a HTML page to a PDF document object pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]); int goToPageNumber = int.Parse(collection["pageNumberTextBox"]); if (goToPageNumber > pdfDocument.Pages.Count) { goToPageNumber = 1; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(collection["xLocationTextBox"]); float goToY = float.Parse(collection["yLocationTextBox"]); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(collection["viewModeComboBox"]); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) { goToDestination.ZoomPercentage = int.Parse(collection["zoomLevelTextBox"]); } // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Go_To_Page_Open_Action.pdf"; return(fileResult); } finally { // Close the PDF document if (pdfDocument != null) { pdfDocument.Close(); } } }
/// <inheritdoc /> /// <summary> /// Create a new <see cref="DocumentBookmarkNode"/>. /// </summary> public DocumentBookmarkNode(string title, int level, ExplicitDestination destination, IReadOnlyList <BookmarkNode> children) : base(title, level, children) { Destination = destination ?? throw new ArgumentNullException(nameof(destination)); PageNumber = destination.PageNumber; }
protected void createPdfButton_Click(object sender, EventArgs e) { // Get the server IP and port String serverIP = textBoxServerIP.Text; uint serverPort = uint.Parse(textBoxServerPort.Text); // Create a PDF document Document pdfDocument = new Document(serverIP, serverPort); // Set optional service password if (textBoxServicePassword.Text.Length > 0) { pdfDocument.ServicePassword = textBoxServicePassword.Text; } // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Add second page to PDF document PdfPage secondPdfPage = pdfDocument.AddPage(); // Add third page to PDF document PdfPage thirdPdfPage = pdfDocument.AddPage(); // The titles font used to mark various sections of the PDF document PdfFont titleFont = new PdfFont("Times New Roman", 10, true); titleFont.Bold = true; PdfFont subtitleFont = new PdfFont("Times New Roman", 8, true); // The links text font PdfFont linkTextFont = new PdfFont("Times New Roman", 8, true); linkTextFont.Bold = true; linkTextFont.Underline = true; // Add document title TextElement titleTextElement = new TextElement(5, 5, "Create Internal Links in PDF Document", titleFont); pdfPage.AddElement(titleTextElement); // Add a text in second page TextElement secondPageTextElement = new TextElement(5, 5, "This text is the target of an internal text link", subtitleFont); secondPdfPage.AddElement(secondPageTextElement); // Add a text in third page TextElement thirdPageTextElement = new TextElement(5, 5, "This text is the target of an internal image link", subtitleFont); thirdPdfPage.AddElement(thirdPageTextElement); // Make a text in PDF an internal link to the second page of the PDF document // Add the text element string text = "Click this text to go to the second page of this document!"; TextElement linkTextElement = new TextElement(5, 30, 200, text, linkTextFont); linkTextElement.ForeColor = RgbColor.Navy; pdfPage.AddElement(linkTextElement); // Make the text element an internal link to the second page of this document RectangleFloat linkRectangle = RectangleFloat.Empty; // Create the destination in second page ExplicitDestination secondPageDestination = new ExplicitDestination(secondPdfPage, new PointFloat(5, 5)); // Create the internal link from text element to second page InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, secondPageDestination); // Add the internal link to PDF document pdfDocument.AddElement(internalLink, 0, true, true, 0, true, false); // Make an image in PDF an internal link to the third page of the PDF document TextElement subtitleTextElement = new TextElement(0, 0, "Click the image below to go to the third page of this document:", subtitleFont); pdfDocument.AddElement(subtitleTextElement, 10); // Add the image element ImageElement linkImageElement = new ImageElement(0, 0, 120, Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg")); pdfDocument.AddElement(linkImageElement); // Make the image element an internal link to the third page of this document linkRectangle = RectangleFloat.Empty; // Create the destination in third page ExplicitDestination thirdPageDestination = new ExplicitDestination(thirdPdfPage, new PointFloat(5, 5)); // Create the internal link from image element to third page internalLink = new InternalLinkElement(linkRectangle, thirdPageDestination); // Add the internal link to PDF document pdfDocument.AddElement(internalLink, 0, true, true, 0, true, false); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Internal_Links.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); try { // Create a HTML to PDF element to add to document HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(urlTextBox.Text); // Optionally set a delay before conversion to allow asynchonous scripts to finish htmlToPdfElement.ConversionDelay = 2; // Add the HTML to PDF element to document pdfPage.AddElement(htmlToPdfElement); int goToPageNumber = int.Parse(pageNumberTextBox.Text); if (goToPageNumber > pdfDocument.Pages.Count) { return; } // Get destination PDF page PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1]; // Get the destination point in PDF page float goToX = float.Parse(xLocationTextBox.Text); float goToY = float.Parse(yLocationTextBox.Text); PointF goToLocation = new PointF(goToX, goToY); // Get the destination view mode DestinationViewMode viewMode = SelectedViewMode(); // Create the destination in PDF document ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode); // Set the zoom level when the destination is displayed if (viewMode == DestinationViewMode.XYZ) { goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text); } // Set the document Go To open action pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Set_Initial_Zoom_Level.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); } finally { // Close the PDF document pdfDocument.Close(); } }
public ActionResult CreatePdf(IFormCollection collection) { // Create a PDF document Document pdfDocument = new Document(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Display the bookmarks panel when the PDF document is opened in a PDF viewer pdfDocument.ViewerPreferences.PageMode = ViewerPageMode.UseOutlines; try { // The titles font used to mark various sections of the PDF document PdfFont titleFont = pdfDocument.AddFont(new Font("Times New Roman", 12, FontStyle.Regular, GraphicsUnit.Point)); // Add a new PDF page to PDF document PdfPage page1 = pdfDocument.AddPage(); TextElement pageText = new TextElement(0, 0, "Page 1. Destination of a Top Bookmark with Fit Width View Mode.", titleFont); page1.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page2 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 2. Destination of a Top Bookmark with Custom Zoom Level.", titleFont); page2.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page3 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 3. Destination of a Child Bookmark with Fit Width and Height View Mode.", titleFont); page3.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page4 = pdfDocument.AddPage(); pageText = new TextElement(0, page4.PageSize.Height / 2 - 20, "Page 4. Destination of a Top Bookmark for the Middle of the Page.", titleFont); page4.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page5 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 5. Destination of a Child Bookmark with Colored Title.", titleFont); page5.AddElement(pageText); // Add a new PDF page to PDF document PdfPage page6 = pdfDocument.AddPage(); pageText = new TextElement(0, 0, "Page 6. Destination of a Child Bookmark with Italic Style Title.", titleFont); page6.AddElement(pageText); // Add a top level bookmark for first page setting destination view mode to fit viewer window horizontally ExplicitDestination page1Destination = new ExplicitDestination(page1, new PointF(0, 0), DestinationViewMode.FitH); Bookmark page1TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark with Fit Width View Mode", page1Destination); page1TopBookmark.Style = PdfBookmarkStyle.Bold; // Add a top level bookmark for second page setting the zoom level to 125% ExplicitDestination page2Destination = new ExplicitDestination(page2, new PointF(0, 0), DestinationViewMode.XYZ); page2Destination.ZoomPercentage = 125; Bookmark page2TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark with Custom Zoom Level", page2Destination); page2TopBookmark.Style = PdfBookmarkStyle.Normal; // Add a child bookmark for third page setting destination view mode to fit viewer window horizontally and vertically ExplicitDestination page3Destination = new ExplicitDestination(page3, new PointF(0, 0), DestinationViewMode.Fit); Bookmark page3ChildBookmark = page2TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Fit Width and Height View Mode", page3Destination); // Add a top level bookmark for fourth page with destination point in the middle of the PDF page ExplicitDestination page4Destination = new ExplicitDestination(page4, new PointF(0, page4.PageSize.Height / 2 - 20)); Bookmark page4TopBookmark = pdfDocument.Bookmarks.AddNewBookmark("Top Bookmark for the Middle of the Page", page4Destination); page4TopBookmark.Style = PdfBookmarkStyle.Bold; page4TopBookmark.Color = Color.Blue; // Add a child bookmark with colored text ExplicitDestination page5Destination = new ExplicitDestination(page5, new PointF(0, 0)); Bookmark page5ChildBookmark = page4TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Colored Title", page5Destination); page5ChildBookmark.Color = Color.Red; // Add a child bookmark with italic style text ExplicitDestination page6Destination = new ExplicitDestination(page6, new PointF(0, 0)); Bookmark page6ChildBookmark = page4TopBookmark.DescendantBookmarks.AddNewBookmark("Child Bookmark with Italic Colored Title", page6Destination); page6ChildBookmark.Style = PdfBookmarkStyle.Italic; page6ChildBookmark.Color = Color.Green; // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Bookmarks.pdf"; return(fileResult); } finally { // Close the PDF document pdfDocument.Close(); } }