itext将文本添加到pdf增加文件大小 [英] itext add text to pdf increase file size
问题描述
我有一个PDF文件,我想在每个页面上添加一个简单的数字。
I have a PDF file and I want to add a simple number on each page.
这是我的代码:
reader = new PdfReader(fileOut);
Document final = new Document(reader.GetPageSize(1));
PdfWriter w = PdfWriter.GetInstance(final, new FileStream(finalFile, FileMode.Create, FileAccess.Write));
w.SetFullCompression();
final.Open();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
final.NewPage();
PdfContentByte cb = w.DirectContent;
ControlNumberTimes(cb, "C"+i, 560, 725, 270, Element.ALIGN_LEFT);
cb.AddTemplate(w.GetImportedPage(reader, i), 0, 0);
}
final.Close();
reader.Close();
private static void ControlNumberTimes( PdfContentByte cb1, string control, int x, int y, int rotation, int allign )
{
cb1.BeginText();
cb1.SetColorFill(BaseColor.BLACK);
cb1.SetFontAndSize(BaseFont.CreateFont("C:\\windows\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 7.5f);
cb1.ShowTextAligned(allign, control, x, y, rotation);
cb1.EndText();
}
在添加此文本之前,PDF文件大小为3.6 Mb,大小为11 Mb。
我做错了什么?
Before adding this text, PDF file size is 3.6 Mb, after the size is 11 Mb. What I am doing wrong?
这是我现在的代码:
string finalFile = System.IO.Path.GetDirectoryName(fileOut) + "\\" +
System.IO.Path.GetFileNameWithoutExtension(fileOut) + "_num.pdf";
reader = new PdfReader(fileOut);
using (FileStream fs = new FileStream(finalFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
int pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_CENTER, new Phrase(
$"C{i}"), 560, 725, 0);
}
}
}
pdf文件我可以' t分享到机密信息。
The pdf file I can't share due of the confidential information.
推荐答案
这是完全错误的:
reader = new PdfReader(fileOut);
Document final = new Document(reader.GetPageSize(1));
PdfWriter w = PdfWriter.GetInstance(final, new FileStream(finalFile, FileMode.Create, FileAccess.Write));
w.SetFullCompression();
final.Open();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
final.NewPage();
PdfContentByte cb = w.DirectContent;
ControlNumberTimes(cb, "C"+i, 560, 725, 270, Element.ALIGN_LEFT);
cb.AddTemplate(w.GetImportedPage(reader, i), 0, 0);
}
final.Close();
说我正在使用文件复制文件
, PdfWriter
, PdfImportedPage
和 AddTemplate
,为什么我的文件大小增加?就像问我用锋利的刀刺伤自己的肚子,我为什么要流血?
Saying "I am copying a file using Document
, PdfWriter
, PdfImportedPage
and AddTemplate
, why does my file size increase?" is like asking "I've stabbed myself in the belly with a sharp knife, why do I bleed?"
如果你想在现有文件中添加页码,您必须按 PdfStamper 9b03-3f9aca1921a5 / samplechapter6.pdfrel =nofollow>我书中的第6章。
If you want to add page numbers to an existing document, you have to use PdfStamper
as explained in chapter 6 of my book.
你想操纵现有PDF ,更具体地说,您希望在页脚中添加页码。这是这样做的:
You want to manipulate an existing PDF, more specifically, you want to add page numbers in the footer. That is done like this:
PdfReader reader = new PdfReader(outputFile);
using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (PdfStamper stamper = new PdfStamper(reader, fs)) {
int PageCount = reader.NumberOfPages;
for (int i = 1; i <= PageCount; i++) {
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i, PageCount)), 560, 725, 270);
}
}
}
一些评论:
- 您正在使用绝对坐标(X = 560,Y = 725)。如官方文档中所述,最好使用相对于页面大小的坐标:如何相对于页面定位文本?
- 您正在使用
BeginText()
...EndText()
,但是你可能更容易使用ColumnText.ShowTextAligned()
。 - 你认为你使用的字体不是在您创建
BaseFont
时嵌入,如此BaseFont.CreateFont(C:\\windows \\Fonts \\ times。 ttf,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED)
。这不是真的。如上所述,使用BaseFont.IDENTITY_H
时,将忽略BaseFont.NOT_EMBEDDED
。请参阅为什么iText嵌入字体即使我指定不嵌入?如果需要小文件大小,我建议你不要嵌入字体。
- You are using absolute coordinates (X = 560, Y = 725). It would be better to use coordinates relative to the page size as described in the official documentation: How to position text relative to page?
- You are using
BeginText()
...EndText()
, but it might be easier for you to useColumnText.ShowTextAligned()
. - You think you are using a font that isn't embedded when you create a
BaseFont
like thisBaseFont.CreateFont("C:\\windows\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)
. That's not true. As documented,BaseFont.NOT_EMBEDDED
is ignored when usingBaseFont.IDENTITY_H
. See Why is iText embedding a font even when I specify not to embed? If a small file size is desired, I suggest you don't embed the font.
您的代码的主要问题是您没有以正确的方式操作文件。我认为这是因为您从一个写得不好的教程中复制/粘贴了您的代码。请不要复制那些不知道他们正在做什么的人的代码。
The main problem with your code, is the fact that you aren't manipulating a file the correct way. I think this is caused by the fact that you copy/pasted your code from a badly written tutorial. Please don't copy code from people who don't know what they're doing.
这篇关于itext将文本添加到pdf增加文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!