Example #1
0
 public ReportPrinter()
 {
     HeaderLine   = true;
     FooterLine   = true;
     MarginTop    = "1.5cm";
     MarginBottom = "1cm";
     Orientation  = PdfOrientation.Portrait;
 }
Example #2
0
        private void BtnConvert_Click(object sender, RoutedEventArgs e)
        {
            PdfOrientation orientation = rbA4Portrait.IsChecked == true ? PdfOrientation.A4_Portrait : PdfOrientation.A4_Landscape;

            _converter.ImagesToPdf(_sourcePaths, txbTarget.Text, orientation);

            MessageBox.Show("Arquivo PDF gerado com sucesso!");
        }
Example #3
0
        public static PdfOutput FromHtml(FileInfo inputFile, string wkhtmlOptions, PdfOrientation orientation = PdfOrientation.Landscape)
        {
            string   fileName = Path.GetFileNameWithoutExtension(inputFile.Name);
            FileInfo output   = new FileInfo("{0}.pdf"._Format(fileName));

            ProcessOutput processOutput = "wkhtml.exe -O {0} {1} {2}"._Format(orientation.ToString(), wkhtmlOptions, output.Name).Run();

            return(new PdfOutput(output));
        }
Example #4
0
        public static string ToRQLString(this PdfOrientation value)
        {
            switch (value)
            {
            case PdfOrientation.Default:
                return("default");

            case PdfOrientation.Portrait:
                return("portrait");

            case PdfOrientation.Landscape:
                return("landscape");

            default:
                throw new ArgumentException(string.Format("Unknown {0} value: {1}",
                                                          typeof(PdfOrientationUtils).Name, value));
            }
        }
Example #5
0
        /// <summary> Converts a set of images to a pdf file
        /// </summary>
        /// <param name="sourceFilePaths">Image file paths</param>
        /// <param name="targetFilePath">Pdf file path</param>
        /// <param name="orientation">Pdf orientation</param>
        public void ImagesToPdf(string[] sourceFilePaths, string targetFilePath, PdfOrientation orientation)
        {
            Rectangle pageSize = orientation == PdfOrientation.A4_Portrait ? PageSize.A4
                : orientation == PdfOrientation.A4_Landscape ? PageSize.A4.Rotate()
                : throw new ArgumentException($"Unknown orientation {orientation}", nameof(orientation));

            var document = new Document(pageSize, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE);

            using (var stream = new FileStream(targetFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                PdfWriter.GetInstance(document, stream);
                document.Open();

                for (int i = 0; i < sourceFilePaths.Length; ++i)
                {
                    string imagePath = sourceFilePaths[i];

                    using (var imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var image = Image.GetInstance(imageStream);

                        if (image.Height > pageSize.Height - BORDER_SIZE)
                        {
                            image.ScaleToFit(pageSize.Width - BORDER_SIZE, pageSize.Height - BORDER_SIZE);
                        }
                        else if (image.Width > pageSize.Width - BORDER_SIZE)
                        {
                            image.ScaleToFit(pageSize.Width - BORDER_SIZE, pageSize.Height - BORDER_SIZE);
                        }

                        image.Alignment = Image.ALIGN_MIDDLE;

                        if (i > 0)
                        {
                            document.NewPage();
                        }

                        document.Add(image);
                    }
                }

                document.Close();
            }
        }
Example #6
0
        public void Commit()
        {
            const string SAVE_DATA =
                @"<TEMPLATE action=""save"" guid=""{0}""><TEMPLATEVARIANT guid=""{1}"" name=""{2}"" fileextension=""{3}"" insertstylesheetinpage=""{4}"" nostartendmarkers=""{5}"" containerpagereference=""{6}"" pdforientation=""{7}""/></TEMPLATE>";

            Project.ExecuteRQL(
                SAVE_DATA.SecureRQLFormat(
                    ContentClass,
                    this,
                    Name,
                    FileExtension ?? RQL.SESSIONKEY_PLACEHOLDER,
                    IsStylesheetIncludedInHeader,
                    _noStartEndMarkers,
                    HasContainerPageReference,
                    PdfOrientation.ToRQLString()));


            Refresh();
        }
Example #7
0
        public async Task Can_Create_Complex_Pdf(PdfOrientation orientation, PdfSize size, int lowerExpectedSize, int higherExpectedSize)
        {
            string html;

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Adliance.AspNetCore.Buddy.Pdf.Test.complex_html.html"))
            {
                Assert.NotNull(stream);
                using (var reader = new StreamReader(stream ?? throw new Exception("Stream should not be null at this point.")))
                {
                    html = reader.ReadToEnd();
                }
            }

            string footer;

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Adliance.AspNetCore.Buddy.Pdf.Test.complex_footer.html"))
            {
                Assert.NotNull(stream);
                using (var reader = new StreamReader(stream ?? throw new Exception("Stream should not be null at this point.")))
                {
                    footer = await reader.ReadToEndAsync();
                }
            }

            var bytes = await _pdfer.HtmlToPdf(html, new PdfOptions
            {
                MarginTop    = 15,
                MarginBottom = 33,
                MarginLeft   = 0,
                MarginRight  = 0,
                FooterHtml   = footer,
                Orientation  = orientation,
                Size         = size
            });

            //File.WriteAllBytes($@"C:\Users\Hannes\Downloads\complex_{size}_{orientation}.pdf", bytes);
            Assert.True(bytes.Length > lowerExpectedSize && bytes.Length < higherExpectedSize, $"Bytes were not in expected range ({bytes.Length})"); // seems like the resulting PDF is about 4MB for this
        }
Example #8
0
 public static PdfOutput FromHtml(string filePath, PdfOrientation orientation = PdfOrientation.Landscape)
 {
     return(FromHtml(new FileInfo(filePath), "-s Letter ", orientation));
 }