public void Adjust(MyPageSize myPageSize) { var pageMediaSize = pageMediaSizes // PageMediaSize の選定方法は必要に応じて修正してください。 .FirstOrDefault(oneSize => (oneSize.PageMediaSizeName + "").Contains(myPageSize.name)); if (pageMediaSize != null) { ticket.PageMediaSize = pageMediaSize; ticket.PageOrientation = myPageSize.tate ? PageOrientation.Portrait : PageOrientation.Landscape; } }
/// <summary> /// store a pdf to a file. will call PrintPage automatically /// </summary> public void SavePDF(string AFilename, PaperKind APaperKind, Margins AMargins, float AWidthInPoint, float AHeightInPoint) { if (Directory.Exists("/usr/share/fonts/")) { PdfSharp.Internal.NativeMethods.FontDirectory = "/usr/share/fonts/"; } if (RegionInfo.CurrentRegion == null) { // https://bugzilla.novell.com/show_bug.cgi?id=588708 // RegionInfo.CurrentRegion is null throw new Exception("Mono bug: CurrentRegion is still null, invariant culture. Please set LANG environment variable"); } FPdfDocument = new PdfDocument(); do { PdfPage page = FPdfDocument.AddPage(); if (APaperKind != PaperKind.Custom) { // see if we can match PaperKind to PageSize by name foreach (PageSize MyPageSize in Enum.GetValues(typeof(PageSize))) { if (MyPageSize.ToString() == APaperKind.ToString()) { page.Size = MyPageSize; break; } } } if ((AWidthInPoint != -1) && (AHeightInPoint != -1)) { // to get the points, eg. inch * 72 page.Width = AWidthInPoint; page.Height = AHeightInPoint; } FXGraphics = XGraphics.FromPdfPage(page, XGraphicsUnit.Inch); FXGraphics.MFEH = PdfFontEmbedding.Always; // it seems for Linux we better reset the FEv, otherwise the positions are only correct on the first page, but wrong on the following pages // was: if (FEv == null) if (true) { PrinterSettings myPrinterSettings = new PrinterSettings(); PageSettings myPageSettings = new PageSettings(myPrinterSettings); myPageSettings.Color = true; myPageSettings.Landscape = false; myPageSettings.Margins = AMargins; myPageSettings.PaperSize = new PaperSize(page.Size.ToString(), Convert.ToInt32(page.Width.Point / 72.0f * 100.0f), Convert.ToInt32(page.Height.Point / 72.0f * 100.0f)); try { myPageSettings.PrinterResolution.X = DEFAULTPRINTERRESOLUTION; myPageSettings.PrinterResolution.Y = DEFAULTPRINTERRESOLUTION; } catch (Exception) { // if no printer is installed we get an exception, but it should work anyway } FEv = new PrintPageEventArgs(FXGraphics.Graphics, new Rectangle(20, 20, 787, 1110), new Rectangle(0, 0, 827, 1169), myPageSettings); FEv.HasMorePages = true; FEv.Graphics.PageUnit = GraphicsUnit.Inch; FEv.Graphics.TranslateTransform(0, 0); InitFontsAndPens(); } PrintPage(null, FEv); } while (HasMorePages()); // should we catch an exception if document cannot be written? FPdfDocument.Save(AFilename); FPdfDocument.Close(); }
/// <summary> /// store a pdf to a file. will call PrintPage automatically /// </summary> public void SavePDF(string AFilename, PaperKind APaperKind, Margins AMargins, float AWidthInPoint, float AHeightInPoint) { if (RegionInfo.CurrentRegion == null) { // https://bugzilla.novell.com/show_bug.cgi?id=588708 // RegionInfo.CurrentRegion is null throw new Exception("Mono bug: CurrentRegion is still null, invariant culture. Please set LANG environment variable"); } FPdfDocument = new PdfDocument(); bool firstPage = true; do { PdfPage page = FPdfDocument.AddPage(); if (APaperKind != PaperKind.Custom) { // see if we can match PaperKind to PageSize by name foreach (PageSize MyPageSize in Enum.GetValues(typeof(PageSize))) { if (MyPageSize.ToString() == APaperKind.ToString()) { page.Size = MyPageSize; break; } } } if ((AWidthInPoint != -1.0) && (AHeightInPoint != -1.0)) { // to get the points, eg. inch * 72 page.Width = AWidthInPoint; page.Height = AHeightInPoint; } FXGraphics = XGraphics.FromPdfPage(page, XGraphicsUnit.Inch); // it seems for Linux we better reset the FEv, otherwise the positions are only correct on the first page, but wrong on the following pages // was: if (FEv == null) if (true) { PrinterSettings myPrinterSettings = new PrinterSettings(); PageSettings myPageSettings = new PageSettings(myPrinterSettings); myPageSettings.Color = true; myPageSettings.Landscape = false; myPageSettings.Margins = AMargins; myPageSettings.PaperSize = new PaperSize(page.Size.ToString(), Convert.ToInt32(page.Width.Point / 72.0f * 100.0f), Convert.ToInt32(page.Height.Point / 72.0f * 100.0f)); InitFontsAndPens(); } if (firstPage) { PrintPage(); firstPage = false; } else { PrintPage(); } } while (HasMorePages()); // should we catch an exception if document cannot be written? FPdfDocument.Save(AFilename); FPdfDocument.Close(); // Console.WriteLine("PDF has been written to " + Path.GetFullPath(AFilename)); }