c# - iTextSharp - PDF - 调整文档大小以容纳大图像

标签 c# .net image pdf itext

我正在使用 iTextSharp 将大图像转换为 PDF 文档。

这可行,但图像看起来被裁剪了,因为它们超出了生成文档的边界。

所以问题是 - 如何使文档与插入其中的图像大小相同?

我正在使用以下代码:

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

最佳答案

iText 和 iTextSharp 中的 Document 对象是一种抽象,可以自动为您处理各种间距、填充和边距。不幸的是,这也意味着当您调用 doc.Add() 时,它会考虑文档的现有页边距。 (此外,如果您碰巧添加了其他任何内容,图像也会相对于它添加。)

一个解决方案是只删除边距:

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

相反,将图像直接添加到通过调用 PdfWriter.GetInstance() 获得的 PdfWriter 对象会更容易。您目前正在丢弃而不是存储该对象,但您可以轻松地将行更改为:

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

然后您可以访问 PdfWriterDirectContent 属性并调用其 AddImage() 方法:

writer.DirectContent.AddImage(img);

在执行此操作之前,您还必须通过调用绝对定位图像:

img.SetAbsolutePosition(0, 0);

下面是针对 iTextSharp 5.1.1.0 的完整工作 C# 2010 WinForms 应用程序,它显示了上面的 DirectContent 方法。它动态创建两个不同大小的图像,两个红色箭头在垂直和水平方向上延伸。您的代码显然只使用标准图像加载,因此可以省略很多内容,但我想提供一个完整的工作示例。有关详细信息,请参阅代码中的注释。

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

关于c# - iTextSharp - PDF - 调整文档大小以容纳大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713963/

相关文章:

c# - 我怎么知道RTF语法的哪一部分是错误的

c# - 在执行 C# 自定义 Cmdlet 期间收集用户输入(不通过参数)

c# - 如何在 C# 中编写 bresenham 算法?

c# - 将 JSON 反序列化为 C# 动态对象?

c# - 如何在加载应用程序时显示图像

二进制文件中的 C# "hex shift"

java - C#/Java 中是否有任何 GLL 解析器组合器库?

.net - 确定文件是否可以 move 或复制

android - 如何在 Android Activity 之间发送大字节数组?

jquery - 如何在网站内容后面创建居中的图像幻灯片