Shrinking the contents of a pdf page

主宰稳场 提交于 2019-12-04 07:14:27

问题


For my current project, I am required to take a pdf and shrink the contents of the first page without changing the overall size of the page so that I can add additional information to the bottom without it overlapping with any pre-existing content. Is there a way to do this using iTextSharp? I would appreciate any help you can give!


回答1:


You can shrink the content of a page by prepending a transformation matrix to that effect to the page content stream(s), e.g. like this:

public void shrink(PdfStamper stamper, int page, float factor)
{
    Rectangle crop = stamper.Reader.GetCropBox(page);
    float diffX = crop.Right * (1 - factor);
    float diffY = crop.Top * (1 - factor);
    PdfDictionary pageN = stamper.Reader.GetPageN(page);
    stamper.MarkUsed(pageN);
    PdfArray ar = null;
    PdfObject content = PdfReader.GetPdfObject(pageN.Get(PdfName.CONTENTS), pageN);
    if (content == null)
        return;
    if (content.IsArray())
    {
        ar = new PdfArray((PdfArray)content);
        pageN.Put(PdfName.CONTENTS, ar);
    }
    else if (content.IsStream())
    {
        ar = new PdfArray();
        ar.Add(pageN.Get(PdfName.CONTENTS));
        pageN.Put(PdfName.CONTENTS, ar);
    }
    else
        return;
    ByteBuffer out_p = new ByteBuffer();
    out_p.Append(factor).Append(" 0 0 ").Append(factor).Append(' ').Append(diffX).Append(' ').Append(diffY).Append(" cm ");
    PdfStream stream = new PdfStream(out_p.ToByteArray());
    ar.AddFirst(stamper.Writer.AddToBody(stream).IndirectReference);
    out_p.Reset(); 
}

(This code borrows from the PdfStamper UnderContent and OverContent generation.)

Hint: shrink has to be used before retrieving the OverContent or UnderContent of the page.

You can use it like this:

[Test]
public void ShrinkFirstPage()
{
    string origFile = ...;
    string resultFile = ...;

    using (PdfReader reader = new PdfReader(origFile))
    using (FileStream output = new FileStream(resultFile, FileMode.Create, FileAccess.Write))
    using (PdfStamper stamper = new PdfStamper(reader, output))
    {
        int page = 1;
        float factor = .9f;
        shrink(stamper, page, factor);

        Rectangle box = reader.GetCropBox(page);
        box.Top = box.Top - factor * box.Height;

        PdfContentByte cb = stamper.GetOverContent(page);
        cb.SetColorFill(BaseColor.YELLOW);
        cb.SetColorStroke(BaseColor.RED);
        cb.Rectangle(box.Left, box.Bottom, box.Width, box.Height);
        cb.FillStroke();
        cb.SetColorFill(BaseColor.BLACK);

        ColumnText ct = new ColumnText(cb);

        ct.AddElement(new Paragraph("This is some text added to the front page of the front page of this document."));

        ct.SetSimpleColumn(box);
        ct.Go();
    }
}

From this original

you get

and from this

you get

Beware: The code does not take page rotation into account. If you have rotated pages, you should extend shrink accordingly.



来源:https://stackoverflow.com/questions/34406925/shrinking-the-contents-of-a-pdf-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!