How to split the PDF documents horizontally in the PDF Viewer
PDF Viewer
PDF Viewer control supports viewing, reviewing, and printing PDF files in ASP.NET Web Forms applications. The hyperlink and table of contents support provides easy navigation within and outside the PDF files. The form-filling support provides a platform to fill, flatten, save, and print PDF files with AcroForm. PDF files can be reviewed with text markup annotation tools.
Split PDF documents horizontally
You can split the PDF documents horizontally by sending the X and Y coordinates to the server side using the Ajax request in the mouse up event of the PDF viewer.
The following code illustrates how to draw the line horizontally in mouse events and send the data to the server side using the Ajax request.
JavaScript
<script type="text/javascript"> var isPageClicked = false; var horizontalElement = null; var count = 0, prevX; $("#PdfViewer1").mousedown(function (event) { if ($(event.target).hasClass("e-pdfviewer-selectiondiv")) { isPageClicked = true; horizontalElement = document.createElement('hr'); horizontalElement.style.position = 'absolute'; horizontalElement.style.top = event.offsetY + 'px'; horizontalElement.style.left = event.offsetX + 'px'; horizontalElement.style.height = '5px'; prevX = event.offsetX; $(horizontalElement).appendTo(event.target); } }); $("#PdfViewer1").mousemove(function (event) { // draws the line horizontally if ($(event.target).hasClass("e-pdfviewer-selectiondiv")) { if (isPageClicked) { horizontalElement.style.width = count + 'px'; if (prevX < event.offsetX) count++; else if (prevX < event.offsetX) count--; prevX = event.offsetX; } } }); $("#PdfViewer1").mouseup(function (event) { isPageClicked = false; if ($(event.target).hasClass("e-pdfviewer-selectiondiv")) { var pageNum = (event.target.id).split('_')[1]; var pdfviewer = $("#PdfViewer1").data("ejPdfViewer"); var jsonObject = { xCoordinate: event.offsetX, yCoordinate: event.offsetY, pageNumber: pageNum }; //Sends X-Coordinate, Y-Coordinate and Page number to the server side as Json data through Ajax request $.ajax({ type: 'POST', url: '../api/PdfViewer/SplitHorizontal', crossDomain: true, contentType: 'application/json; charset=utf-8', dataType: 'json', data: JSON.stringify(jsonObject), traditional: true, success: function () { } }); } $(horizontalElement).remove(); count = 0; }); </script>
The following code illustrates how to split the PDF documents horizontally with X and Y coordinates and save the created PDF documents in the Data folder.
C#
public void SplitHorizontal(Dictionary<string, string> jsonResult) { PdfViewerHelper helper = new PdfViewerHelper(); if (jsonResult != null) { //Gets the X Coordinate and Y Coordinate int pageIndex = Convert.ToInt32(jsonResult["pageNumber"].ToString()) - 1; int x = Convert.ToInt32(ConvertPixelToPoint(jsonResult["xCoordinate"].ToString())); int y = Convert.ToInt32(ConvertPixelToPoint(jsonResult["yCoordinate"].ToString())); float width = (float)x; float height = (y > 792) ? (float)(y / 1.3712) : (float)y; PdfLoadedDocument loadedDocument = new PdfLoadedDocument(helper.DocumentStream); //Load the page PdfLoadedPage loadedPage = loadedDocument.Pages[pageIndex] as PdfLoadedPage; //Create the template from the page. PdfTemplate template = loadedPage.CreateTemplate(); //Create new PDF documents PdfDocument document = new PdfDocument(); document.PageSettings.SetMargins(0, 0, 0, 0); PdfDocument document2 = new PdfDocument(); document2.PageSettings.SetMargins(0, 0, 0, 0); { //Add second section AddNewSection(document); for (int i = 0; i < pageIndex; i++) { PdfTemplate pageTemplate = loadedDocument.Pages[i].CreateTemplate(); LoadPage(pageTemplate, document); } //Height of the custom panel System.Drawing.Rectangle clippedRectangle = new System.Drawing.Rectangle(); if (y > loadedDocument.Pages[pageIndex].Size.Height) { float exactHeight = (float)y - 1086; CropPage(document, template, (int)loadedDocument.Pages[pageIndex].Size.Width, (float)(exactHeight / 1.37)); clippedRectangle.Location = new System.Drawing.Point(0, (int)(exactHeight / 1.37)); clippedRectangle.Size = new System.Drawing.Size((int)loadedDocument.Pages[pageIndex].Size.Width, (int)(792 - (exactHeight / 1.37))); } else { CropPage(document, template, (int)loadedDocument.Pages[pageIndex].Size.Width, height); clippedRectangle.Location = new System.Drawing.Point(0, y > 792 ? (int)height : (int)(height / 1.37)); clippedRectangle.Size = new System.Drawing.Size((int)loadedDocument.Pages[pageIndex].Size.Width, y > 792 ? (int)(792 - height) : (int)(792 - (height / 1.37))); } if (pageIndex != loadedDocument.Pages.Count - 1) { //Add section AddNewSection(document2); PdfPage remainingPage = document2.Sections[document2.Sections.Count - 1].Pages.Add(); PdfGraphics remainingPageGraphics = remainingPage.Graphics; PdfGraphics templateGraphics = template.Graphics; remainingPageGraphics.SetClip(clippedRectangle); remainingPageGraphics.DrawPdfTemplate(template, new PointF(0, 0)); document2.Sections[document2.Sections.Count - 1].PageSettings.Height = clippedRectangle.Height; document2.Sections[document2.Sections.Count - 1].PageSettings.Width = clippedRectangle.Width; MemoryStream ms = new MemoryStream(); document2.Save(ms); AddNewSection(document2); int index = pageIndex + 1; for (int i = index; i < loadedDocument.Pages.Count; i++) { PdfTemplate pageTemplate = loadedDocument.Pages[i].CreateTemplate(); LoadPage(pageTemplate, document2); } } } // Saves the created documents in Data folder MemoryStream stream = new MemoryStream(); document.Save(stream); stream.Position = 0; document.Save(HttpContext.Current.Server.MapPath("~/Data/output1.pdf")); document.Close(true); MemoryStream stream2 = new MemoryStream(); document2.Save(stream2); stream2.Position = 0; document2.Save(HttpContext.Current.Server.MapPath("~/Data/output2.pdf")); document2.Close(true); } } private void AddNewSection(PdfDocument document) { PdfSection pdfSec = document.Sections.Add(); pdfSec.PageSettings.Width = 612; pdfSec.PageSettings.Height = 792; } private void CropPage(PdfDocument document, PdfTemplate template, float width, float height) { PdfSection pdfSec = document.Sections.Add(); pdfSec.PageSettings.Width = width > 612 ? 612 : (float)(width); pdfSec.PageSettings.Height = height; //Add the page PdfPage page = document.Sections[document.Sections.Count - 1].Pages.Add(); //Create the graphics PdfGraphics graphics = page.Graphics; //Draw the template graphics.DrawPdfTemplate(template, new PointF(0, 0)); } private void LoadPage(PdfTemplate pageTemplate, PdfDocument document) { PdfPage pdfPage = document.Sections[document.Sections.Count - 1].Pages.Add(); PdfGraphics pageGraphics = pdfPage.Graphics; pageGraphics.DrawPdfTemplate(pageTemplate, new PointF(0, 0)); } private float ConvertPixelToPoint(string value) { //Convets pixel to point float floatVal = float.Parse(value); return floatVal * 72 / 96; }