c# - 使用 IText 7 时看似膨胀的 PDF 大小
问题描述
我在 dotnet core 中使用 IText 7 来操作 PDF 文件。
完成该过程后,我发现自己拥有 14.2MB 的 5 页 PDF。
当我在 Adobe Reader 中打开生成的 PDF 时,系统会在关闭 Reader 时提示我保存。如果我选择是,它会将 pdf 缩小 50%。
以下是相关代码:
// ASDLine Model
public class ASDLine
{
public long ARInvoiceHeaderKey { get; set; }
public string BillingCode { get; set; }
public string Description { get; set; }
public decimal Amount { get; set; }
public bool? MatchProfile { get; set; }
}
public AuditSummaryDocument Create()
{
// Load in the template
MemoryStream outputDoc = new MemoryStream();
// Attempt to shrink the PDF..(didn't make any difference)
var w = new PdfWriter(outputDoc);
var wb = w.IsFullCompression();
w.SetCompressionLevel(9);
w.SetSmartMode(true);
PdfDocument pdfDoc = new PdfDocument(new PdfReader("mytemplate.pdf"), w);
font = PdfFontFactory.CreateRegisteredFont("Calibri");
Document doc = new Document(pdfDoc);
PdfAcroForm pdfAcroForm = PdfAcroForm.GetAcroForm(pdfDoc, true);
var fields = pdfAcroForm.GetFormFields();
//header:
fields["generated"].SetValue($"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}");
// .... lots of fields
pdfAcroForm.FlattenFields();
// Add lines
Table table = GenerateTable();
table.SetRelativePosition(0, 510, 0, 0);
doc.Add(table);
// attempt to shrink PDF
pdfDoc.SetFlushUnusedObjects(true);
pdfDoc.Close();
//outputDoc.Position = 0;
var r = new AuditSummaryDocument
{
Data = outputDoc.ToArray()
};
return r;
}
该表如下所示:
private Table GenerateTable()
{
// Example list of ASDLine
var list = new List<ASDLine>();
list.Add(new ASDLine { Amount = 20, ARInvoiceHeaderKey = 1, BillingCode = "ABC123", Description = "A billing code 1", MatchProfile = null });
list.Add(new ASDLine { Amount = 40, ARInvoiceHeaderKey = 1, BillingCode = "ABC456", Description = "A billing code 2", MatchProfile = true });
list.Add(new ASDLine { Amount = 60, ARInvoiceHeaderKey = 1, BillingCode = "ABC789", Description = "A billing code 3", MatchProfile = false });
list.Add(new ASDLine { Amount = 80, ARInvoiceHeaderKey = 1, BillingCode = "ABC987", Description = "A billing code 4", MatchProfile = true });
Table table = new Table(new UnitValue[] { new UnitValue(UnitValue.POINT, 80)
,new UnitValue(UnitValue.POINT, 370)
,new UnitValue(UnitValue.POINT, 80)
,new UnitValue(UnitValue.POINT, 80)});
table.SetWidth(new UnitValue(UnitValue.PERCENT, 102));
Style hs = new Style();
hs.SetFont(font);
hs.SetFontSize(10);
hs.SetBold();
hs.SetTextAlignment(TextAlignment.CENTER);
Style cs = new Style();
cs.SetFont(font);
cs.SetFontSize(10);
table.AddHeaderCell(new Cell().AddStyle(hs).Add(new Paragraph("Billing Code")));
table.AddHeaderCell(new Cell().AddStyle(hs).Add(new Paragraph("Description")));
table.AddHeaderCell(new Cell().AddStyle(hs).Add(new Paragraph("Amount")));
table.AddHeaderCell(new Cell().AddStyle(hs).Add(new Paragraph("Match Profile")));
list.ForEach(line =>
{
table.AddCell(new Cell().AddStyle(cs).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(line.BillingCode)));
table.AddCell(new Cell().AddStyle(cs).SetTextAlignment(TextAlignment.LEFT).Add(new Paragraph(line.Description)));
table.AddCell(new Cell().AddStyle(cs).SetTextAlignment(TextAlignment.RIGHT).Add(new Paragraph(line.Amount.ToString("C"))));
table.AddCell(new Cell().AddStyle(cs).SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(line.MatchProfile.HasValue ? line.MatchProfile.Value ? "Yes" : "No" : "-")));
});
return table;
}
我的代码做错了什么导致此 PDF 的大小是应有的两倍 - 为什么 Adobe 会提示保存更改?
解决方案
推荐阅读
- sql - 如何从 postgres 中的 jsonb 列获取数据?
- java - 关于 java 中的 equals() 方法,下列哪项是正确的?
- python - 有没有办法分段读取 JSON 文件?
- c# - 带有外部 PostGres 的 Dotnet EF 迁移失败,因为 db 尚未准备好
- deepsecurity - Deepsecurity 的管理控制台
- javascript - 错误:函数在 Cloud Functions 中返回未定义、预期的 Promise 或值
- tensorflow - MatMul中的Keras LSTM Dtype不一样
- text - 您可以在 RTF 文件中超链接文档吗?
- r - 用于在 Shiny 中过滤表的 Null SelectInput
- python - 如何将flask-python中的小数点后2位浮点数传递给html?