//测试 /// <summary> /// 转换ppt文件在线预览 /// </summary> /// <param name="fileName">文件路径</param> /// <param name="savepath">保存路径</param> /// <returns></returns> //public bool ppt2Pdf(string fileName, string savepath, HttpResponse Response) //{ // try // { // Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(fileName); // //将ppt保存到指定路径下 savepath:保存路径 // ppt.Save(savepath, Aspose.Slides.Export.SaveFormat.Pdf); // //将ppt保存至数据流中 再通过Response显示在页面中 // MemoryStream memStream = new MemoryStream(); // ppt.Save(memStream, Aspose.Slides.Export.SaveFormat.Pdf); // byte[] bt = memStream.ToArray(); // Response.ContentType = "application/pdf"; // Response.OutputStream.Write(bt, 0, bt.Length); // return true; // } // catch (Exception) // { // return false; // } //} /// <summary> /// 将Word转换为Png /// </summary> /// <param name="filepath">文件地址</param> /// <param name="pageIndex">要转换的页</param> /// <returns></returns> public void Word2Png(string filepath, int pageIndex, HttpResponse Response) { MemoryStream memStream = new MemoryStream(); Aspose.Words.Document doc = new Aspose.Words.Document(filepath); PageInfo pageInfo = doc.GetPageInfo(pageIndex); Document d = new Document(); float scale = 100 / 100.0f; const int Resolution = 96; Size imgSize = pageInfo.GetSizeInPixels(scale, Resolution); using (Bitmap img = new Bitmap(imgSize.Width, imgSize.Height)) { img.SetResolution(Resolution, Resolution); using (Graphics gfx = Graphics.FromImage(img)) { gfx.Clear(Color.White); doc.RenderToScale(pageIndex, gfx, 0, 0, scale); img.Save(memStream, ImageFormat.Png); } } // Send the bitmap data to the output stream. Response.ContentType = "image/png"; byte[] imageData = memStream.ToArray(); Response.OutputStream.Write(imageData, 0, imageData.Length); }
public void PrintPageInfo() { //ExStart //ExFor:PageInfo //ExFor:PageInfo.GetSizeInPixels(Single, Single, Single) //ExFor:PageInfo.GetSpecifiedPrinterPaperSource(PaperSourceCollection, PaperSource) //ExFor:PageInfo.HeightInPoints //ExFor:PageInfo.Landscape //ExFor:PageInfo.PaperSize //ExFor:PageInfo.PaperTray //ExFor:PageInfo.SizeInPoints //ExFor:PageInfo.WidthInPoints //ExSummary:Shows how to print page size and orientation information for every page in a Word document. Document doc = new Document(MyDir + "Rendering.docx"); // The first section has 2 pages. We will assign a different printer paper tray to each one, // whose number will match a kind of paper source. These sources and their Kinds will vary // depending on the installed printer driver. PrinterSettings.PaperSourceCollection paperSources = new PrinterSettings().PaperSources; doc.FirstSection.PageSetup.FirstPageTray = paperSources[0].RawKind; doc.FirstSection.PageSetup.OtherPagesTray = paperSources[1].RawKind; Console.WriteLine("Document \"{0}\" contains {1} pages.", doc.OriginalFileName, doc.PageCount); float scale = 1.0f; float dpi = 96; for (int i = 0; i < doc.PageCount; i++) { // Each page has a PageInfo object, whose index is the respective page's number. PageInfo pageInfo = doc.GetPageInfo(i); // Print the page's orientation and dimensions. Console.WriteLine($"Page {i + 1}:"); Console.WriteLine($"\tOrientation:\t{(pageInfo.Landscape ? "Landscape" : "Portrait")}"); Console.WriteLine($"\tPaper size:\t\t{pageInfo.PaperSize} ({pageInfo.WidthInPoints:F0}x{pageInfo.HeightInPoints:F0}pt)"); Console.WriteLine($"\tSize in points:\t{pageInfo.SizeInPoints}"); Console.WriteLine($"\tSize in pixels:\t{pageInfo.GetSizeInPixels(1.0f, 96)} at {scale * 100}% scale, {dpi} dpi"); // Print the source tray information. Console.WriteLine($"\tTray:\t{pageInfo.PaperTray}"); PaperSource source = pageInfo.GetSpecifiedPrinterPaperSource(paperSources, paperSources[0]); Console.WriteLine($"\tSuitable print source:\t{source.SourceName}, kind: {source.Kind}"); } //ExEnd }
private void UpdatePage() { // This operation can take some time (for the first page) so we set the Cursor to WaitCursor. Cursor cursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; bool canMoveBack = (mPageNumber > 1); navigationFirstPageMenuItem.Enabled = canMoveBack; navigationFirstPageButton.Enabled = canMoveBack; navigationPreviousPageMenuItem.Enabled = canMoveBack; navigationPreviousPageButton.Enabled = canMoveBack; bool canMoveForward = (mPageNumber < mDocument.PageCount); navigationLastPageMenuItem.Enabled = canMoveForward; navigationLastPageButton.Enabled = canMoveForward; navigationNextPageMenuItem.Enabled = canMoveForward; navigationNextPageButton.Enabled = canMoveForward; int pageIndex = mPageNumber - 1; PageInfo pageInfo = mDocument.GetPageInfo(pageIndex); const int Resolution = 96; const float scale = 1.0f; Size imgSize = pageInfo.GetSizeInPixels(scale, Resolution); Bitmap img = new Bitmap(imgSize.Width, imgSize.Height); img.SetResolution(Resolution, Resolution); using (Graphics gfx = Graphics.FromImage(img)) { gfx.Clear(Color.White); mDocument.RenderToScale(pageIndex, gfx, 0, 0, scale); } docPagePictureBox.Width = Math.Max(img.Width + 100, SystemInformation.WorkingArea.Width - SystemInformation.VerticalScrollBarWidth); docPagePictureBox.Height = img.Height + 100; docPagePictureBox.Image = img; statusBar.Text = string.Format("Page {0} of {1}", mPageNumber, mDocument.PageCount); // Restore cursor. Cursor.Current = cursor; }
public void RenderToScale() { //ExStart //ExFor:Document.RenderToScale //ExFor:Document.GetPageInfo //ExFor:PageInfo //ExFor:PageInfo.GetSizeInPixels(Single, Single) //ExSummary:Renders a page of a Word document into a bitmap using a specified zoom factor. Document doc = new Document(MyDir + "Rendering.doc"); PageInfo pageInfo = doc.GetPageInfo(0); // Let's say we want the image at 50% zoom. const float MyScale = 0.50f; // Let's say we want the image at this resolution. const float MyResolution = 200.0f; Size pageSize = pageInfo.GetSizeInPixels(MyScale, MyResolution); using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height)) { img.SetResolution(MyResolution, MyResolution); using (Graphics gr = Graphics.FromImage(img)) { // You can apply various settings to the Graphics object. gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Fill the page background. gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width, pageSize.Height); // Render the page using the zoom. doc.RenderToScale(0, gr, 0, 0, MyScale); } img.Save(MyDir + @"\Artifacts\Rendering.RenderToScale.png"); } //ExEnd }
public static void Run(Document doc, LayoutEnumerator layoutEnumerator, string folderPath) { // Make sure the enumerator is at the beginning of the document. layoutEnumerator.Reset(); for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++) { // Use the document class to find information about the current page. PageInfo pageInfo = doc.GetPageInfo(pageIndex); const float resolution = 150.0f; Size pageSize = pageInfo.GetSizeInPixels(1.0f, resolution); using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height)) { img.SetResolution(resolution, resolution); using (Graphics g = Graphics.FromImage(img)) { // Make the background white. g.Clear(Color.White); // Render the page to the graphics. doc.RenderToScale(pageIndex, g, 0.0f, 0.0f, 1.0f); // Add an outline around each element on the page using the graphics object. AddBoundingBoxToElementsOnPage(layoutEnumerator, g); // Move the enumerator to the next page if there is one. layoutEnumerator.MoveNext(); img.Save(folderPath + $"EnumerateLayoutElements.Page_{pageIndex + 1}.png"); } } } }