缩小PDF页面的内容 [英] Shrinking the contents of a pdf page

查看:282
本文介绍了缩小PDF页面的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我目前的项目,我必须采取一个PDF和缩小的第一页的内容,不改变页面的整体大小,这样我可以没有它与任何预先存在的内容重叠的底部添加附加信息。有没有办法做到这一点使用iTextSharp的?我将不胜感激任何帮助,您可以给!

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!

推荐答案

您可以通过预先计算转换矩阵的影响缩小页面的内容页面内容数据流(多个),例如像这样的:

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(); 
}



(此代码借用 PdfStamper UnderContent和OverContent代)

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

提示: 缩小已被检索之前使用页面OverContent或UnderContent

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

<一个HREF =htt​​p://i.stack.imgur.com/9qJF7.jpg相对=nofollow>

从这个

请注意:该代码并不需要页面旋转考虑。如果您旋转页面,则应该扩展缩小相应。

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

这篇关于缩小PDF页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