public static bool TryToThickness(dynamic value, out PrintElementThickness result) { if (value != null) { double all; if (TryToSizeInPixels(value.All, value.SizeUnit, out all)) { result = new PrintElementThickness(all); } else { double left, top, right, bottom; TryToSizeInPixels(value.Left, value.SizeUnit, out left); TryToSizeInPixels(value.Top, value.SizeUnit, out top); TryToSizeInPixels(value.Right, value.SizeUnit, out right); TryToSizeInPixels(value.Bottom, value.SizeUnit, out bottom); result = new PrintElementThickness(left, top, right, bottom); } return(true); } result = default(PrintElementThickness); return(false); }
private static dynamic CreateSectionWithBorder(PrintElementThickness margin, PrintElementThickness padding, PrintElementThickness border, string borderBrush, string background, params dynamic[] blocks) { dynamic sectionItem = CreateSection(blocks); sectionItem.Section.Margin = new DynamicWrapper(); sectionItem.Section.Margin.Left = margin.Left; sectionItem.Section.Margin.Top = margin.Top; sectionItem.Section.Margin.Right = margin.Right; sectionItem.Section.Margin.Bottom = margin.Bottom; sectionItem.Section.Margin.SizeUnit = "Px"; sectionItem.Section.Padding = new DynamicWrapper(); sectionItem.Section.Padding.Left = padding.Left; sectionItem.Section.Padding.Top = padding.Top; sectionItem.Section.Padding.Right = padding.Right; sectionItem.Section.Padding.Bottom = padding.Bottom; sectionItem.Section.Padding.SizeUnit = "Px"; sectionItem.Section.Border = new DynamicWrapper(); sectionItem.Section.Border.Thickness = new DynamicWrapper(); sectionItem.Section.Border.Thickness.Left = border.Left; sectionItem.Section.Border.Thickness.Top = border.Top; sectionItem.Section.Border.Thickness.Right = border.Right; sectionItem.Section.Border.Thickness.Bottom = border.Bottom; sectionItem.Section.Border.Thickness.SizeUnit = "Px"; sectionItem.Section.Border.Color = borderBrush; sectionItem.Section.Background = background; return(sectionItem); }
private static void ApplyPagePadding(PrintViewDocument element, dynamic pagePadding) { // По умолчанию отступ на странице 1см const double pagePadding1Cm = 1.0 * SizeUnits.Cm; PrintElementThickness pagePaddingThickness; if (!BuildHelper.TryToThickness(pagePadding, out pagePaddingThickness)) { pagePaddingThickness = new PrintElementThickness(pagePadding1Cm); } element.PagePadding = pagePaddingThickness; }
private string BuildHtmlToPdfUtilArguments(PrintElementSize size, PrintElementThickness padding, string fileHtmlPath, string filePdfPath) { var mmPaddingBottom = (int)(padding.Bottom / SizeUnits.Mm); var mmPaddingLeft = (int)(padding.Left / SizeUnits.Mm); var mmPaddingRight = (int)(padding.Right / SizeUnits.Mm); var mmPaddingTop = (int)(padding.Right / SizeUnits.Mm); var htmlToPdfUtil = _htmlToPdfUtilArguments .Replace(HtmlInput, fileHtmlPath) .Replace(PdfOutput, filePdfPath) .Replace(PaddingBottom, mmPaddingBottom.ToString()) .Replace(PaddingLeft, mmPaddingLeft.ToString()) .Replace(PaddingRight, mmPaddingRight.ToString()) .Replace(PaddingTop, mmPaddingTop.ToString()) ; if (size != null) { if (size.Height != null) { var mmPageHeight = (int)(size.Height / SizeUnits.Mm); htmlToPdfUtil = htmlToPdfUtil.Replace(PageHeight, mmPageHeight.ToString()); } if (size.Width != null) { var mmPageWidth = (int)(size.Width / SizeUnits.Mm); htmlToPdfUtil = htmlToPdfUtil.Replace(PageWidth, mmPageWidth.ToString()); } } else { const int defaultPageHeight = 297; const int defaultPageWidth = 210; htmlToPdfUtil = htmlToPdfUtil.Replace(PageHeight, defaultPageHeight.ToString()); htmlToPdfUtil = htmlToPdfUtil.Replace(PageWidth, defaultPageWidth.ToString()); } return(htmlToPdfUtil); }
public void Convert(PrintElementSize size, PrintElementThickness padding, Stream inHtmlStream, Stream outPdfStream) { var fileName = Guid.NewGuid().ToString("N"); var fileHtmlPath = Path.Combine(_htmlToPdfTemp, fileName + ".html"); var filePdfPath = Path.Combine(_htmlToPdfTemp, fileName + ".pdf"); try { using (var htmlFileStream = File.Create(fileHtmlPath)) { inHtmlStream.CopyTo(htmlFileStream); htmlFileStream.Flush(); } var htmlToPdfUtilArguments = BuildHtmlToPdfUtilArguments(size, padding, fileHtmlPath, filePdfPath); var htmlToPdfConvertResult = ExecuteShellCommand(_htmlToPdfUtilCommand, htmlToPdfUtilArguments, UtilTimeout); if (htmlToPdfConvertResult.Completed && htmlToPdfConvertResult.ExitCode == 0) { using (var fileStream = File.OpenRead(filePdfPath)) { fileStream.CopyTo(outPdfStream); outPdfStream.Flush(); } } else { if (htmlToPdfConvertResult.Completed) { throw new InvalidOperationException(string.Format(Resources.CannotConvertHtmlToPdf, htmlToPdfConvertResult.Output, htmlToPdfConvertResult.ExitCode)); } throw new InvalidOperationException(Resources.CannotConvertHtmlToPdfByTimeout); } } finally { DeleteFile(fileHtmlPath); DeleteFile(filePdfPath); } }