Example #1
0
        public static byte[] SpirePDFHtmlToPDF(string html)
        {
            byte[] ret = null;

            //using (MemoryStream ms = new MemoryStream())
            //{
            //    Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(
            //    html,
            //    //memory stream
            //    ms,
            //    //enable javascript
            //    true,
            //    //load timeout
            //    10 * 1000,
            //    //page size
            //    new SizeF(612, 792),
            //    //page margins
            //    new Spire.Pdf.Graphics.PdfMargins(0),
            //    //load from content type
            //    LoadHtmlType.SourceCode
            //    );
            //}

            using (MemoryStream ms = new MemoryStream())
            {
                //Create a pdf document.
                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();

                PdfPageSettings setting = new PdfPageSettings();

                //setting.Size = new SizeF(1000, 1000);
                setting.Size        = Spire.Pdf.PdfPageSize.A4;
                setting.Orientation = Spire.Pdf.PdfPageOrientation.Portrait;
                setting.Margins     = new Spire.Pdf.Graphics.PdfMargins(20);

                doc.Template.Top = GetHeader(doc, setting.Margins);
                //apply blank templates to other parts of page template
                doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, setting.Margins.Bottom);
                doc.Template.Left   = new PdfPageTemplateElement(setting.Margins.Left, doc.PageSettings.Size.Height);
                doc.Template.Right  = new PdfPageTemplateElement(setting.Margins.Right, doc.PageSettings.Size.Height);

                PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
                htmlLayoutFormat.IsWaiting = true;

                Thread thread = new Thread(() =>
                                           { doc.LoadFromHTML(html, true, setting, htmlLayoutFormat); });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();

                //Save pdf file.
                doc.SaveToStream(ms);
                doc.Close();

                ret = ms.ToArray();
            }

            return(ret);
        }
Example #2
0
        public MemoryStream SpireFields(Dictionary <string, string> formFields)
        {
            if (string.IsNullOrEmpty(this._location.Trim()))
            {
                return(null);
            }
            byte[] fileBytes;
            var    doc = new Spire.Pdf.PdfDocument();

            doc.LoadFromFile(this._location);
            var form = doc.Form as PdfFormWidget;

            doc.Pages[0].BackgroudOpacity = 1.0f;
            form.AutoNaming      = false;
            form.NeedAppearances = true;



            foreach (var field in form.FieldsWidget.List)
            {
                var formField = field as PdfField;

                if (field is PdfTextBoxFieldWidget)
                {
                    var textField = field as PdfTextBoxFieldWidget;
                    if (formFields.ContainsKey(textField.Name))
                    {
                        textField.Text = formFields[textField.Name];
                    }
                }

                else if (field is PdfCheckBoxWidgetFieldWidget)
                {
                    var checkBoxField = field as PdfCheckBoxWidgetFieldWidget;
                    if (formFields.ContainsKey(checkBoxField.Name))
                    {
                        checkBoxField.Checked = formFields[checkBoxField.Name].ToUpper() == "TRUE" ? true : false;
                    }
                }
                formField.Flatten = true;
            }

            using (var str = new MemoryStream())
            {
                doc.SaveToStream(str);
                doc.Close();

                return(str);
            }
        }
