Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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创建的pdf中的图像大小不正确_C#_Itextsharp - Fatal编程技术网

C# 使用itextsharp创建的pdf中的图像大小不正确

C# 使用itextsharp创建的pdf中的图像大小不正确,c#,itextsharp,C#,Itextsharp,我在使用itextsharp从.tiff中的图像创建pdf时遇到问题。 下面是一些代码: iTextSharp.text.Document d = new iTextSharp.text.Document(); PdfWriter pw = PdfWriter.GetInstance(d, new FileStream(filename, FileMode.Create)); d.Open(); PdfContentByte cb

我在使用itextsharp从.tiff中的图像创建pdf时遇到问题。 下面是一些代码:

        iTextSharp.text.Document d = new iTextSharp.text.Document();
        PdfWriter pw = PdfWriter.GetInstance(d, new FileStream(filename, FileMode.Create));
        d.Open();

        PdfContentByte cb = pw.DirectContent;
        foreach (Image img in imgs)
        {
            d.NewPage();
            d.SetPageSize(new iTextSharp.text.Rectangle(0, 0, img.Width, img.Height));
            iTextSharp.text.Image timg = iTextSharp.text.Image.GetInstance(img, iTextSharp.text.BaseColor.WHITE);
            timg.SetAbsolutePosition(0, 0);
            cb.AddImage(timg);
            cb.Stroke();
        }
        d.Close();
它用两页创建pdf,但第一页上的图像太大。
该页面具有图像的大小,但它会缩放图像的左下角。 它只对tiff图像执行此操作,如果我使用png,效果很好


有什么解决办法吗?

多亏了mkl的评论,我找到了。 在新页面命令(NewPage)之前设置页面大小(SetPageSize)

如下使用

string[] validFileTypes = {"tiff"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isValidFile = false;
if (!isValidFile)
{
Label.Text = "Invalid File. Please upload a File with extension " +
                       string.Join(",", validFileTypes);
}
 else
    {
        string pdfpath = Server.MapPath("pdf");
        Document doc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
        PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf", FileMode.Create));
        doc.Open();

        string savePath = Server.MapPath("images\\");
        if (FileUpload1.PostedFile.ContentLength != 0)
          {
            string path = savePath + FileUpload1.FileName;
            FileUpload1.SaveAs(path);
            iTextSharp.text.Image tiff= iTextSharp.text.Image.GetInstance(path);
            tiff.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height);
            tiff.SetAbsolutePosition(0,0);
            PdfPTable table = new PdfPTable(1);
            table.AddCell(new PdfPCell(tiff));
            doc.Add(table);
          }
         doc.Close();
      }

页面大小不应该在创建新页面之前设置吗?如果我没记错的话,一旦创建了一个页面,它的大小是固定的。