PageInfoLandscape Property |
Namespace: Aspose.Words.Rendering
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"); // Paper 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}"); }
Document doc = new Document(MyDir + "Rendering.docx"); // Create an instance of our own PrintDocument MyPrintDocument printDoc = new MyPrintDocument(doc); // Specify the page range to print printDoc.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages; printDoc.PrinterSettings.FromPage = 1; printDoc.PrinterSettings.ToPage = 1; // Print our document. printDoc.Print(); } /// <summary> /// The way to print in the .NET Framework is to implement a class derived from PrintDocument. /// This class is an example on how to implement custom printing of an Aspose.Words document. /// It selects an appropriate paper size, orientation and paper tray when printing. /// </summary> public class MyPrintDocument : PrintDocument { public MyPrintDocument(Document document) { mDocument = document; } /// <summary> /// Called before the printing starts. /// </summary> protected override void OnBeginPrint(PrintEventArgs e) { base.OnBeginPrint(e); // Initialize the range of pages to be printed according to the user selection switch (PrinterSettings.PrintRange) { case System.Drawing.Printing.PrintRange.AllPages: mCurrentPage = 1; mPageTo = mDocument.PageCount; break; case System.Drawing.Printing.PrintRange.SomePages: mCurrentPage = PrinterSettings.FromPage; mPageTo = PrinterSettings.ToPage; break; default: throw new InvalidOperationException("Unsupported print range."); } } /// <summary> /// Called before each page is printed. /// </summary> protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e) { base.OnQueryPageSettings(e); // A single Word document can have multiple sections that specify pages with different sizes, // orientation and paper trays. This code is called by the .NET printing framework before // each page is printed and we get a chance to specify how the page is to be printed PageInfo pageInfo = mDocument.GetPageInfo(mCurrentPage - 1); e.PageSettings.PaperSize = pageInfo.GetDotNetPaperSize(PrinterSettings.PaperSizes); // MS Word stores the paper source (printer tray) for each section as a printer-specfic value // To obtain the correct tray value you will need to use the RawKindValue returned // by .NET for your printer e.PageSettings.PaperSource.RawKind = pageInfo.PaperTray; e.PageSettings.Landscape = pageInfo.Landscape; } /// <summary> /// Called for each page to render it for printing. /// </summary> protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); // Aspose.Words rendering engine creates a page that is drawn from the 0,0 of the paper, // but there is some hard margin in the printer and the .NET printing framework // renders from there. We need to offset by that hard margin // In .NET 1.1 the hard margin is not available programmatically, lets hardcode to about 4mm float hardOffsetX = 20; float hardOffsetY = 20; // This is in .NET 2.0 only. Uncomment when needed // float hardOffsetX = e.PageSettings.HardMarginX; // float hardOffsetY = e.PageSettings.HardMarginY; int pageIndex = mCurrentPage - 1; mDocument.RenderToScale(mCurrentPage, e.Graphics, -hardOffsetX, -hardOffsetY, 1.0f); mCurrentPage++; e.HasMorePages = (mCurrentPage <= mPageTo); } private readonly Document mDocument; private int mCurrentPage; private int mPageTo; }