簡(jiǎn)便實(shí)用:在 ASP.NET Core 中使用GrapeCity.Documents.Pdf實(shí)現(xiàn) PDF 的加載與顯示
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
前言 在Web應(yīng)用開發(fā)中,經(jīng)常需要實(shí)現(xiàn)PDF文件的加載和顯示功能。首先說明,該插件是收費(fèi)的。 實(shí)現(xiàn)步驟 1)在服務(wù)器端創(chuàng)建PDF
//Define Environment variable to access web root folder private IWebHostEnvironment Environment; public IndexModel(ILogger<IndexModel> logger, IWebHostEnvironment _environment) { _logger = logger; Environment = _environment; CreatePDF(); }
public void CreatePDF() { const int FontSize = 12; //Define an instance of GcPdfDocument var doc = new GcPdfDocument(); //Add a new page var page = doc.Pages.Add(); var g = page.Graphics; //Initialize TextLayout to render text var tl = g.CreateTextLayout();
//Add an image to PDF document var img = Image.FromFile(Path.Combine("Resources", "ImagesBis", "2020-website-gcdocs-headers_tall.png")); var rc = page.Bounds; rc.Height *= 0.65f; g.DrawImage(img, rc, null, ImageAlign.StretchImage); //Define text format settings var ip = new PointF(48, 72); var font = Font.FromFile(Path.Combine("Resources", "Fonts", "OpenSans-Regular.ttf")); var tfCap = new TextFormat() { Font = font, FontSize = FontSize * 1.6f, ForeColor = Color.White }; var tf = new TextFormat() { Font = font, FontSize = FontSize, ForeColor = Color.White }; tl.MaxWidth = 72 * 5; // Add Header: tl.AppendLine("Fast, Efficient Document APIs for .NET 5 and Java Applications", tfCap); tl.AppendLine(tfCap); tl.AppendLine("Take total control of your documents with ultra-fast, low-footprint APIs for enterprise apps.", tf); tl.AppendLine(tf); g.DrawTextLayout(tl, ip); // Add Bullet list: ip.Y += tl.ContentHeight; tl.Clear(); const string bullet = "\x2022\x2003"; tl.FirstLineIndent = -g.MeasureString(bullet, tf).Width; tl.ParagraphSpacing += 4; tl.Append(bullet, tf); tl.AppendLine("Generate, load, edit, save XLSX spreadsheets, PDF, Images, and DOCX files using C# .NET, VB.NET, or Java", tf); tl.Append(bullet, tf); tl.AppendLine("View, edit, print, fill and submit documents in JavaScript PDF Viewer and PDF Editor.", tf); tl.Append(bullet, tf); tl.AppendLine("Compatible on Windows, macOS, and Linux", tf); tl.Append(bullet, tf); tl.AppendLine("No dependencies on Excel, Word, or Acrobat", tf); tl.Append(bullet, tf); tl.AppendLine("Deploy to a variety of cloud-based services, including Azure, AWS, and AWS Lambda", tf); tl.Append(bullet, tf); tl.AppendLine("Product available individually or as a bundle", tf); //Render text g.DrawTextLayout(tl, ip); //Save the document to web root folder doc.Save(Path.Combine(Environment.WebRootPath, "sample.pdf")); } 實(shí)現(xiàn)效果如下所示(用Adobe打開): 2)加載和查看PDF 在實(shí)現(xiàn)步驟1)中,小編實(shí)現(xiàn)了如何新建一個(gè)PDF的過程,但是新建的PDF需要在Adobe中打開,那么有沒有一種可以直接在瀏覽器中編輯和修改PDF的編輯器呢?答案是肯定的。接下來小編就將繼續(xù)為大家介紹一下如何使用JavaScript實(shí)現(xiàn)一個(gè)加載和修改PDF的編輯器的步驟:
npm install @grapecity/gcpdfviewer
<div id="root" style="height:600px;"></div> <script src="~/node_modules/@@grapecity/gcpdfviewer/gcpdfviewer.js"></script> <script> window.onload = function () { var viewer = new GcPdfViewer("#root", { /* Specify options here */ } ); viewer.addDefaultPanels(); viewer.open("sample.pdf"); } </script>
在第3步實(shí)現(xiàn)的PDF編輯器中提供了一個(gè)注釋編輯器功能,用于在文檔中添加或刪除不同類型的注釋,例如文本注釋,圓圈注釋,圖章注釋,編輯注釋等。下面的GIF就是一個(gè)圓圈注釋的例子: 該文章在 2024/1/27 9:14:16 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |