【问题标题】:How do I insert a background image on all pages of a pdf-document? [closed]如何在 pdf 文档的所有页面上插入背景图像? [关闭]
【发布时间】:2011-08-06 02:48:51
【问题描述】:

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

【问题讨论】:

    标签: c# sharepoint-2010 pdf-generation itextsharp


    【解决方案1】:

    你可以试试这个;

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

    【讨论】:

    • 我无法使用响应。也许我需要连接一些命名空间?
    • 我认为你应该像这样使用 iTextSharp.text.pdf 添加使用 iTextSharp.text。我不确定..
    • 我想他只是想把它写到一个文件中。
    • 此方案只在首页添加背景图片。为每个页面添加背景怎么样?
    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2021-10-29
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多