17

私はしばらくの間、この問題を解決しようとしてきましたが、役に立ちませんでした。改行を入れようとしている iTextSharp にテキストがあります。\nエスケープ文字 ,を使用してみましたが、成功Environment.NewLinedocument.Add(new Phrase(Environment.NewLine))ませんでした。これを行う方法はありますか?これが私がそれをやろうとしている私のコードの一部です( でコメントされた行に注意してください//Doesn't work):

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

//Configure the content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 10);

//Write the text here
cb.BeginText();
text = "F\n";//Doesn’t work
document.Add(new Phrase(Environment.NewLine));//Doesn’t work
text += "o\n";
text += Environment.NewLine;//Doesn’t work
text += "o\n";
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 85, 311, 0);
cb.EndText();

//Create the new page
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

//Close all streams
document.Close();
fs.Close();
writer.Close();
reader.Close();

助言がありますか?

編集 1:

まだ動作していませんdocument.Add(new Paragraph("\n"));。私はそれを間違っていましたか?

cb.BeginText();
text = "F";
document.Add(new Paragraph("\n"));
text += "o";
document.Add(new Paragraph("\n"));
text += "o";
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 85, 311, 0);
cb.EndText();
4

10 に答える 10

25

iTextSharp でテキストを操作するには、主に 2 つの方法があります。1ParagraphPhrasePdfContentByte. 抽象化はマージン、改行、間隔などを処理しますが、手動ルートはすべてあなた次第です。あなたがしていることである2つを実際に混在させることはできません。詳細な制御が特に必要でない限り、手動ルートの代わりに抽象化を使用することを強くお勧めします。以下は、両方をオフにしたサンプルです。

しかし、あなたの質問に具体的に答えるために、生のPDFコマンド(使用している)は、特定のx,y座標で左から右にテキストを描画し、「改行」または「改行」の概念をサポートしていません。これを行うには、現在のテキスト カーソルを手動で新しい行に移動する必要があります。そのサンプルについては、以下のコードを参照してください。

        string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
        using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
            using (Document doc = new Document(PageSize.LETTER)) {
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                    doc.Open();

                    //This creates two lines of text using the iTextSharp abstractions
                    doc.Add(new Paragraph("This is Paragraph 1"));
                    doc.Add(new Paragraph("This is Paragraph 2"));

                    //This does the same as above but line spacing needs to be calculated manually
                    PdfContentByte cb = writer.DirectContent;
                    cb.SaveState();
                    cb.SetColorFill(BaseColor.BLACK);
                    cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12f);
                    cb.BeginText();
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb1", 20, 311, 0);
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is cb2", 20, 291, 0);//Just guessing that line two should be 20px down, will actually depend on the font
                    cb.EndText();
                    cb.RestoreState();
                    doc.Close();
                }
            }
        }
于 2012-05-25T14:51:49.973 に答える
16

次のようなことを試してください:

document.Add(new Chunk("\n"));
于 2013-12-17T07:29:25.490 に答える
8

document.Add(new Paragraph(" "));私にとってはうまくいきます。このParagraphステートメントは自動的に改行を追加することに注意してください。あなたがしなければならないのは、レンダリングするものを与えることだけです。この場合、スペースで問題ありません。

于 2013-02-08T21:42:37.333 に答える
3
document.Add(new Paragraph("\n"));

編集:

cb.BeginText();
string text = "";
text = "F\n";           
text += "o\n";            
text += "o";
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 85, 311, 0);
cb.EndText();


//Create the new page
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

Paragraph p = new Paragraph(text);
document.Add(p);

//Close all streams
document.Close();
fs.Close();
writer.Close();
reader.Close();
于 2012-05-25T14:29:10.020 に答える
0

このツールを試しているだけで、新しい行を追加するために '\r\n' を追加したところ、うまくいきました。以下のように。

String content01 = "Nulla condimentum dui lobortis risus viverra, eu pellentesque sem blandit.\r\nUt congue turpis quis sapien mollis, vitae rutrum mi consectetur. Integer maximus nisl sed tellus pharetra pharetra.";
Phrase contentPhrase = new Phrase(content01);
Paragraph contentPr = new Paragraph();
contentPr.Add(contentPhrase);

次に、Paragraph contentPtr を Document インスタンスに追加しました。

于 2015-04-10T14:12:43.553 に答える