2

Itextsharp と PdfWriter を使用して、pdf を受け取り、最初のページにテキストを印刷するプログラムがあります。現在、これは、テキストを入力しなければならなかった各 pdf に対して意図したとおりに機能しています。ただし、ソース pdf のレイアウトが横向きの場合、ライターは、pdf の最初のページにテキストを入力した後、レイアウトを縦向きに回転します。PDFにテキストが入力された後、デフォルトのレイアウトが縦向きに変更される理由に関するドキュメントが見つかりません。この回転により、元のレイアウトが横向きだったため、情報が右側で途切れてしまいます。

PdfStamper に関する他の回答を見てきましたが、既存のコードを操作して自分がしていることを操作するのに問題があります。C#、pdf 操作、および iTextSharp でのプログラミングにかなり慣れていません。強調表示可能な PDF 上のテキストの最終目標。

//Adds white invisible text to the pdf document that is highlightable
public static void stamp(string pdfName, string filePath, string textToStamp)
{
    //Make a Temporary copy of the original to manipulate
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    File.Copy(filePath, tempPath);
    //Make a new reader using the copied source file
    PdfReader reader = new PdfReader(tempPath);
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)))
        {
            document.Open();
            int numofpages = reader.NumberOfPages;
            for (int p = 1; p <= numofpages; p++)
            {
                //create the ContentByte to give the text a position on the first page
                PdfContentByte cb = writer.DirectContent;
                //Get the page to work with
                PdfImportedPage page = writer.GetImportedPage(reader, p);

                document.NewPage();
                cb.AddTemplate(page, 0, 20);
                var FontColour = new BaseColor(255, 255, 255);
                var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour);
                //Gets the first page and sends the text to the upper left corner
                if (p == 1)
                {
                    if (TextToStamp!= null)
                    {
                        document.Add(new Paragraph("Hello World", MyFont));
                    }
                }
                document.Close();
            }
        }
        reader.Close();
        File.Delete(tempPath);
    }

追加したいコメントや提案があれば、お気軽に! 感謝

4

2 に答える 2

2

Bruno が指摘したhttp://manning.com/lowagie2/samplechapter6.pdfを読むときは、特に 6.3.1 に注意してPdfStamperください。これは、特定のページの特定の位置にテキストを追加するために使用する方法を説明しています。また、2 種類の横向きページ (回転したページと幅 > 高さのページ) の違いも示しています。

完全なコード例はここにあります: http://www.itextpdf.com/examples/iia.php?id=117

于 2014-05-01T17:57:30.997 に答える
0

Bruno が投稿した情報を使用して、この解決策を思いつきました。これにより、レイアウトが何であれ、ページに情報をスタンプすることができ、少量のカスタマイズが可能になります。

public static void AddText(string pdfName, string filePath, string textToStamp, float? x = null, float? y = null)
{
    //x and y are used to position the text and allow multiple different templates to use the same method
    //Designate the Temporary source to be used
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    //Copy to file to the source path
    File.Copy(filePath, tempPath);
    PdfReader reader = new PdfReader(tempPath);
    iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(1);
    //Convert the pageHeight into a float
    int pageHeight = Convert.ToInt32(pageSize.Height);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(filePath, FileMode.Create));

    PdfContentByte canvas = stamper.GetOverContent(1);
    //Set a default value if x and y have no value 
    if (x.HasValue == false)
    {
        x = 35;
    }
    if (y.HasValue == false)
    {
        y = 30;
    }
    //choose the font type 
    var FontColour = new BaseColor(255, 255, 255);
    var MyFont = FontFactory.GetFont("Times New Roman", 10, FontColour);
    ColumnText.ShowTextAligned
                (canvas, Element.ALIGN_LEFT, new Phrase("Hello World", MyFont), (float)x, pageHeight - (float)y, 0);

    stamper.Close();
    reader.Close();
    File.Delete(tempPath);
}
于 2014-05-01T21:28:32.280 に答える