iTextSharp: Why when adding a image to a pdf page

2020-05-03 11:10发布

问题:

I use Helvetica font and 14 px size for text. The problem is that if a page does not have any image on it the text is very clear, but in a page with at least 1 image the text is getting a little bold. You can see what I mean in images below:

* Without image on page

* With image on page

The correct font is the one that appear in picture #1. How to make all pages have the same font even if the page contains an image or not?

Thanks.

Sample code:

Document document = new Document(PageSize.LETTER);
document.SetMargins(docMargin, docMargin, docMargin, 25);
writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
document.Open();

Font defaultFont = FontFactory.GetFont("Helvetica", 7.8, Font.NORMAL, new Color(75, 75, 75));
document.Add(new Paragraph("Lorem ipsum lorem ipsum lorem ipsum", defaultFont));
document.Add(Chunk.NEWLINE);
Image img = Image.GetInstance("my png image path");
document.Add(img);

document.Close();

回答1:

I was finally able to reproduce your problem. The first PNG that I tested with which didn't reproduce your problem I created from Photoshop and used the Save For Web command. The second PNG that I tested and was able to reproduce your problem I created from MSPAINT.EXE. I tried various combinations within Save For Web and none of them have the same problem as Paint.

According to this thread from the official iText mailing list it appears to be something about the color profile of the image.

What are you seeing is the impact of newly placed transparency into a PDF that had not previously contained it, when consideration isn't given for the blending colorspace of the final output document.

You have an RGB document that upon adding transparency is forced into CMYK due to lack of explicit blending space. If you were to specify RGB as your explicit blending space at the same time you added your transparency, all would be well.

One thing they recommend is setting the following property on your PdfWriter before adding anything:

writer.RgbTransparencyBlending = true;

When I do it I still see a very minor shift but no where near as pronounced as without it.



回答2:

This isn't an answer, I just need to be able to post code.

I'm unable to reproduce your results but if I were to guess it has something to do with your PDF renderer. You can confirm this by zooming in on the text, does it look the same when zoomed in? If so, that's your renderer trying to apply visual hints to a print document. If not, can you post a simplified version of your code that does this? Does this do this for all images or just one specific one? How are you creating your text, with Paragraphs, Tables, HTML parsing or something else? What version of iTextSharp are you using?

Below is a full working WinForms C# 2010 targeting iTextSharp 5.1.2.0 that creates a two page PDF. The first page has just text and the second page has text followed by an image loaded from the desktop. On my machine, using Adobe Acrobat Pro 9.1.3 I don't see any difference in fonts when I view it on screen.

using System;
using System.IO;
using System.Windows.Forms;
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) {
            string pdfFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            string imgFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.png");

            using (FileStream fs = new FileStream(pdfFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();

                        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
                        iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 14);

                        doc.NewPage();
                        doc.Add(new Paragraph("This is a test", f));

                        doc.NewPage();
                        doc.Add(new Paragraph("This is a test", f));
                        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgFile);
                        img.ScaleAbsolute(100, 100);
                        doc.Add(img);


                        doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}