【问题标题】:Insert text in Red Color at the bottom of existing pdf file with itextsharp使用 itextsharp 在现有 pdf 文件的底部插入红色文本
【发布时间】:2020-01-17 06:49:49
【问题描述】:

我有一个现有的 PDF 文件,我想在 PDF 文件的底部插入一个红色的文本,但现有的 pdf 文件颜色必须保持不变。

【问题讨论】:

  • 你尝试了什么,是什么阻止了你?

标签: c# pdf itext styles


【解决方案1】:

谢谢@mkl 下面的代码,我使用的是特定于 Stamp 的代码。

 public static void ManipulatePdf(string src, string dest)
        {
            src = @"C:\CCPR Temp\TempFiles\PayStub_000106488_12282019_20200117112403.pdf";
            dest = @"C:\CCPR Temp\TempFiles\PayStub_WithStamper.pdf";
            PdfReader reader = new PdfReader(src);
            PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); // create?
            int numberOfPages = reader.NumberOfPages;
            Rectangle pagesize;
            for (int i = 1; i <= numberOfPages; i++)
            {
                PdfContentByte under = stamper.GetUnderContent(i);
                pagesize = reader.GetPageSize(i);
                float x =40;// (pagesize.Left + pagesize.Right) / 2;
                float y = pagesize.Top/4;// (pagesize.Bottom + pagesize.Top) / 2;
                PdfGState gs = new PdfGState();
                gs.FillOpacity = 1.0f;
                under.SaveState();
                under.SetGState(gs);
                under.SetRGBColorFill(255,0,0); 
                ColumnText.ShowTextAligned(under, Element.ALIGN_BOTTOM,
                    new Phrase("Watermark", new Font(Font.FontFamily.HELVETICA, 20)),
                    x, y, 1);
                under.RestoreState();
            }
            stamper.Close();
            reader.Close();
        }

【讨论】:

    【解决方案2】:

    在不更改现有内容的情况下向现有 PDF 文档添加内容,有时称为。示例包括添加页码、添加水印和添加跑头。

    针对您的具体情况:

    • PdfReader(读取输入PDF)和PdfWriter(写入输出PDF)创建PdfDocument实例。 PdfDocument 实例将处于 stamping 模式。
    • 在带有页脚文本的Paragraph 上设置红色文本颜色。
    • 根据页面大小使用ShowTextAligned 便捷方法定位文本。

    您可能还需要考虑页面旋转。

    PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter("output.pdf"));
    Document doc = new Document(pdfDoc);
    
    Paragraph footer = new Paragraph("This is a footer").SetFontColor(ColorConstants.RED);
    
    for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) 
    {
        Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
        float x = pageSize.GetLeft() + pageSize.GetWidth() / 2;
        float y = pageSize.GetBottom() + 20;
        doc.ShowTextAligned(footer, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
    }
    
    doc.Close();
    

    【讨论】:

      【解决方案3】:

      这将设置段落的字体。我不确定职位。

                  BaseFont btnRedFooter = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                  iTextSharp.text.Font fntRedFooter = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.TIMES_ROMAN.ToString(), 16,
                  iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED);
      

      pProtocoll.Add(new Paragraph("页脚文本", fntRedFooter));

      【讨论】:

        【解决方案4】:

        我使用了以下步骤,最后得到了所需的输出。我正在为字体颜色而苦苦挣扎,以应用于新添加的文本。

        public static void StampPdfFile(string oldFile, string newFile)
        {
            // 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();
        
            // the pdf content
            PdfContentByte cb = writer.DirectContent;
        
            // select the font properties
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        
            cb.SetFontAndSize(bf, 8);
        
            // write the text in the pdf content
        
            cb.BeginText();
            string text = $"Voided On - {DateTime.Now.Date.ToString("MM/dd/yyyy")}";
            // put the alignment and coordinates here
            cb.SetColorFill(BaseColor.RED); //Give Red color to the newly added Text only
            cb.ShowTextAligned(2, text, 120, 250, 0);
            cb.SetColorFill(BaseColor.BLACK);  //Give Red color to the exisitng file content only
            cb.EndText();
        
            // create the new page and add it to the pdf
            PdfImportedPage page = writer.GetImportedPage(reader, 1);
            cb.AddTemplate(page, 0, 0);
        
            document.Close();
            fs.Close();
            writer.Close();
            reader.Close();
        }
        

        【讨论】:

        • 你应该在这里使用PdfStamper。您的代码会删除交互式和文档级元素。
        猜你喜欢
        • 2017-05-10
        • 2011-04-28
        • 1970-01-01
        • 2016-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多