我有一个将pdf文件合并在一起的桌面应用程序。我的老板希望我让应用程序将excel文件转换为pdf,并允许应用程序将它们全部合并在一起。有人可以帮忙吗?除了将excel文件转换为pdf的功能之外,我拥有一切,以便我可以成功地将它们转换。
我不确定itextsharp是否允许我将excel转换为pdf并将它们合并在一起,但是请让我知道我能做什么。
namespace mergePdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected void btnMerge_Click(object sender, EventArgs e)
{
if (!Directory.Exists(@"C:\Merge Folder\"))
Directory.CreateDirectory(@"C:\Merge Folder\");
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "Select PDFFile";
saveFileDialog1.Filter = "PDF(*.pdf)|*.pdf";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
string pdfFile1 = Path.GetFullPath(textBoxPdfFile1Path.Text);
string[] filenames = { pdfFile1 };
string outputFileName = System.IO.Path.GetRandomFileName() +
".pdf"; ;
string outputPath = saveFileDialog1.FileName;
FileInfo fileInfo = new FileInfo(outputPath);
Console.WriteLine(fileInfo.DirectoryName);
Document doc = new Document();
PdfCopy writer = new PdfCopy(doc, new FileStream(outputPath,
FileMode.Create));
if (writer == null)
{
return;
}
doc.Open();
foreach (var filename in listBox1.Items)
{
PdfReader reader = new PdfReader(filename.ToString());
reader.ConsolidateNamedDestinations();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
reader.Close();
}
writer.Close();
doc.Close();
System.Diagnostics.Process.Start(outputPath);
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected void btnSelectFile1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
textBoxPdfFile1Path.Text = ofd.FileName;
string[] files = ofd.FileNames;
listBox1.Items.AddRange(files);
}
}
}
}