0

itextsharp ライブラリを使用して次のコードを使用し、テキストを PDF に追加しました (リンクITextSharp からコードを取得し、既存の PDF にテキストを挿入します

    Dim reader As New PdfReader(oldFile)
    Dim size As iTextSharp.text.Rectangle = reader.GetPageSizeWithRotation(1)
    Dim document As New iTextSharp.text.Document(size)

    ' open the writer
    Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
    document.Open()

    ' the pdf content
    Dim cb As PdfContentByte = writer.DirectContent

    ' select the font properties
    Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    'cb.SetColorFill(GrayColor.DARK_GRAY)
    cb.SetFontAndSize(bf, 8)
    cb.BeginText()
    Dim Text As String = "l"
    ' put the alignment and coordinates here
    cb.ShowTextAligned(2, Text, 84, 729, 0)
    cb.EndText()

    Dim bf1 As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    cb.SetFontAndSize(bf1, 8)
    cb.BeginText()
    Dim text1 As String = "Navaneeth A"
    cb.ShowTextAligned(1, text1, 65, 690, 0)
    cb.EndText()

    ' create the new page and add it to the pdf
    Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
    cb.AddTemplate(page, 0, 0)

    ' close the streams and voilá the file should be changed :)
    document.Close()
    fs.Close()
    writer.Close()
    reader.Close()

問題は、ソース pdf に約 5 ページあることですが、このコードによって生成された出力ファイルには最初のページしかありません。では、ソースファイルのすべてのページを出力ファイルに含めるにはどうすればよいですか? ソース pdf リンクはhttp://law.incometaxindia.gov.in/DITTaxmann/IncomeTaxRules/PDF/Ay-2012-2013/SAHAJ2012_14.pdfです。


 Dim reader As New PdfReader(oldFile)
    Using ms = New MemoryStream()
        Dim stamper As New PdfStamper(reader, ms)
        'Using stamper  'As New PdfStamper(reader, ms)
        stamper.RotateContents = False
        Dim canvas As PdfContentByte = stamper.GetOverContent(1)
        ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, New Phrase("Hello people!"), 36, 540, 0)
        'End Using

        Dim result As Byte() = ms.ToArray()
        File.WriteAllBytes(newFile, result)
        System.Diagnostics.Process.Start(newFile)
    End Using

次の変更を行いましたが、動作していません.resultファイルはわずか1kbのファイルでした。

4

1 に答える 1

1

残念ながら、使用してはならないサンプル コードが見つかりました。既存の PDF を操作するには、PdfStamperではなくを使用する必要がありますPdfWriter

コードは (すべてのページをコピーするように修正した後でも) インタラクティブな機能 (フォーム、その他の注釈など) をコピーしません。代わりに、iText in Action — 2nd Editionの第 6 章で説明されているWebified iTextSharp Example StampText.csに基づいてコードを作成する必要があります。

PdfReader reader = new PdfReader(resource);
using (var ms = new MemoryStream()) {
  using (PdfStamper stamper = new PdfStamper(reader, ms)) {
    stamper.RotateContents = false;
    PdfContentByte canvas = stamper.GetOverContent(1);
    ColumnText.ShowTextAligned(
      canvas,
      Element.ALIGN_LEFT, 
      new Phrase("Hello people!"), 
      36, 540, 0
    );
  }
  byte[] result = ms.ToArray();
}    

次のようにコードを変更すると、フォントと色も制御できます。

[...]
Font FONT = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, new GrayColor(0.75f));
PdfContentByte canvas = stamper.GetOverContent(1);
ColumnText.ShowTextAligned(
  canvas,
  Element.ALIGN_LEFT, 
  new Phrase("Hello people!", FONT), 
  36, 540, 0
);
[...]

PS何らかの理由で古いiTextSharpバージョンを使用する必要がある場合、詳細が異なる場合があります(あなたからの他の質問は、VB6を使用していることを暗示しているようです...)。それでも、使用に切り替える必要がありますPdfStamper

于 2013-03-05T08:06:48.520 に答える