iTextSharp的 - PDF - 调整文件,以容纳一个大的图像 [英] iTextSharp - PDF - Resize Document to Accomodate a Large Image

查看:1024
本文介绍了iTextSharp的 - PDF - 调整文件,以容纳一个大的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的大图像转换为PDF文档。

这工作,但显示图像裁剪,因为它们超出了生成文档的边界。

所以,问题是 - 如何使文件大小相同的图像被插入到什么?

我用下面的code:

 文档DOC =新的文件(PageSize.LETTER.Rotate());
  尝试
  {
     PdfWriter.GetInstance(文件,新的FileStream(saveFileDialog1.FileName,FileMode.Create));
     doc.Open();
     doc.Add(新的第());
     iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(的ImagePath);
     doc.Add(IMG);
   }
   抓住
   {
      //添加一些code在这里柜面你有一个例外
   }
   最后
   {
      //自由创建文档实例,以及
      doc.Close();
   }
 

解决方案

在iText的和iTextSharp的的文件对象是一个抽象,采取各种间距,填充和利润率的护理自动为您。不幸的是你,这也意味着,当你调用 doc.Add()需要到文档的考虑现有的利润。 (另外,如果你碰巧添加其他的图像将被添加相对于这一点。)

一个解决办法是只删除页边距:

  doc.SetMargins(0,0,0,0);
 

相反,它更容易直接添加图像,你从调用 PdfWriter.GetInstance() PdfWriter 对象C>。你现在扔掉,而不是存储的对象,但您可以轻松地将路线更改为:

  PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(saveFileDialog1.FileName,FileMode.Create));
 

然后,您可以访问 PdfWriter DirectContent 属性并调用它的 AddImage( )方法:

  writer.DirectContent.AddImage(IMG);
 

在这样做,你还必须完全通过调用定位图像:

  img.SetAbsolutePosition(0,0);
 

下面是一个完整的工作C#2010 WinForms应用程序针对iTextSharp的5.1.1.0,显示上方的 DirectContent 方法。它动态地创建两个大小不同的两个红色箭头跨两个拉伸垂直和水平的图像。您的code显然用标准的图像加载,因此可以省略很多,但我想提供一个完整的工作的例子。请参见code中的注意事项了解更多详情。

 使用系统;
使用System.Drawing中;
使用System.Windows.Forms的;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;

命名空间WindowsFormsApplication1 {
    公共部分Form1类:表格{
        公共Form1中(){
            的InitializeComponent();
        }

        私人无效Form1_Load的(对象发件人,EventArgs的){
            //文件到了写
            串outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),Images.pdf);

            //标准的PDF创建
            使用(的FileStream FS =新的FileStream(outputFilename,FileMode.Create,FileAccess.Write,FileShare.None)){
                //注意,我们不是设置一个文件大小在这里的是,我们会做的,后来
                使用(DOC文件=新的文件()){
                    使用(PdfWriter作家= PdfWriter.GetInstance(DOC,FS)){
                        doc.Open();

                        //创建一个简单的位图有两个红色箭头延伸穿过它
                        使用(位图B1 =新位图(100,400)){
                            使用(图形G1 = Graphics.FromImage(B1)){
                                使用(笔P1 =新钢笔(Color.Red,10)){
                                    p1.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    p1.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    g1.DrawLine(P1,0,b1.Height / 2,b1.Width,b1.Height / 2);
                                    g1.DrawLine(P1,b1.Width / 2,0,b1.Width / 2,b1.Height);

                                    //从位图创建一个iTextSharp的图像(我们需要指定一个背景颜色,我认为它是与透明度)
                                    iTextSharp.text.Image IMG1 = iTextSharp.text.Image.GetInstance(B1,BaseColor.WHITE);
                                    //绝对定位图像
                                    img1.SetAbsolutePosition(0,0);
                                    //改变页面大小的下一个页面添加到源图像匹配
                                    doc.SetPageSize(新iTextSharp.text.Rectangle(0,0,b1.Width,b1.Height,0));
                                    //添加了新的一页
                                    doc.NewPage();
                                    //直接添加图像作家
                                    writer.DirectContent.AddImage(IMG1);
                                }
                            }
                        }

                        //重复以上但有一个更大的和更广泛的图像
                        使用(位图B2 =新位图(4000,1000)){
                            使用(图形G2 = Graphics.FromImage(B2)){
                                使用(笔P2 =新钢笔(Color.Red,10)){
                                    p2.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    g2.DrawLine(P2,0,b2.Height / 2,b2.Width,b2.Height / 2);
                                    g2.DrawLine(P2,b2.Width / 2,0,b2.Width / 2,b2.Height);
                                    iTextSharp.text.Image IMG2 = iTextSharp.text.Image.GetInstance(B2,BaseColor.WHITE);
                                    img2.SetAbsolutePosition(0,0);
                                    doc.SetPageSize(新iTextSharp.text.Rectangle(0,0,b2.Width,b2.Height,0));
                                    doc.NewPage();
                                    writer.DirectContent.AddImage(IMG2);
                                }
                            }
                        }


                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}
 

I'm using iTextSharp to convert large images to PDF documents.

This works, but the images appear cropped, because they exceed boundaries of the generated document.

So the question is - how to make the document same size as the image being inserted into it?

I'm using the following code:

  Document doc = new Document(PageSize.LETTER.Rotate());
  try
  {
     PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));
     doc.Open();
     doc.Add(new Paragraph());
     iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
     doc.Add(img);
   }
   catch
   {
      // add some code here incase you have an exception
   }
   finally
   {
      //Free the instance of the created doc as well
      doc.Close();
   }

解决方案

The Document object in iText and iTextSharp is an abstraction that takes care of various spacings, paddings and margins for you automatically. Unfortunately for you, this also means that when you call doc.Add() it takes into account existing margins of the document. (Also, if you happen to add anything else the image will be added relative to that, too.)

One solution would be to just remove the margins:

doc.SetMargins(0, 0, 0, 0);

Instead, it's easier to add the image directly to the PdfWriter object which you get from calling PdfWriter.GetInstance(). You're currently throwing away and not storing that object but you can easily change your line to:

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(saveFileDialog1.FileName,FileMode.Create));

