PDF

要在 .net 写出一个PDF的file,我们可以使用一个插件。

iTextSharp

这个插件据说是JAVA的iText,只是被翻译了过来。

参考 : http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm

基本用法 

复制代码
try
{
            
    FileStream fs = new FileStream(Server.MapPath("pdf") + "\\" + "First PDF document.pdf", FileMode.Create);
    Document document = new Document(PageSize.A4, 25, 25, 30, 30);            
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.AddAuthor("Micke Blomquist");
    document.AddCreator("Sample application using iTextSharp");
    document.AddKeywords("PDF tutorial education");
    document.AddSubject("Document subject - Describing the steps creating a PDF document");
    document.AddTitle("The document title - PDF creation using iTextSharp");
    BaseFont baseFont = BaseFont.CreateFont(
        "C:\\Windows\\Fonts\\simhei.ttf",
        BaseFont.IDENTITY_H,
        BaseFont.EMBEDDED);
    Font font = new Font(baseFont);
    //{"yahei", "C:\\WINDOWS\\FONTS\\msyh.ttf"},
    //{"fangsong", "C:\\WINDOWS\\FONTS\\simfang.ttf"},
    //{"heiti" ,"C:\\Windows\\Fonts\\simhei.ttf"},
    //{"kaiti" ,"C:\\Windows\\Fonts\\simkai.ttf"},
    //{"lishu" ,"C:\\Windows\\Fonts\\SIMLI.TTF"},
    //{"youyuan" ,"C:\\Windows\\Fonts\\SIMYOU.TTF"},
    //{"songti" ,"C:\\Windows\\Fonts\\simsun.ttc,0"},
    //{"xinsongti" ,"C:\\Windows\\Fonts\\simsun.ttc,1"}            
    document.Open();
    document.Add(new Paragraph("美丽的泡沫", font));
    document.Close();
    writer.Close();
    fs.Close();            
}
catch (Exception ex)
{
    throw;
}  
复制代码

要支持中文字的话,要特别处理,如图 

输出 : 

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment;Filename=xx.pdf");
Context.Response.WriteFile(Server.MapPath("pdf") + "\\" + "First PDF document.pdf"); 

更新 2016-03-14

在原有的 pdf 加字和图片 

refer : 

http://stackoverflow.com/questions/3992617/itextsharp-insert-text-to-an-existing-pdf

http://www.schiffhauer.com/adding-text-to-a-pdf-in-c-with-itextsharp/

 

复制代码
string oldFile = @"D:\Google Drive\learn\learn\TDD\AIA.pdf";
string newFile = @"D:\Google Drive\learn\learn\TDD\newAIA.pdf";

// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);

// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
FileStream inputImageStream = new FileStream(@"D:\Google Drive\learn\learn\TDD\Capture.PNG", FileMode.Open, FileAccess.Read, FileShare.Read);
Image image = Image.GetInstance(inputImageStream);

for (int i = 0; i < reader.NumberOfPages; i++)
{ 
    document.NewPage(); 
    PdfContentByte cb = writer.DirectContent;

    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.SetColorFill(BaseColor.DARK_GRAY);
    cb.SetFontAndSize(bf, 8);

    // write the text in the pdf content
    cb.BeginText();
    string text = "Some random blablablabla...";
    // put the alignment and coordinates here
    cb.ShowTextAligned(1, text, 520, 640, 0);
    cb.EndText();
    cb.BeginText();
    text = "Other random blabla...";
    // put the alignment and coordinates here
    cb.ShowTextAligned(2, text, 100, 200, 0);
    cb.EndText();
               
    image.SetAbsolutePosition(100, 100);
    cb.AddImage(image);
    // create the new page and add it to the pdf
    int pageNumber = i + 1;
    PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
    cb.AddTemplate(page, 0, 0);                
}

inputImageStream.Close();           
document.Close();
fs.Close();
writer.Close();
reader.Close();  
复制代码

 

posted @   兴杰  阅读(193)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 百万级群聊的设计实践
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
点击右上角即可分享
微信分享提示