首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Spire的C# merge pdf不编写内容

使用Spire的C# merge pdf不编写内容
EN

Stack Overflow用户
提问于 2016-12-24 01:26:48
回答 1查看 1.8K关注 0票数 1

我有下面的代码来合并不同的文档,并正确地创建一个pdf file.creates文件,但是没有在创建的文件上显示文档内容。它看起来像是创建了页码,但我看不到文档内容,它只是空白。这里错过了什么

代码语言:javascript
复制
private void buttonAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "All files (*.docx, *.pdf, *.pptx, *.pdf)| *.docx; *.pdf; *.pptx; *.xlsx";

            ofd.Multiselect = true;

            if (DialogResult.OK == ofd.ShowDialog())

            {

                string[] files = ofd.FileNames;

                listBoxFiles.Items.AddRange(files);

            }
        }

这是文档合并代码

代码语言:javascript
复制
 private void buttonMerge_Click(object sender, EventArgs e)
        {
            //Convert other file formats to PDF file
            string ext = string.Empty;
            foreach (object item in listBoxFiles.Items)
            {
                ext = Path.GetExtension(item.ToString());
                switch (ext)
                {
                    case ".docx":
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Document doc = new Document(item.ToString());
                            doc.SaveToStream(ms, Spire.Doc.FileFormat.PDF);
                            PdfFiles.Add(new PdfDocument(ms));
                        }
                        break;
                    case ".pdf":
                        PdfFiles.Add(new PdfDocument(item.ToString()));
                        break;
                    case ".pptx":
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Presentation ppt = new Presentation(item.ToString(), Spire.Presentation.FileFormat.Auto);
                            ppt.SaveToFile(ms, Spire.Presentation.FileFormat.PDF);
                            PdfFiles.Add(new PdfDocument(ms));
                        }
                        break;
                    case ".xlsx":
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Workbook xls = new Workbook();
                            xls.LoadFromFile(item.ToString());
                            xls.SaveToStream(ms, Spire.Xls.FileFormat.PDF);
                            PdfFiles.Add(new PdfDocument(ms));
                        }
                        break;
                    default:
                        break;
                }
            }
            //Merge the PDF files into one PDF
            PdfDocument newPdf1 = new PdfDocument();
            foreach (PdfDocument doc in PdfFiles)
            {
                newPdf1.AppendPage(doc);
            }
            //Create a new PDF with specified page size, copy the content of merged file to new PDF file
            PdfDocument newPdf2 = new PdfDocument();
            foreach (PdfPageBase page in newPdf1.Pages)
            {
                PdfPageBase newPage = newPdf2.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
                PdfTextLayout loLayout = new PdfTextLayout();
                loLayout.Layout = PdfLayoutType.OnePage;
                page.CreateTemplate().Draw(newPage, new PointF(0, 0), loLayout);
            }
            //Save the destination PDF file
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Pdf files(*.pdf)|*.pdf";
            if (DialogResult.OK == sfd.ShowDialog())
            {
                newPdf2.SaveToFile(sfd.FileName);
            }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-26 10:05:28

您应该使用新的合并方法而不是旧的合并方法。请检查以下代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Presentation;
using Spire.Xls;

namespace Merge_Office_Files_to_PDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();

        ofd.Filter = "All files (*.docx, *.pdf, *.pptx, *.pdf)| *.docx; *.pdf; *.pptx; *.xlsx";

        ofd.Multiselect = true;

        if (DialogResult.OK == ofd.ShowDialog())
        {

            string[] files = ofd.FileNames;

            listBox1.Items.AddRange(files);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {

        string ext = string.Empty;
        List<Stream> filesStreams = new List<Stream>();
        MemoryStream ms1 = new MemoryStream();
        MemoryStream ms2 = new MemoryStream();
        MemoryStream ms3 = new MemoryStream();
        foreach (object item in listBox1.Items)
        {
            ext = Path.GetExtension(item.ToString());
            switch (ext)
            {
                case ".docx":
                    Document doc = new Document(item.ToString());
                    doc.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
                    filesStreams.Add(ms1);

                    break;
                case ".pdf":
                    filesStreams.Add(File.OpenRead(item.ToString()));
                    break;
                case ".pptx":
                    Presentation ppt = new Presentation(item.ToString(), Spire.Presentation.FileFormat.Auto);
                    ppt.SaveToFile(ms2, Spire.Presentation.FileFormat.PDF);
                    filesStreams.Add(ms2);

                    break;
                case ".xlsx":
                    Workbook xls = new Workbook();
                    xls.LoadFromFile(item.ToString());
                    xls.SaveToStream(ms3, Spire.Xls.FileFormat.PDF);
                    filesStreams.Add(ms3);

                    break;
                default:
                    break;

            }
        }
        string outputFile = "result.pdf";
        PdfDocumentBase result = PdfDocument.MergeFiles(filesStreams.ToArray());
        result.Save(outputFile, Spire.Pdf.FileFormat.PDF);
        ms1.Close();
        ms2.Close();
        ms3.Close();            
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41309547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档