如何使用itextsharp在一个pdf中打印多个文档

时间:2017-04-28 15:00:36

标签: c# pdf itext pdf-generation

我想在一个pdf文件中打印多个文档。

 private bool CreateBatchDetails(string path, string PDFFilename, List<BatchDetails> _bd)
{
    bool chk = false;
    DataSet ds = new DataSet();
    try
    {
        foreach (var item in _bd)
        {
            var _IsSpecialPullID = (...................
                                    select new
                                    {
                                        m.PullId,
                                        m.RentalOrderId
                                    }).FirstOrDefault();

            if ((_IsSpecialPullID.RentalOrderId == null) || (_IsSpecialPullID.RentalOrderId.Value.ToString() == string.Empty))
            {

                ds = SqlHelper.ExecuteDataset(ConnectionString, "Stored Procedure1", _IsSpecialPullID.PullId);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    chk = CreatePullSheetPdf(PDFFilename, ConfigurationManager.AppSettings["SitePath"].ToString() + @"images\nemc-pdf-logo.gif", ds);
                }
            }
            else
            {
                ds = SqlHelper.ExecuteDataset(ConnectionString, "Stored Procedure2", _IsSpecialPullID.PullId);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    tbl ro = XYZDb.tbl.Where(t => t.OrderId == _IsSpecialPullID.RentalOrderId).FirstOrDefault();
                    chk = PDFGeneration.CreatePullSheetPdfSpecialOrder(PDFFilename, ConfigurationManager.AppSettings["SitePath"].ToString() + @"images\nemc-pdf-logo.gif", ds, RentalOrder, Server.HtmlDecode(ds.Tables[0].Rows[0]["Comments"].ToString()));
                }
            }
        }
        return chk;
    }
    catch (Exception ex)
    {
        return chk = false;
    }

CreatePullSheetPdf PDFGeneration.CreatePullSheetPdfSpecialOrder 都在创建itextsharp文档,并返回布尔值。

我只想将所有文件打成一个文件。我怎样才能做到这一点?

正在调用 CreateBatchDetails 的主程序如下:

 PdfCreated = CreateBatchDetails(path, PDFFilename, _bd);
     try
      {
        if (PdfCreated)
        {
            try
            {

                FileInfo file = new FileInfo(PDFFilename);
                file.AppendText();

              if (file.Exists)
                {

                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Cache.SetExpires(DateTime.Now);
                    Response.Cache.SetNoServerCaching();
                    Response.Cache.SetNoStore();

                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + file.Name);
                    HttpContext.Current.Response.TransmitFile(PDFFilename);


                }
                string filename = PDFFilename;
                    return true;
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.TransmitFile(PDFFilename);
                if (File.Exists(PDFFilename) == true)
                {
                    File.Delete(PDFFilename);
                }

                LogGeneration.GenerateLog(LogType.UserErrorLog, Common.GetIpAddress(), string.Empty,
                    "nemcadmin/App_code/PDFGeneration.cs[Method:private public static bool CreatePackaginglistPdf(string RentalContractId, int pastDue, out string message, out string PDFFilename) - Cannot generate pdf file]:- \n"
                    + ex.Message.ToString() + " " + ex.StackTrace, "error", SeverityLevel.Critical);
                message = "Cannot generate pdf file";
                 return false;
            }

任何形式的帮助将不胜感激....感谢任何进展......欢迎所有问题

请提供帮助

1 个答案:

答案 0 :(得分:0)

创建一个新的PDF文件。

Document newPDF = new Document();
PdfCopy mergedPDFs = new PdfCopy(newPDF, new FileStream(pdfPath, FileMode.Create));
document.Open();

在要合并的PDF上循环并阅读它们。

foreach (string pdfFile in ListOfPDFsToMergePaths)
{
    PdfReader reader = new PdfReader(pdfFile);
    for (int i = 0; i < reader.NumberOfPages; i++)
    {
        mergedPDFs.AddPage(mergedPDFs.GetImportedPage(reader, i));
    }
}

mergedPDFs.Close();
newPDF.Close();