通过iTextSharp将从右到左的语言文本添加到现有pdf文件的自定义位置

本文关键字:pdf 文件 位置 自定义 添加 iTextSharp 从右到左 语言 文本 通过 | 更新日期: 2023-09-27 17:52:12

嗨我想使用iTextSharp 5.0.0.0版本库,在现有pdf文件的顶部添加一个波斯语言(从右到左的语言(文本。

我使用这个代码:

public static void InsertText(string SourceFileName, string TargetFileName, string Text, int x, int y)
{
    PdfReader reader = new PdfReader(SourceFileName);            
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);  // open the writer 
    FileStream fs = new FileStream(TargetFileName, FileMode.Create, FileAccess.Write);
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    document.Open();

    PdfContentByte cb = writer.DirectContent;  // select the font properties
    FontFactory.RegisterDirectories();
    Font fTahoma = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 10, Font.NORMAL, BaseColor.RED);
    writer.SetMargins(0, 0, 0, 0);
    ColumnText ct = new ColumnText(writer.DirectContent);
    ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL;            
    Phrase p = new Phrase(Text, fTahoma);
    ct.SetText(p);
    ct.SetSimpleColumn(x, y, x + 350, y + 20);            
    ct.Go();           
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        PdfImportedPage page = writer.GetImportedPage(reader, i);
        cb.AddTemplate(page, 0, 0);  // close the streams and voilá the file should be changed :)                
        document.NewPage();
    }
    document.Close();
    fs.Close();
    writer.Close();
}

我呼叫InsertText("Source.pdf", "Target.pdf", "Persian message", 10, 800);我的source.pdf页面大小为:高:842,宽:595

不幸的是,我只收到了一半的target.pdf!另一半是隐藏的!现在的问题是:如何在现有pdf文件的顶部添加从右到左的语言文本?

通过iTextSharp将从右到左的语言文本添加到现有pdf文件的自定义位置

问题:导入的页面可能覆盖了文本。

解决方案:添加PdfImportedPage,然后添加文本。