Esempio n. 1
0
        public byte[] CreateWordDocument2()
        {
            //创建一个Document实例并添加section
            Spire.Doc.Document doc     = new Spire.Doc.Document();
            Spire.Doc.Section  section = doc.AddSection();

            //添加指向网址的超链接
            Spire.Doc.Documents.Paragraph para1 = section.AddParagraph();
            para1.AppendHyperlink("www.baidu.com", "www.baidu.com",
                                  Spire.Doc.Documents.HyperlinkType.WebLink);

            //添加指向邮件地址的超链接
            Spire.Doc.Documents.Paragraph para2 = section.AddParagraph();
            para2.AppendHyperlink("mailto:[email protected]", "mailto:[email protected]",
                                  Spire.Doc.Documents.HyperlinkType.EMailLink);


            //设置段落之间的间距
            para1.Format.AfterSpacing = 15f;
            para2.Format.AfterSpacing = 15f;

            MemoryStream memoryStream = new MemoryStream();

            StreamWriter wr = new StreamWriter(memoryStream);


            //保存文档
            doc.SaveToStream(memoryStream, Spire.Doc.FileFormat.Docx2013);

            return(memoryStream.ToArray());
        }
        private static void method3(string filename, string printer)
        {
            MemoryStream ms = new MemoryStream();

            Spire.Doc.Document doc = new Spire.Doc.Document();
            doc.LoadFromFile(filename);
            doc.SaveToStream(ms, Spire.Doc.FileFormat.XPS);
            ms.Position = 0;
            XpsPrintHelper.Print(ms, printer, $"{Path.GetFileName(filename)}", true);
        }
        private byte[] ConvertToPdf(string input)
        {
            var input_bytes   = System.Convert.FromBase64String(input);
            var input_stream  = new System.IO.MemoryStream(input_bytes);
            var output_stream = new System.IO.MemoryStream();

            var pdfdoc = new Spire.Doc.Document(input_stream, Spire.Doc.FileFormat.Auto);

            pdfdoc.SaveToStream(output_stream, Spire.Doc.FileFormat.PDF);

            return(output_stream.ToArray());
        }
 internal static byte[] DocumentToPdf(byte[] docBytes)
 {
     byte[] result = null;
     using (MemoryStream ms = new MemoryStream(docBytes))
     {
         Spire.Doc.Document doc = new Spire.Doc.Document(ms);
         using (MemoryStream target = new MemoryStream())
         {
             doc.SaveToStream(target, Spire.Doc.FileFormat.PDF);
             result = target.ToArray();
         }
         return(result);
     }
 }
Esempio n. 5
0
        public static string ToHTML(string fBase64, string nomeoriginal)
        {
            using (var doc = new Spire.Doc.Document())
            {
                using (Stream stream = new MemoryStream(Convert.FromBase64String(fBase64)))
                {
                    doc.LoadFromStream(stream, Path.GetExtension(nomeoriginal).ToUpper().Contains("DOCX") ? Spire.Doc.FileFormat.Docx : Spire.Doc.FileFormat.Doc);
                    doc.HtmlExportOptions.ImageEmbedded              = true;
                    doc.HtmlExportOptions.IsExportDocumentStyles     = false;
                    doc.HtmlExportOptions.CssStyleSheetType          = Spire.Doc.CssStyleSheetType.Internal;
                    doc.HtmlExportOptions.HasHeadersFooters          = false;
                    doc.HtmlExportOptions.IsTextInputFormFieldAsText = true;

                    var outstr = new MemoryStream();
                    doc.SaveToStream(outstr, Spire.Doc.FileFormat.Html);
                    var rethtml = Encoding.GetEncoding("utf-8").GetString(outstr.ToArray()); //basicamente funciona com HTML

                    return(rethtml);
                }
            }
        }
Esempio n. 6
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());
        }
Esempio n. 7
0
        private MemoryStream PrepareCommonOrderPrintData(IOrdersManager ordersManager, int id, string path, PrintTypes type,
            IInvoicesManager invoicesManager, ITaxesManager taxesManager, IInvoiceStornosManager invoiceStornosManager = null,
            ITransportOrdersManager transportOrdersManager = null,
            ITermsManager termsManager = null)
        {
            var result = new MemoryStream();
            try
            {
                Package pkg;
                PackagePart part;
                XmlReader xmlReader;
                XDocument xmlMainXMLDoc;
                GetXmlDoc(path, result, out pkg, out part, out xmlReader, out xmlMainXMLDoc);


                var images = new List<Image>();

                if (type == PrintTypes.DeliveryNote)
                {
                    var term = termsManager.GetById(id);
                    for (var i = 0; i < term.DeliveryNoteSignatures.Count; i++)
                    {
                        var deliveryNoteSignature = term.DeliveryNoteSignatures.ElementAt(term.DeliveryNoteSignatures.Count - i - 1);
                        //TODO doesnt work ((
                        //pkg.CreateRelationship(uri, TargetMode.Internal,
                        //    "Http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
                        //    "barcodeImageId");


                        byte[] bytes = Convert.FromBase64String(deliveryNoteSignature.Signature.Replace("data:image/png;base64,", ""));

                        Image image;
                        using (MemoryStream ms = new MemoryStream(bytes))
                        {
                            image = Image.FromStream(ms);
                        }

                        images.Add(image);
                    }
                }

                //replace fields
                var templateBody = ReplaceFields(ordersManager, id, type, xmlMainXMLDoc.Root.ToString(),
                    invoicesManager, taxesManager, null, invoiceStornosManager, transportOrdersManager, termsManager, images);

                xmlMainXMLDoc = SaveDoc(result, pkg, part, xmlReader, xmlMainXMLDoc, templateBody);

                InsertImages(result, images);

                var doc = new Spire.Doc.Document();
                doc.LoadFromStream(result, Spire.Doc.FileFormat.Docx);
                doc.JPEGQuality = 100;

                result = new MemoryStream();
                doc.SaveToStream(result, Spire.Doc.FileFormat.PDF);
            }
            catch
            {
            }

            return result;
        }