Then you can access the DirectContent property of the PdfWriter and call its AddImage() method:

writer.DirectContent.AddImage(img);

Before doing this you must also absolutely position the image by calling:

img.SetAbsolutePosition(0, 0);

Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.0 that shows the DirectContent method above. It dynamically creates two images of different sizes with two red arrows stretching across both vertically and horizontally. Your code would obviously just use standard image loading and could thus omit a lot of this but I wanted to deliver a full working example. See the notes in the code for more details.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            //File to write out
            string outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Images.pdf");

            //Standard PDF creation
            using (FileStream fs = new FileStream(outputFilename, FileMode.Create, FileAccess.Write, FileShare.None)) {
                //NOTE, we are not setting a document size here at all, we'll do that later
                using (Document doc = new Document()) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();

                        //Create a simple bitmap with two red arrows stretching across it
                        using (Bitmap b1 = new Bitmap(100, 400)) {
                            using (Graphics g1 = Graphics.FromImage(b1)) {
                                using(Pen p1 = new Pen(Color.Red,10)){
                                    p1.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    p1.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    g1.DrawLine(p1, 0, b1.Height / 2, b1.Width, b1.Height / 2);
                                    g1.DrawLine(p1, b1.Width / 2, 0, b1.Width / 2, b1.Height);

                                    //Create an iTextSharp image from the bitmap (we need to specify a background color, I think it has to do with transparency)
                                    iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(b1, BaseColor.WHITE);
                                    //Absolutely position the image
                                    img1.SetAbsolutePosition(0, 0);
                                    //Change the page size for the next page added to match the source image
                                    doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b1.Width, b1.Height, 0));
                                    //Add a new page
                                    doc.NewPage();
                                    //Add the image directly to the writer
                                    writer.DirectContent.AddImage(img1);
                                }
                            }
                        }

                        //Repeat the above but with a larger and wider image
                        using (Bitmap b2 = new Bitmap(4000, 1000)) {
                            using (Graphics g2 = Graphics.FromImage(b2)) {
                                using (Pen p2 = new Pen(Color.Red, 10)) {
                                    p2.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    p2.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
                                    g2.DrawLine(p2, 0, b2.Height / 2, b2.Width, b2.Height / 2);
                                    g2.DrawLine(p2, b2.Width / 2, 0, b2.Width / 2, b2.Height);
                                    iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(b2, BaseColor.WHITE);
                                    img2.SetAbsolutePosition(0, 0);
                                    doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, b2.Width, b2.Height, 0));
                                    doc.NewPage();
                                    writer.DirectContent.AddImage(img2);
                                }
                            }
                        }


                        doc.Close();
                    }
                }
            }
            this.Close();
        }
    }
}

这篇关于iTextSharp的 - PDF - 调整文件,以容纳一个大的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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