如何在pdf文档的所有页面上插入背景图像? [英] How do I insert a background image on all pages of a pdf-document?

查看:193
本文介绍了如何在pdf文档的所有页面上插入背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要C#中的示例代码才能在完成的pdf文档的所有页面上插入背景图像。我正在使用iTextSharp库。

I need sample code in C# to insert the background image on all pages of the finished pdf-document. I'm using iTextSharp library.

推荐答案

你可以试试这个;

void makePDF()
{
    Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition", "attachment;filename=test.pdf");

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    string imageFilePath = Server.MapPath(".") + "/images/test.jpg";

    iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

    // Page site and margin left, right, top, bottom is defined
     Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

    //Resize image depend upon your need
    //For give the size to image
     jpg.ScaleToFit(3000, 770);

    //If you want to choose image as background then,

    jpg.Alignment = iTextSharp.text.Image.UNDERLYING;

    //If you want to give absolute/specified fix position to image.
    jpg.SetAbsolutePosition(7, 69);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    pdfDoc.NewPage();

    Paragraph paragraph = new Paragraph("this is the testing text for demonstrate the image is in background \n\n\n this is the testing text for demonstrate the image is in background");

    pdfDoc.Add(jpg);

    pdfDoc.Add(paragraph);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End();
 }

这篇关于如何在pdf文档的所有页面上插入背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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