使用iTextSharp的添加图像到PDF和正确缩放 [英] Adding an image to a PDF using iTextSharp and scale it properly

查看:6478
本文介绍了使用iTextSharp的添加图像到PDF和正确缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code。它正确地增加了我想要的图片和一切工作的除了,该图像是使用原始分辨率,所以如果图像是大它被裁剪以适合页面。

有一些办法让画面使用像变焦功能延伸到适合,而且还保持宽高比?必须有我丢失的东西在那里。 :P

下面是一个图片来说明这个问题:

 使用系统;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用System.Drawing中;
使用System.Collections.Generic;命名空间WinformsPlayground
{
    公共类PDFWrapper
    {
        公共无效CreatePDF(列表<&System.Drawing.Image对象GT;图片)
        {
            如果(images.Count> = 1)
            {
                文档的文档=新的文件(PageSize.LETTER);
                尝试
                {                    // 第2步:
                    //我们创建侦听到文档中的作家
                    //和指导PDF-流文件                    PdfWriter.GetInstance(文件,新的FileStream(Chap0101.pdf,FileMode.Create));                    //第3步:我们打开文档
                    document.Open();                    的foreach(在图像VAR图片)
                    {
                        iTextSharp.text.Image PIC = iTextSharp.text.Image.GetInstance(图像,System.Drawing.Imaging.ImageFormat.Jpeg);
                        document.Add(PIC);
                        document.NewPage();
                    }
                }
                赶上(DocumentException德)
                {
                    Console.Error.WriteLine(de.Message);
                }
                赶上(IOException异常IOE)
                {
                    Console.Error.WriteLine(ioe.Message);
                }                //第5步:我们关闭文档
                document.Close();
            }
        }
    }
}


解决方案

我解决它使用以下内容:

 的foreach(在图像VAR图片)
{
    iTextSharp.text.Image PIC = iTextSharp.text.Image.GetInstance(图像,System.Drawing.Imaging.ImageFormat.Jpeg);    如果(pic.Height> pic.Width)
    {
        //最大高度为800像素。
        浮动比例= 0.0;
        百分比= 700 / pic.Height;
        pic.ScalePercent(百分比* 100);
    }
    其他
    {
        //最大宽度为600像素。
        浮动比例= 0.0;
        百分比= 540 / pic.Width;
        pic.ScalePercent(百分比* 100);
    }    pic.Border = iTextSharp.text.Rectangle.BOX;
    pic.BorderColor = iTextSharp.text.BaseColor.BLACK;
    pic.BorderWidth = 3F;
    document.Add(PIC);
    document.NewPage();
}

here's my code. It correctly adds the pictures I want and everything works except that the images are using their native resolution, so if the image is big it's being cropped to fit the page.

Is there some way to have the picture use like a Zoom feature to stretch to fit, but also maintain the aspect ratio? There has to be something I'm missing there. :P

Here's a picture to illustrate the problem:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing;
using System.Collections.Generic;

namespace WinformsPlayground
{
    public class PDFWrapper
    {
        public void CreatePDF(List<System.Drawing.Image> images)
        {
            if (images.Count >= 1)
            {
                Document document = new Document(PageSize.LETTER);
                try
                {

                    // step 2:
                    // we create a writer that listens to the document
                    // and directs a PDF-stream to a file

                    PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));

                    // step 3: we open the document
                    document.Open();

                    foreach (var image in images)
                    {
                        iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
                        document.Add(pic);
                        document.NewPage();
                    }
                }
                catch (DocumentException de)
                {
                    Console.Error.WriteLine(de.Message);
                }
                catch (IOException ioe)
                {
                    Console.Error.WriteLine(ioe.Message);
                }

                // step 5: we close the document
                document.Close();
            }
        }
    }
}

解决方案

I solved it using the following:

foreach (var image in images)
{
    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);

    if (pic.Height > pic.Width)
    {
        //Maximum height is 800 pixels.
        float percentage = 0.0f;
        percentage = 700 / pic.Height;
        pic.ScalePercent(percentage * 100);
    }
    else
    {
        //Maximum width is 600 pixels.
        float percentage = 0.0f;
        percentage = 540 / pic.Width;
        pic.ScalePercent(percentage * 100);
    }

    pic.Border = iTextSharp.text.Rectangle.BOX;
    pic.BorderColor = iTextSharp.text.BaseColor.BLACK;
    pic.BorderWidth = 3f;
    document.Add(pic);
    document.NewPage();
}

这篇关于使用iTextSharp的添加图像到PDF和正确缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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