如何使用iTextSharp转换为PDF [英] How to use iTextSharp to convert to PDF

查看:85
本文介绍了如何使用iTextSharp转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP:

<asp:Label ID="Label1" runat="server" Text="Folder Location: "></asp:Label> <asp:TextBox ID="tbFolder" runat="server"></asp:TextBox> 
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Destination Folder: "></asp:Label> <asp:TextBox ID="tbDestination" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnExecute" runat="server" Text="Button" OnClick="btnExecute_Click" />

隐藏代码:

public void btnExecute_Click(object sender, EventArgs e)
{
    try
    {
        strFolder = tbFolder.Text;
    }
    catch (Exception)
    {
        strFolder = "";
    }
    try
    {
        strDestination = tbDestination.Text;
    }
    catch (Exception)
    {
        strDestination = "";
    }
    try
    {
        strFileArray = Directory.GetFiles(strFolder, "*.tif");
        meregTiff(strFileArray);
    }
    catch (Exception)
    {
    }
}
public void meregTiff(string[] files)
{
    // Create the PDF with the proper parameters (change as you please)
    iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

    // Ensure the path to the folder is located where all the merged TIFF files will be saved as a PDF
    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(strDestination + "/result2.pdf", System.IO.FileMode.Create));

    // Load each tiff files and convert it into a BITMAP and save it as a PDF
    // Recommendation: Use TRY/CATCH method to ensure any errors are handled properly...
    foreach (string image in files)
    {
        try
        {
            str = image.Substring(image.LastIndexOf("\\"));
            bm = new System.Drawing.Bitmap(Server.MapPath("~/TiffImages" + str)); //modify this to ensure the file exists (can be same as the page_load method)
            total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        }
        catch (Exception ce) //getting error here... Parameter is invalid.
        {
        }

        document.Open();
        iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
        for (int k = 0; k < total; ++k)
        {
            bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
            // scale the image to fit in the page 
            img.ScalePercent(72f / img.DpiX * 100);
            img.SetAbsolutePosition(0, 0);
            cb.AddImage(img);
            document.NewPage();
        }
    }
    document.Close();
}

在这里遇到错误:catch (Exception ce) //getting error here... Parameter is invalid.

我该如何解决它,以便将文件从tbFolder文件夹中提取并另存为一个PDF到tbDestination文件夹中.

How can I resolve it so the files are taken from the tbFolder folder and saved as one PDF to the tbDestination folder.

推荐答案

首先,在您的情况下,mergeTiff方法应具有Document属性,您可以在其中传递一次创建的文档,因为在创建时几个文档,其中每个文档都包含所有tiff-至少所有文档都保存在result2.pdf中.

First of all in your case the mergeTiff method should have a Document property, where you pass in the document you create once, because right at the moment you are creating several documents where each document contains all tiffs - at least the are all saved in result2.pdf.

只是让您开始(没有测试它,显然应该对其进行进一步优化)...

Just to get you started (didn't test it and it clearly should be further optimized)...

public void btnExecute_Click(object sender, EventArgs e)
{   
    strFolder = tbFolder.Text;  
    strDestination = tbDestination.Text;
    strFileArray = Directory.GetFiles(strFolder, "*.tif");

    // Create the PDF with the proper parameters (change as you please)
    iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

    // Ensure the path to the folder is located where all the merged TIFF files will be saved as a PDF  
    iTextSharp.text.pdf.PdfWriter writer = 
       iTextSharp.text.pdf.PdfWriter.GetInstance(document, 
       new System.IO.FileStream(strDestination + "/result2.pdf", System.IO.FileMode.Create));

    meregTiff(document, writer, strFileArray);

}

public void meregTiff(iTextSharp.text.Document document, iTextSharp.text.PdfWriter pdfWriter, string[] files)
{ 
// Load each tiff files and convert it into a BITMAP and save it as a PDF
// Recommendation: Use TRY/CATCH method to ensure any errors are handled properly...
foreach (string image in files)
{
    try
    {
        str = image.Substring(image.LastIndexOf("\\"));
        bm = new System.Drawing.Bitmap(Server.MapPath("~/TiffImages" + str)); //modify this to ensure the file exists (can be same as the page_load method)
        total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
    }
    catch (Exception ce) //getting error here... Parameter is invalid.
    {
    }

    document.Open();
    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
    for (int k = 0; k < total; ++k)
    {
        bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
        // scale the image to fit in the page 
        img.ScalePercent(72f / img.DpiX * 100);
        img.SetAbsolutePosition(0, 0);
        cb.AddImage(img);
        document.NewPage();
    }
   }
   document.Close();
 }

这篇关于如何使用iTextSharp转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