我正在使用 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));
然后您可以访问 PdfWriter
的 DirectContent
属性并调用其 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/