使用c#

时间:2017-02-12 13:54:10

标签: c# asp.net pdf

我正在使用visual studio 2012 c#asp.net,我正在寻找一种方法让用户通过Web应用程序合并PDF或图像文件,我在下面创建了一个代码,通过Web应用程序上传多个文件,我需要知道是否可以使用我的代码与iText(夏普)?或者它只适用于exe应用程序

Web.aspx:

<form id="form1" runat="server"  enctype="multipart/form-data">
      <div>
            <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
            <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick ="UploadMultipleFiles" accept ="image/gif, image/jpeg" />
            <hr />
            <asp:Label ID="lblSuccess" runat="server" ForeColor ="Green" />
      </div>
</form>

Web.aspx.cs:

protected void UploadMultipleFiles(object sender, EventArgs e)
{
   foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
   {

      string fileName = Path.GetFileName(postedFile.FileName);
      postedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
   }

   lblSuccess.Text = string.Format("{0} files have been uploaded  successfully.", FileUpload1.PostedFiles.Count);
}

1 个答案:

答案 0 :(得分:0)

以下iTextSharp代码将mutlitple pdf文件合并为一个文件,请参阅so question以获取更多信息

public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
// step 1: creation of a document-object
Document document = new Document();

// step 2: we create a writer that listens to the document
PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
if (writer == null)
{
    return;
}

// step 3: we open the document
document.Open();

foreach (string fileName in fileNames)
{
    // we create a reader for a certain document
    PdfReader reader = new PdfReader(fileName);
    reader.ConsolidateNamedDestinations();

    // step 4: we add content
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {                
        PdfImportedPage page = writer.GetImportedPage(reader, i);
        writer.AddPage(page);
    }

    PRAcroForm form = reader.AcroForm;
    if (form != null)
    {
        writer.CopyAcroForm(reader);
    }

    reader.Close();
}

// step 5: we close the document and writer
writer.Close();
document.Close();

}