与iTextSharp的图像转换为PDF格式保存剪辑路径 [英] Converting images to PDF with iTextSharp preserve clipping path

查看:546
本文介绍了与iTextSharp的图像转换为PDF格式保存剪辑路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在寻找批量图像转换为PDF格式,编程。到目前为止,它看起来我们将使用iTextSharp的,但我们必须与剪辑路径JPG图片的问题。我们使用在我们的测试下面的代码:

We're looking to convert images in bulk to PDF, programmatically. So far it looks like we will be using iTextSharp but we have an issue with JPG images with clipping path. We are using the following code in our tests:

using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None))
{
    using (Document doc = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
        {
            doc.Open();
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(source);

            image.SetAbsolutePosition(0, 0);
            doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0));
            doc.NewPage();

            writer.DirectContent.AddImage(image,false); 

            doc.Close();
        }
    }
}

在JPG图像剪切路径似乎只是被丢弃。有没有办法保存剪切路径?还呼吁AddImage有用于InlineImage一个选项时,任何人都知道这是什么呢?

Clipping path in JPG images seems to just be discarded. Is there a way to preserve the clipping path? Also when calling AddImage there is an option for InlineImage, anyone knows what this does?

推荐答案

iText的副本的一个JPG直字节到PDF。不是一个单一的字节被改变。如果你说你的JPG文件具有剪切路径(我从来没有听说过这样的事),你没有看到这个功能在PDF中,你正面临着固有的PDF的限制,而不是iText的。 iText的甚至不看JPG字节:它只是创建了过滤器DCTDecode一个PDF流对象

iText copies the bytes of a JPG straight into the PDF. Not a single byte is changed. If you say that your JPGs have clipping paths (I've never heard of such a thing) and you don't see that feature in the PDF, you are being confronted with a limitation inherent to PDF, not to iText. iText doesn't even look at the JPG bytes: it just creates a PDF stream object with the filter DCTDecode.

您将不得不申请剪贴路径的的添加图像到PDF。正如你可能知道,PDF格式不支持PNG格式和PNG支持透明度。当iText的遇到一个透明的PNG,它处理PNG。它创建两个图像:使用一个不透明的图像 / FlateDecode ,并使用 / FlateDecode 一个黑白图像。不透明图像被附加了单色图象作为其掩模以获得透明度。我猜你必须预处理以同样的方式您的JPG

You will have to apply the clipping path before adding the image to the PDF. As you may know, PDF doesn't support PNGs and PNG supports transparency. When iText encounters a transparent PNG, it processes the PNG. It creates two images: one opaque image using /FlateDecode and one monochrome image using /FlateDecode. The opaque image is added with the monochrome image as its mask to obtain transparency. I guess you'll have to preprocess your JPG in a similar way.

关于内嵌图像:

不要使用内嵌图像:使用内联图像意味着图像存储PDF的内容流中,而不是存储作为图像XObject的(这是在PDF存储图像的最佳方式)。内嵌图像只能被用于图像的尺寸为4 KB或更小的。 。较大的内嵌图像将在PDF 2.0被禁止

Don't use inline images: using inline images means that the images are stored in the content stream of the PDF as opposed to being stored as an Image XObject (which is the optimal way of storing images in a PDF). Inline images can only be used for images with a size of 4 KB or less. Larger inline images will be forbidden in PDF 2.0.

附加备注:

我想我在你的代码中发现了问题所在。您正在创建与页A4大小的文档:

I think I see a problem in your code. You are creating a document with page size A4:

Document doc = new Document()

A4是默认的大小,当你不传递参数给文件构造。之后,您尝试改变页面大小是这样的:

A4 is the default size when you don't pass a parameter to the Document constructor. Afterwards, you try changing the page size like this:

doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0));
doc.NewPage();



不过:因为你没有添加任何内容到第一页然而,新页()方法将被忽略,页面大小不会改变。您将仍然页面1尺寸A4上。

However: as you didn't add any content to the first page yet, the NewPage() method will be ignored and the page size will not be changed. You will still be on page 1 with size A4.

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(source);
using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None))
{
    using (Document doc = new Document(image))
    {
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
        {
            doc.Open();
            image.SetAbsolutePosition(0, 0);
            writer.DirectContent.AddImage(image); 
            doc.Close();
         }
     }
}

这篇关于与iTextSharp的图像转换为PDF格式保存剪辑路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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