Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用iTextSharp将多个TIFF图像转换为PDF_C#_Asp.net_Itext - Fatal编程技术网

C# 使用iTextSharp将多个TIFF图像转换为PDF

C# 使用iTextSharp将多个TIFF图像转换为PDF,c#,asp.net,itext,C#,Asp.net,Itext,我正在使用ASP.NET和iTextSharp PDF库中的网站。我有一个包含3页的tiff文档图像,我想将所有这3页tiff页面转换为一个包含3页的PDF文件 我试过了,但效果不太好 请告诉我该怎么办 using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; Document document = new Document(); using (var stream = new FileStream(@"C:\File

我正在使用ASP.NET和iTextSharp PDF库中的网站。我有一个包含3页的tiff文档图像,我想将所有这3页tiff页面转换为一个包含3页的PDF文件

我试过了,但效果不太好

请告诉我该怎么办

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

Document document = new Document();
using (var stream = new FileStream(@"C:\File\0.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
    PdfWriter.GetInstance(document, stream);
    document.Open();
    using (var imageStream = new FileStream(@"C:\File\0.tiff", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        var image = iTextSharp.text.Image.GetInstance(imageStream);
        document.Add(image);
    }
    document.Close();
}

我只是从中复制了代码,并将其修改为您的示例。因此,积分归在链接中回答问题的人

using (var stream = new FileStream(@"C:\File\0.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    var writer = PdfWriter.GetInstance(document, stream);    
    var bitmap = new System.Drawing.Bitmap(@"C:\File\0.tiff"); 
    var pages = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

    document.Open();
    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
    for (int i = 0; i < pages; ++i)
    {
        bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, 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();
}
使用(var stream=newfilestream(@“C:\File\0.pdf”,FileMode.Create,FileAccess.Write,FileShare.None))
{
文档=新文档(PageSize.A4,0,0,0);
var writer=PdfWriter.GetInstance(文档、流);
var bitmap=new System.Drawing.bitmap(@“C:\File\0.tiff”);
var pages=bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
document.Open();
iTextSharp.text.pdf.PdfContentByte cb=writer.DirectContent;
对于(int i=0;i
//创建具有特定大小和特定边距的文档
iTextSharp.text.Document Document=新的iTextSharp.text.Document(iTextSharp.text.PageSize.A4,0,0,0);
//不同作家的创作
iTextSharp.text.pdf.PdfWriter writer=iTextSharp.text.pdf.PdfWriter.GetInstance(文档,new System.IO.FileStream(Server.MapPath(“~/App_Data/result.pdf”)、System.IO.FileMode.Create));
//加载tiff图像并计算总页数
System.Drawing.Bitmap bm=新的System.Drawing.Bitmap(Server.MapPath(“~/App_Data/source.tif”);
int total=bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
document.Open();
iTextSharp.text.pdf.PdfContentByte cb=writer.DirectContent;
对于(整数k=0;k
@mjwills此代码仅将一页转换为PDF,我在该文件中有3页,我应该说TIFF文件仅将图片的一小部分转换为PDF。请看,foreach循环的内容(
foreach(文件中的字符串图像)
)@jesse de我的代码是哪一部分?你能给出答案吗?谢谢你,朋友,但请告诉我,我应该如何在PDF页面中设置图像。现在,我将TIFF文件转换为PDF,但PDF文件未打开,并且出现错误:(您是否取消了对缩放和定位部分的注释?另外,在第二行代码中添加了文档大小。是的。我编写了您的示例并执行了该程序,它工作正常,但如果您尝试在文档中添加页面大小,则结果为PDF,而不是openedPerhaps。请参阅更新的答案。或者使scalepercent非常小?尝试70f。如果没有,请尝试70f。)工作,试试3f,只是为了确定尺寸是否有问题。如果这不起作用,恐怕我无法帮你。
// creation of the document with a certain size and certain margins
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

// creation of the different writers
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(Server.MapPath("~/App_Data/result.pdf"), System.IO.FileMode.Create));

// load the tiff image and count the total pages
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(Server.MapPath("~/App_Data/source.tif"));
int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

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();