Example #3
0
        public void PDFExport(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName   = "Document";
            dlg.DefaultExt = ".pdf";
            dlg.Filter     = "PDF files (*.pdf)|*.pdf";
            Nullable <bool> result = dlg.ShowDialog();

            if (result == true)
            {
                string data     = "";
                int    lmsCount = 0;
                double tmpMax   = 0;
                double tmpMin   = 0;
                double tmpAvg   = 0;
                double humMax   = 0;
                double humMin   = 0;
                double humAvg   = 0;
                int    ct       = 0;
                bool   first    = true;

                Spire.Pdf.PdfDocument doc  = new Spire.Pdf.PdfDocument();
                PdfPageBase           page = doc.Pages.Add();
                page.Canvas.DrawString("DATA REPORT",
                                       new Spire.Pdf.Graphics.PdfFont(PdfFontFamily.Helvetica, 20f),
                                       new PdfSolidBrush(new PdfRGBColor()),
                                       300, 10);
                if (debut.SelectedDate.HasValue && fin.SelectedDate.HasValue && run)
                {
                    List <MStatement> lms = man.GetAllStatementByDate(mainWindow.captor, debut.SelectedDate.Value, fin.SelectedDate.Value.AddDays(1));
                    foreach (MStatement s in lms)
                    {
                        data += string.Format("{0}.\t{1}\t{2}\t{3}\r\n", s.StatementId, s.DateTime, s.Temperature, s.Humidity);
                        if (first)
                        {
                            tmpMin = s.Temperature;
                            tmpMax = s.Temperature;
                            tmpAvg = s.Temperature;
                            humMin = s.Humidity;
                            humMax = s.Humidity;
                            humAvg = s.Humidity;
                            first  = false;
                        }
                        else
                        {
                            if (tmpMin > s.Temperature)
                            {
                                tmpMin = s.Temperature;
                            }
                            if (tmpMax < s.Temperature)
                            {
                                tmpMax = s.Temperature;
                            }
                            tmpAvg += s.Temperature;
                            if (humMin > s.Humidity)
                            {
                                humMin = s.Humidity;
                            }
                            if (humMax < s.Humidity)
                            {
                                humMax = s.Humidity;
                            }
                            humAvg += s.Humidity;
                        }
                        ct++;
                    }
                    tmpAvg   = tmpAvg / ct;
                    humAvg   = humAvg / ct;
                    lmsCount = lms.Count;
                }
                else
                {
                    List <MStatement> lms = man.GetAllStatement(mainWindow.captor);
                    foreach (MStatement s in lms)
                    {
                        data += string.Format("{0}.\t{1}\t{2}\t{3}\r\n", s.StatementId, s.DateTime, s.Temperature, s.Humidity);
                        if (first)
                        {
                            tmpMin = s.Temperature;
                            tmpMax = s.Temperature;
                            tmpAvg = s.Temperature;
                            humMin = s.Humidity;
                            humMax = s.Humidity;
                            humAvg = s.Humidity;
                            first  = false;
                        }
                        else
                        {
                            if (tmpMin > s.Temperature)
                            {
                                tmpMin = s.Temperature;
                            }
                            if (tmpMax < s.Temperature)
                            {
                                tmpMax = s.Temperature;
                            }
                            tmpAvg += s.Temperature;
                            if (humMin > s.Humidity)
                            {
                                humMin = s.Humidity;
                            }
                            if (humMax < s.Humidity)
                            {
                                humMax = s.Humidity;
                            }
                            humAvg += s.Humidity;
                        }
                        ct++;
                    }
                    tmpAvg   = Math.Round(tmpAvg / ct, 1);
                    humAvg   = Math.Round(humAvg / ct, 1);
                    lmsCount = lms.Count;
                }
                string info = string.Format("SN: {0}\nTotal Records: {1}\nTemp Max/Min/Avg: {2}/{3}/{4}\nHumidity Max/Min/Avg: {5}/{6}/{7}", mainWindow.captor.Serial_number, lmsCount, tmpMax, tmpMin, tmpAvg, humMax, humMin, humAvg);
                page.Canvas.DrawImage();
                page.Canvas.DrawString(info,
                                       new Spire.Pdf.Graphics.PdfFont(PdfFontFamily.Helvetica, 12f),
                                       new PdfSolidBrush(new PdfRGBColor()),
                                       10, 60);

                page.Canvas.DrawString("Temperature and humidity data",
                                       new Spire.Pdf.Graphics.PdfFont(PdfFontFamily.Helvetica, 18f),
                                       new PdfSolidBrush(new PdfRGBColor()),
                                       10, 140);

                page.Canvas.DrawString(data,
                                       new Spire.Pdf.Graphics.PdfFont(PdfFontFamily.Helvetica, 12f),
                                       new PdfSolidBrush(new PdfRGBColor()),
                                       10, 160);
                FileStream to_stream = new FileStream(dlg.FileName, FileMode.Create);
                doc.SaveToStream(to_stream);
                to_stream.Close();
                doc.Close();
            }
        }
Example #4
0
        public static byte[] ConvertToPdf(string fileName)
        {
            string       contentType = MimeMapping.GetMimeMapping(fileName);
            MemoryStream mem         = new MemoryStream();

            switch (contentType.ToLower())
            {
            case "image/gif":
            case "image/jpg":
            case "image/png":
            case "image/jpeg":
            case "application/BMP":
            case "image/bmp":
            case "image/tiff":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Document document = new Document(PageSize.LETTER, 10, 10, 10, 10);
                    //using ( var stream = new MemoryStream() )
                    //{
                    PdfWriter.GetInstance(document, mem);

                    document.Open();
                    using (var imageStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var image = Image.GetInstance(imageStream);
                        image.ScaleAbsolute(585, 750);
                        //image.ScaleAbsoluteHeight(PageSize.A4.Height - 20);
                        //image.ScaleToFit(PageSize.LETTER);

                        document.Add(image);
                    }
                    document.Close();

                    // return stream.ToArray();
                    //}
                }

                break;

            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
            case "application/vnd.ms-excel":
            case "application/xlsx":
            case "application/xls":
            case "application/vnd.ms-excel.sheet.macroEnabled.12":
            case "application/XLSM":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
                    workbook.LoadFromFile(fileName, true);
                    workbook.SaveToStream(mem, Spire.Xls.FileFormat.PDF);
                }

                break;

            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            case "application/msword":
            case "application/docx":
            case "application/doc":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Doc.Document document = new Spire.Doc.Document();
                    document.LoadFromFile(fileName, Spire.Doc.FileFormat.Auto);
                    document.SaveToStream(mem, Spire.Doc.FileFormat.PDF);
                    document.Close();
                }

                break;

            case "application/html":
            case "application/txt":
            case "application/htm":
            case "text/html":
            case "application/xml":
            case "text/plain":
                if (!string.IsNullOrEmpty(fileName))
                {
                    Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
                    PdfHtmlLayoutFormat   htmlLayoutFormat = new PdfHtmlLayoutFormat
                    {
                        Layout          = PdfLayoutType.Paginate,
                        FitToPage       = Clip.Width,
                        LoadHtmlTimeout = 60 * 1000
                    };
                    htmlLayoutFormat.IsWaiting = true;
                    PdfPageSettings setting = new PdfPageSettings();
                    setting.Size = PdfPageSize.A4;
                    Thread thread = new Thread(() => { pdf.LoadFromHTML(fileName, true, true, true); });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    pdf.SaveToStream(mem, Spire.Pdf.FileFormat.PDF);
                }
                break;

            case "application/msg":
            case "application/octet-stream":
            case "multipart/related":
            case "application/ZIP":
            case "application/VCF":
            default:
                break;
            }

            return(mem.ToArray());
        }