Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用itextsharp.NETMVC以横向模式在现有pdf中编写内容?_Asp.net_Asp.net Mvc_Pdf_Itext - Fatal编程技术网

Asp.net 使用itextsharp.NETMVC以横向模式在现有pdf中编写内容?

Asp.net 使用itextsharp.NETMVC以横向模式在现有pdf中编写内容?,asp.net,asp.net-mvc,pdf,itext,Asp.net,Asp.net Mvc,Pdf,Itext,我想在现有的pdf中写一些东西。 到目前为止,我已完成=> public void certificate() { //get user info using UserId from database //UserDetail UserDetail = db.UserDetails.Where(x => x.UserId == UserId).FirstOrDefault(); string oldFile = Server.MapPath("~/Content/i

我想在现有的pdf中写一些东西。 到目前为止,我已完成=>

public void certificate()
{
    //get user info using UserId from database
    //UserDetail UserDetail = db.UserDetails.Where(x => x.UserId == UserId).FirstOrDefault();
    string oldFile = Server.MapPath("~/Content/img/tsms/Certificate/Certificate-of-Completion-Award-Template-Blue.pdf");
    string newFile = Server.MapPath("~/Content/img/tsms/Certificate/newFile.pdf");

    // open the reader
    PdfReader reader = new PdfReader(oldFile);
    Document document = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
    document.SetPageSize(PageSize.A4);

    // open the writer
    FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.Open();

    // the pdf content
    PdfContentByte cb = writer.DirectContent;

    // select the font properties
    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();

    // write the text in the pdf content
    cb.BeginText();
    text = "Other random blabla...";
    // put the alignment and coordinates here
    cb.ShowTextAligned(2, text, 100, 200, 0);
    cb.EndText();

    // create the new page and add it to the pdf
    PdfImportedPage page = writer.GetImportedPage(reader, 1);
    cb.AddTemplate(page, 0, 0);

    // close the streams and voilá the file should be changed :)
    document.Close();
    fs.Close();
    writer.Close();
    reader.Close();
}
这是有效的。我的问题是:我提供的
旧文档
处于横向模式。我希望新文档处于横向模式。但这为我提供了肖像模式下的pdf。所以我试着

更新1=>

document.SetPageSize(PageSize.A4.Rotate());
这也不起作用

此外,新的文本我加入到旧的pdf显示在旧的页面下

意味着-(下面的pdf快照)。。。。
我的旧PDF和新PDF(已创建)-

在回答你的另一个问题时,我解释了为什么页面会出现旋转,以及如何抵消旋转

不过,在这里,很明显您的任务是填写该证书。此任务不应通过
PdfReader
/
PdfWriter
对(用于使用其他文档的一个或两个页面的副本创建新文档)实现,而应通过
PdfReader
/
PdfStamper
对(用于操作现有PDF,例如表单填写)

使用该技术,您的代码可能如下所示:

using (PdfReader reader = new PdfReader(oldFile))
using (FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
    PdfContentByte cb = stamper.GetOverContent(1);

    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, 640, 520, 0);
    cb.EndText();

    // write the text in the pdf content
    cb.BeginText();
    text = "Other random blabla...";
    // put the alignment and coordinates here
    cb.ShowTextAligned(2, text, 100, 200, 0);
    cb.EndText();
}
导致


我建议您使用基于
PdfStamper
的解决方案,而不是基于普通
PdfWriter
的解决方案。这样可以更自然地处理页面旋转。