如何使用 iTextSharp 转换为 PDF
How to use iTextSharp to convert to 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
文件夹中。
首先,在你的情况下,mergeTiff 方法应该有一个文档 属性,你在其中传入你创建一次的文档,因为此时你正在创建多个文档,每个文档都包含所有 tiff - 至少都保存在 result2.pdf.
只是为了让你开始(没有测试它,它显然应该进一步优化)...
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();
}
public void btnExecute_Click(string strFolder, string strDestination, string[] strFileArray)
{
//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.pdf.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)
{
Bitmap bm = null;
int total = 0;
string str = string.Empty;
try
{
str = image.Substring(image.LastIndexOf("\"));
bm = new System.Drawing.Bitmap("TiffFolder"+ 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 = pdfWriter.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();
}
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
文件夹中。
首先,在你的情况下,mergeTiff 方法应该有一个文档 属性,你在其中传入你创建一次的文档,因为此时你正在创建多个文档,每个文档都包含所有 tiff - 至少都保存在 result2.pdf.
只是为了让你开始(没有测试它,它显然应该进一步优化)...
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();
}
public void btnExecute_Click(string strFolder, string strDestination, string[] strFileArray)
{
//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.pdf.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)
{
Bitmap bm = null;
int total = 0;
string str = string.Empty;
try
{
str = image.Substring(image.LastIndexOf("\"));
bm = new System.Drawing.Bitmap("TiffFolder"+ 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 = pdfWriter.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();
}