public void WritePageInfo() { //ExStart //ExFor:PageInfo //ExFor:PageInfo.PaperSize //ExFor:PageInfo.PaperTray //ExFor:PageInfo.Landscape //ExFor:PageInfo.WidthInPoints //ExFor:PageInfo.HeightInPoints //ExSummary:Retrieves page size and orientation information for every page in a Word document. Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Rendering.doc"); Console.WriteLine("Document \"{0}\" contains {1} pages.", doc.OriginalFileName, doc.PageCount); for (int i = 0; i < doc.PageCount; i++) { PageInfo pageInfo = doc.GetPageInfo(i); Console.WriteLine( "Page {0}. PaperSize:{1} ({2:F0}x{3:F0}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.PaperSize, pageInfo.WidthInPoints, pageInfo.HeightInPoints, pageInfo.Landscape ? "Landscape" : "Portrait", pageInfo.PaperTray); } //ExEnd }
//测试 /// <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); }
/// <summary> /// Common method for 'Page' and 'Print' /// </summary> /// <param name="doc"></param> /// <param name="pageNumber">Starts with one</param> /// <returns></returns> private PageView PreparePageView(Document doc, int pageNumber) { var so = new HtmlFixedSaveOptions() { ExportEmbeddedFonts = true, ExportEmbeddedImages = true, ExportEmbeddedCss = true, CssClassNamesPrefix = "pg" + pageNumber, ShowPageBorder = false, Encoding = UTF8WithoutBom, PageCount = 1, PageIndex = pageNumber - 1 }; using (var stream = new MemoryStream()) { doc.Save(stream, so); var pageInfo = doc.GetPageInfo(pageNumber - 1); var size = pageInfo.GetSizeInPixels(1, 72); return(new PageView() { width = size.Width, height = size.Height, angle = 0, number = pageNumber, data = UTF8WithoutBom.GetString(stream.ToArray()).Replace("letter-spacing:-107374182.4pt;", "") }); } }
public void RenderToScale() { //ExStart //ExFor:Document.RenderToScale //ExFor:Document.GetPageInfo //ExFor:PageInfo //ExFor:PageInfo.GetSizeInPixels //ExSummary:Renders a page of a Word document into a bitmap using a specified zoom factor. Aspose.Words.Document doc = new Aspose.Words.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 + "Rendering.RenderToScale Out.png"); } //ExEnd }
public HttpResponseMessage DocumentInfo(RequestData request) { Opts.AppName = "Viewer"; Opts.FileName = request.fileName; Opts.FolderName = request.folderName; Opts.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; try { if (Opts.FolderName.Contains("..")) { throw new Exception("Break-in attempt"); } var doc = new Document(Opts.WorkingFileName); var lc = new LayoutCollector(doc); PrepareInternalLinks(doc, lc); var lst = new List <PageView>(doc.PageCount); for (int i = 0; i < doc.PageCount; i++) { var pageInfo = doc.GetPageInfo(i); var size = pageInfo.GetSizeInPixels(1, 72); lst.Add(new PageView() { width = size.Width, height = size.Height, angle = 0, number = i + 1 }); } return(Request.CreateResponse(HttpStatusCode.OK, new PageParametersResponse(request.fileName, lst, PrepareNavigationPaneList(doc, lc)))); } catch (Exception ex) { return(ExceptionResponse(ex)); } }
private static bool SavePdfToStream(string inputFile, MemoryStream fileStream, MemoryStream documentStream, ref bool isLandscape) { var extension = Path.GetExtension(inputFile); if (string.IsNullOrEmpty(extension)) throw new ArgumentException(inputFile); try { fileStream.Position = 0; switch (extension.ToUpperInvariant()) { case ".DOC": case ".DOCX": case ".RTF": case ".DOT": case ".DOTX": var doc = new Aspose.Words.Document(fileStream); if (doc.PageCount > 0) { var pageInfo = doc.GetPageInfo(0); isLandscape = pageInfo.WidthInPoints > pageInfo.HeightInPoints; } doc.Save(documentStream, Aspose.Words.SaveFormat.Pdf); break; case ".XLS": case ".XLSX": var workbook = new Aspose.Cells.Workbook(fileStream); for (var i = 0; i < workbook.Worksheets.Count; i++) { if (!workbook.Worksheets[i].IsVisible) continue; isLandscape = workbook.Worksheets[i].PageSetup.Orientation == Aspose.Cells.PageOrientationType.Landscape; break; } workbook.Save(documentStream, Aspose.Cells.SaveFormat.Pdf); break; //Microsoft Visio case ".VSD": case ".VSS": case ".VST": case ".VSX": case ".VTX": case ".VDW": case ".VDX": var vsdDiagram = new Aspose.Diagram.Diagram(fileStream); if (vsdDiagram.Pages.Count > 0) isLandscape = vsdDiagram.Pages[0].PageSheet.PrintProps.PrintPageOrientation.Value == Aspose.Diagram.PrintPageOrientationValue.Landscape; vsdDiagram.Save(documentStream, Aspose.Diagram.SaveFileFormat.PDF); break; //Microsoft Project case ".MPP": var project = new Aspose.Tasks.Project(fileStream); project.Save(documentStream, Aspose.Tasks.Saving.SaveFileFormat.PDF); isLandscape = true; break; //PowerPoint case ".PPT": case ".PPS": case ".POT": using (var pres = new Aspose.Slides.Presentation(fileStream)) { isLandscape = pres.SlideSize.Size.Width > pres.SlideSize.Size.Height; pres.Save(documentStream, Aspose.Slides.Export.SaveFormat.Pdf); } break; case ".PPTX": case ".PPSX": case ".POTX": //case ".XPS": using (var presEx = new Aspose.Slides.Presentation(fileStream)) { isLandscape = presEx.SlideSize.Orientation == Aspose.Slides.SlideOrienation.Landscape; presEx.Save(documentStream, Aspose.Slides.Export.SaveFormat.Pdf); } break; case ".PDF": { using (var pdf = new Aspose.Pdf.Document(fileStream)) { var page = pdf.Pages.OfType<Aspose.Pdf.Page>().FirstOrDefault(); if (page != null && page.MediaBox != null) { isLandscape = page.MediaBox.Width > page.MediaBox.Height; } } fileStream.Seek(0, SeekOrigin.Begin); fileStream.CopyTo(documentStream); } break; } } finally { fileStream.Close(); } return true; }
public void createThumbnails() { //ExStart //ExFor:Document.RenderToScale //ExSummary:Renders individual pages to graphics to create one image with thumbnails of all pages. // The user opens or builds a document. Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Rendering.doc"); // This defines the number of columns to display the thumbnails in. const int thumbColumns = 2; // Calculate the required number of rows for thumbnails. // We can now get the number of pages in the document. int remainder; int thumbRows = Math.DivRem(doc.PageCount, thumbColumns, out remainder); if (remainder > 0) thumbRows++; // Lets say I want thumbnails to be of this zoom. const float scale = 0.25f; // For simplicity lets pretend all pages in the document are of the same size, // so we can use the size of the first page to calculate the size of the thumbnail. Size thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96); // Calculate the size of the image that will contain all the thumbnails. int imgWidth = thumbSize.Width * thumbColumns; int imgHeight = thumbSize.Height * thumbRows; using (Bitmap img = new Bitmap(imgWidth, imgHeight)) { // The user has to provides a Graphics object to draw on. // The Graphics object can be created from a bitmap, from a metafile, printer or window. using (Graphics gr = Graphics.FromImage(img)) { gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Fill the "paper" with white, otherwise it will be transparent. gr.FillRectangle(new SolidBrush(Color.White), 0, 0, imgWidth, imgHeight); for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++) { int columnIdx; int rowIdx = Math.DivRem(pageIndex, thumbColumns, out columnIdx); // Specify where we want the thumbnail to appear. float thumbLeft = columnIdx * thumbSize.Width; float thumbTop = rowIdx * thumbSize.Height; SizeF size = doc.RenderToScale(pageIndex, gr, thumbLeft, thumbTop, scale); // Draw the page rectangle. gr.DrawRectangle(Pens.Black, thumbLeft, thumbTop, size.Width, size.Height); } img.Save(ExDir + "Rendering.Thumbnails Out.png"); } } //ExEnd }
public void RenderToScale() { //ExStart //ExFor:Document.RenderToScale //ExFor:Document.GetPageInfo //ExFor:PageInfo //ExFor:PageInfo.GetSizeInPixels //ExSummary:Renders a page of a Word document into a bitmap using a specified zoom factor. Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "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(ExDir + "Rendering.RenderToScale Out.png"); } //ExEnd }
public void WritePageInfo() { //ExStart //ExFor:PageInfo //ExFor:PageInfo.PaperSize //ExFor:PageInfo.PaperTray //ExFor:PageInfo.Landscape //ExFor:PageInfo.WidthInPoints //ExFor:PageInfo.HeightInPoints //ExSummary:Retrieves page size and orientation information for every page in a Word document. Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Rendering.doc"); Console.WriteLine("Document \"{0}\" contains {1} pages.", doc.OriginalFileName, doc.PageCount); for (int i = 0; i < doc.PageCount; i++) { PageInfo pageInfo = doc.GetPageInfo(i); Console.WriteLine( "Page {0}. PaperSize:{1} ({2:F0}x{3:F0}pt), Orientation:{4}, PaperTray:{5}", i + 1, pageInfo.PaperSize, pageInfo.WidthInPoints, pageInfo.HeightInPoints, pageInfo.Landscape ? "Landscape" : "Portrait", pageInfo.PaperTray); } //ExEnd }
public void createThumbnails() { //ExStart //ExFor:Document.RenderToScale //ExSummary:Renders individual pages to graphics to create one image with thumbnails of all pages. // The user opens or builds a document. Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Rendering.doc"); // This defines the number of columns to display the thumbnails in. const int thumbColumns = 2; // Calculate the required number of rows for thumbnails. // We can now get the number of pages in the document. int remainder; int thumbRows = Math.DivRem(doc.PageCount, thumbColumns, out remainder); if (remainder > 0) { thumbRows++; } // Lets say I want thumbnails to be of this zoom. const float scale = 0.25f; // For simplicity lets pretend all pages in the document are of the same size, // so we can use the size of the first page to calculate the size of the thumbnail. Size thumbSize = doc.GetPageInfo(0).GetSizeInPixels(scale, 96); // Calculate the size of the image that will contain all the thumbnails. int imgWidth = thumbSize.Width * thumbColumns; int imgHeight = thumbSize.Height * thumbRows; using (Bitmap img = new Bitmap(imgWidth, imgHeight)) { // The user has to provides a Graphics object to draw on. // The Graphics object can be created from a bitmap, from a metafile, printer or window. using (Graphics gr = Graphics.FromImage(img)) { gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Fill the "paper" with white, otherwise it will be transparent. gr.FillRectangle(new SolidBrush(Color.White), 0, 0, imgWidth, imgHeight); for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++) { int columnIdx; int rowIdx = Math.DivRem(pageIndex, thumbColumns, out columnIdx); // Specify where we want the thumbnail to appear. float thumbLeft = columnIdx * thumbSize.Width; float thumbTop = rowIdx * thumbSize.Height; SizeF size = doc.RenderToScale(pageIndex, gr, thumbLeft, thumbTop, scale); // Draw the page rectangle. gr.DrawRectangle(Pens.Black, thumbLeft, thumbTop, size.Width, size.Height); } img.Save(MyDir + "Rendering.Thumbnails Out.png"); } } //ExEnd }