首页 > 解决方案 > 使用 IText 7 时看似膨胀的 PDF 大小

问题描述

我在 dotnet core 中使用 IText 7 来操作 PDF 文件。

完成该过程后,我发现自己拥有 14.2MB 的 5 页 PDF。

当我在 Adob​​e 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 的大小是应有的两倍 - 为什么 Adob​​e 会提示保存更改?

标签: c#itextitext7

解决方案


推荐阅读