在C#云环境中将Word(DOCX)文件转换为PDF

本文关键字:DOCX 文件 转换 PDF Word 环境 | 更新日期: 2023-09-27 17:59:49

我已经使用Open Xml生成了一个word文件,我需要将其作为附件以pdf格式发送到电子邮件中,但我无法在磁盘上保存任何物理pdf或word文件,因为我在云环境(CRM在线)中开发我的应用程序。

我发现唯一的办法是"把单词放到网上"。http://www.aspose.com/docs/display/wordsnet/How+to++Convert+a+Document+to+a+Byte+Array但是它太贵了。

然后我发现一个解决方案是将word转换为html,然后将html转换为pdf。但我的话里有一幅图画。我无法解决这个问题。

在C#云环境中将Word(DOCX)文件转换为PDF

从DOCX到PDF的最准确转换将通过Word进行。你最好的选择是用OWAS(Office Web Apps server)建立一个服务器,并通过它进行转换。

您需要在应用程序服务器上设置一个WOPI端点,然后调用:

/wv/WordViewer/request.pdf?WOPISrc={WopiUrl}&type=downloadpdf

/wv/WordViewer/request.pdf?WOPISrc={WopiUrl}&type=printpdf

或者,你可以尝试使用OneDrive和Word Online,但你需要计算出Word Online使用的参数,以及这是否在T&Cs。

您可以尝试Gnostice XtremeDocumentStudio.NET.

使用XtremeDocumentStudio.NET从DOCX转换为PDFhttp://www.gnostice.com/goto.asp?id=24900&t=convert_docx_to_pdf_using_xdoc.net

在已发表的文章中,已经演示了将转换保存到物理文件。您可以使用documentConverter.ConvertToStream方法将文档转换为Stream,如下代码段所示。

DocumentConverter documentConverter = new DocumentConverter();
// input can be a FilePath, Stream, list of FilePaths or list of Streams
Object input = "InputDocument.docx";
string outputFileFormat = "pdf";
ConversionMode conversionMode = ConversionMode.ConvertToSeperateFiles;
List<Stream> outputStreams = documentConverter.ConvertToStream(input, outputFileFormat, conversionMode);

免责声明:我为诺斯替工作。

如果你想转换字节数组,那么使用变形:

            string docxPath = @"example.docx";
            string pdfPath = Path.ChangeExtension(docxPath, ".pdf");
            byte[] docx = File.ReadAllBytes(docxPath);
            //  Convert DOCX to PDF in memory
            byte[] pdf = p.DocxToPdfConvertByte(docx);
            if (pdf != null)
            {
                //  Save the PDF document to a file for a viewing purpose.
                File.WriteAllBytes(pdfPath, pdf);
                System.Diagnostics.Process.Start(pdfPath);
            }
            else
            {
                System.Console.WriteLine("Conversion failed!");
                Console.ReadLine();
            }

我最近在React(前端).Net核心(微服务-后端)应用程序中使用了SautinSoft的Document.Net库将docx转换为pdf。生成一个23页的pdf只需要15秒。这15秒包括从数据库中获取数据,然后将数据与docx模板合并,然后将其转换为pdf。该代码已部署到azure Linux盒子中,运行良好。

https://sautinsoft.com/products/document/

样本代码

public string GeneratePDF(PDFDocumentModel document)
        {
            byte[] output = null;
            using (var outputStream = new MemoryStream())
            {
                // Create single pdf.
                DocumentCore singlePDF = new DocumentCore();
                var documentCores = new List<DocumentCore>();
                foreach (var section in document.Sections)
                {
                    documentCores.Add(GenerateDocument(section));
                }
                foreach (var dc in documentCores)
                {
                    // Create import session.
                    ImportSession session = new ImportSession(dc, singlePDF, StyleImportingMode.KeepSourceFormatting);
                    // Loop through all sections in the source document.
                    foreach (Section sourceSection in dc.Sections)
                    {
                        // Because we are copying a section from one document to another,
                        // it is required to import the Section into the destination document.
                        // This adjusts any document-specific references to styles, bookmarks, etc.
                        // Importing a element creates a copy of the original element, but the copy
                        // is ready to be inserted into the destination document.
                        Section importedSection = singlePDF.Import<Section>(sourceSection, true, session);
                        // First section start from new page.
                        if (dc.Sections.IndexOf(sourceSection) == 0)
                            importedSection.PageSetup.SectionStart = SectionStart.NewPage;
                        // Now the new section can be appended to the destination document.
                        singlePDF.Sections.Add(importedSection);
                        //Paging 
                       
                        HeaderFooter footer = new HeaderFooter(singlePDF, HeaderFooterType.FooterDefault);
                        // Create a new paragraph to insert a page numbering.
                        // So that, our page numbering looks as: Page N of M.
                        Paragraph par = new Paragraph(singlePDF);
                        par.ParagraphFormat.Alignment = HorizontalAlignment.Center;
                        CharacterFormat cf = new CharacterFormat() { FontName = "Consolas", Size = 11.0 };
                        par.Content.Start.Insert("Page ", cf.Clone());
                        // Page numbering is a Field.
                        Field fPage = new Field(singlePDF, FieldType.Page);
                        fPage.CharacterFormat = cf.Clone();
                        par.Content.End.Insert(fPage.Content);
                        par.Content.End.Insert(" of ", cf.Clone());
                        Field fPages = new Field(singlePDF, FieldType.NumPages);
                        fPages.CharacterFormat = cf.Clone();
                        par.Content.End.Insert(fPages.Content);
                        footer.Blocks.Add(par);
                        importedSection.HeadersFooters.Add(footer);
                    }
                }
                var pdfOptions = new PdfSaveOptions();
                pdfOptions.Compression = false;
                pdfOptions.EmbedAllFonts = false;
                pdfOptions.EmbeddedImagesFormat = PdfSaveOptions.EmbImagesFormat.Png;
                pdfOptions.EmbeddedJpegQuality = 100;
                //dont allow editing after population, also ensures content can be printed.
                pdfOptions.PreserveFormFields = false;
                pdfOptions.PreserveContentControls = false;
                if (!string.IsNullOrEmpty(document.PdfProperties.Title))
                {
                    singlePDF.Document.Properties.BuiltIn[BuiltInDocumentProperty.Title] = document.PdfProperties.Title;
                }
    if (!string.IsNullOrEmpty(document.PdfProperties.Author))
    {
        singlePDF.Document.Properties.BuiltIn[BuiltInDocumentProperty.Author] = document.PdfProperties.Author;
    }
                if (!string.IsNullOrEmpty(document.PdfProperties.Subject))
                {
                    singlePDF.Document.Properties.BuiltIn[BuiltInDocumentProperty.Subject] = document.PdfProperties.Subject;
                }                          
                singlePDF.Save(outputStream, pdfOptions);
                output = outputStream.ToArray();
            }
            return Convert.ToBase64String(output);

        }