在带有extsharp的现有pdf文件底部插入红色文本

问题描述 投票:-1回答:4

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

c# pdf itext styles
4个回答
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();
        }

0
投票

在不更改现有内容的情况下将内容添加到现有PDF文档中,有时称为stamping。示例包括添加页码,添加水印和添加运行头。

针对您的特定情况:

  • PdfDocument(读取输入的PDF)和PdfReader(写入输出的PDF)创建PdfWriter实例。 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();

-1
投票

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

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


-1
投票

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

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.