PYTHON用IRONPDFを使用する PythonでPDFファイルを編集する方法 Curtis Chau 更新日:6月 22, 2025 Download IronPDF pipダウンロード Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Iron Softwareは、IronPDF for Pythonライブラリを紹介します。これは、PythonでPDF編集作業を簡単に実行できるようにする革命的なソリューションです。 署名の挿入、HTMLフッターの追加、透かしの埋め込み、注釈のインクルード、PDFファイルの編集など、IronPDF for Pythonはあなたのためのツールです。 このライブラリは、コードの可読性を維持し、プログラムによるPDFの作成をサポートし、わかりやすいデバッグを容易にし、互換性のあるすべてのプラットフォームと環境にシームレスに展開します。 このチュートリアル記事では、例示的なPythonコードの例と包括的な説明を使用して、これらの広範な機能を探ります。 このガイドが終わるころには、IronPDF for Pythonをどのように使えばPDF編集ができるかを理解していることでしょう。 PythonでPDFファイルを編集する方法 1.Python PDF Library を pip インストーラを使ってインストールします。 2.Python PDF Libraryのライセンスキーを適用します。 3.PDF ドキュメントを読み込んで編集します。 4.分割、ページのコピー、その他のPDF操作など、さまざまなオプションを使用してPDF文書を編集します。 5.SaveAs関数を使用して変更したファイルを保存します。 ドキュメント構造の編集 ページの操作 IronPdfは特定の位置にページを追加したり、特定のページや範囲を抽出したり、PDFからページを削除したりするプロセスを簡素化します。 複雑なプロセスをすべて処理するため、これらのタスクを簡単に効率的に実行できます。 ページを追加する ページの内容、サイズ、位置を指定することで、PDF文書にページを追加することができます。 必要な変更を行った後、SaveAs関数を使って出力PDFファイルを保存することができます。 from ironpdf import * # Enable debugging and set log path Logger.EnableDebugging = True Logger.LogFilePath = "Custom.log" Logger.LoggingMode = Logger.LoggingModes.All # Load existing PDF and Render HTML as new PDF page pdf = PdfDocument("C:\\Users\\Administrator\\Downloads\\Documents\\sample.pdf") renderer = ChromePdfRenderer() coverPagePdf = renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>") # Prepend new page to existing PDF pdf.PrependPdf(coverPagePdf) # Save the updated PDF document pdf.SaveAs("report_with_cover.pdf") from ironpdf import * # Enable debugging and set log path Logger.EnableDebugging = True Logger.LogFilePath = "Custom.log" Logger.LoggingMode = Logger.LoggingModes.All # Load existing PDF and Render HTML as new PDF page pdf = PdfDocument("C:\\Users\\Administrator\\Downloads\\Documents\\sample.pdf") renderer = ChromePdfRenderer() coverPagePdf = renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>") # Prepend new page to existing PDF pdf.PrependPdf(coverPagePdf) # Save the updated PDF document pdf.SaveAs("report_with_cover.pdf") PYTHON コピーページ ページ番号とコピー先を指定することで、あるPDF文書から別の既存のPDFファイルにページをコピーすることができます。 さらに、コピーしたPDFページから新しいPDFファイルを作成するオプションもあります。 1つのPDFファイルから1ページまたは複数ページを選択してコピーすることも可能です。 from ironpdf import * # Load the PDF document pdf = PdfDocument("C:\\Users\\Administrator\\Downloads\\Documents\\sample.pdf") # Copy pages 3 to 5 and save them as a new document. pdf.CopyPages(2, 4).SaveAs("report_highlight.pdf") from ironpdf import * # Load the PDF document pdf = PdfDocument("C:\\Users\\Administrator\\Downloads\\Documents\\sample.pdf") # Copy pages 3 to 5 and save them as a new document. pdf.CopyPages(2, 4).SaveAs("report_highlight.pdf") PYTHON ページの削除 ページ番号を指定することで、入力PDFファイルからページを削除することができます。 from ironpdf import * # Load the PDF document pdf = PdfDocument("report.pdf") # Remove the last page from the PDF pdf.RemovePage(pdf.PageCount-1) # Save the updated PDF pdf.SaveAs("Report-Minus-1.pdf") from ironpdf import * # Load the PDF document pdf = PdfDocument("report.pdf") # Remove the last page from the PDF pdf.RemovePage(pdf.PageCount-1) # Save the updated PDF pdf.SaveAs("Report-Minus-1.pdf") PYTHON PDFのマージと分割 [PDF_A] [PDF_A] 1st Page [PDF_A] 2nd Page""" html_b = """ [PDF_B] [PDF_B] 1st Page [PDF_B] 2nd Page""" # Render each HTML content as PDF renderer = ChromePdfRenderer() pdfdoc_a = renderer.RenderHtmlAsPdf(html_a) pdfdoc_b = renderer.RenderHtmlAsPdf(html_b) # Merge the PDFs into a single document merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b) # Save the merged PDF merged.SaveAs("Merged.pdf") ``` #### PDFを分割してページを抽出する Hello Iron This is 1st Page This is 2nd Page This is 3rd Page""" # Render the HTML as a PDF document renderer = ChromePdfRenderer() pdf = renderer.RenderHtmlAsPdf(html) # Create a separate document for the first page page1doc = pdf.CopyPage(0) page1doc.SaveAs("Split1.pdf") # Create a separate document for pages 2 and 3 page23doc = pdf.CopyPages(1, 2) page23doc.SaveAs("Split2.pdf") ``` ## ドキュメント プロパティの編集 ### PDFメタデータを追加して使用するTesting 2048 bit digital security") # Step 2. Create a digital signature. signature = PdfSignature(r"certificates\IronSoftware.pfx", "123456") # Step 3. Optional signing options and a handwritten signature graphic. signature.SigningContact = "support@ironsoftware.com" signature.SigningLocation = "Chicago, USA" signature.SigningReason = "To show how to sign a PDF" # Step 4. Sign the PDF with the PdfSignature. doc.Sign(signature) # Step 5. The PDF is not signed until saved to file, stream, or byte array. doc.SaveAs("signed.pdf") ``` ### PDFの添付ファイル IronPDFはPDFドキュメントに添付ファイルを追加したり、削除したりすることをとても簡単にします。 つまり、IronPdfを使うことで、PDFに余計なファイルを入れたり、必要に応じて取り出したりすることができるのです。 ```python from ironpdf import * # Instantiate the Renderer and create a PdfDocument from HTML renderer = ChromePdfRenderer() my_pdf = renderer.RenderHtmlFileAsPdf("my-content.html") # Open the PDF document to be attached pdf = PdfDocument.FromFile("new_sample.pdf") # Add an attachment with a name and a byte array attachment1 = my_pdf.Attachments.AddAttachment("attachment_1", pdf.BinaryData) # Remove an attachment my_pdf.Attachments.RemoveAttachment(attachment1) # Save the PDF with attachments my_pdf.SaveAs("my-content.pdf") ``` ### PDFを圧縮する IronPDFはPDFを圧縮してファイルサイズを小さくする機能を持っています。一つの方法は、`CompressImages`メソッドを使ってPDFドキュメントに埋め込まれた画像のサイズを小さくすることです。 画質については、JPEG画像の場合、100%の画質であればほぼ画質の劣化はありませんが、1%の画質では非常に画質の悪い出力になってしまいます。 一般的に、90%以上の画質が高品質とみなされます。 中品質の画像は80%~90%、低品質の画像は70%~80%です。 70%を下回ると画質が著しく劣化しますが、PDF文書全体のファイルサイズを大幅に縮小することができます。 ニーズに合った品質とファイルサイズの適切なバランスを見つけるために、さまざまな品質の割合を試してみることをお勧めします。 画像によっては、他の画像よりも鮮明さが損なわれる場合があるため、縮小後の画質の顕著な低下は、扱う画像の種類によって異なることに留意してください。 ```python from ironpdf import * # Load the PDF document pdf = PdfDocument("document.pdf") # Compress images within the PDF (quality between 1-100) pdf.CompressImages(60) pdf.SaveAs("document_compressed.pdf") # Compress images with scaling image resolution pdf.CompressImages(90, True) pdf.SaveAs("document_scaled_compressed.pdf") ``` ## PDFコンテンツの編集 ### ヘッダーとフッターの追加 IronPDFを使えばPDFドキュメントにヘッダーとフッターを追加するのは簡単です。 このソフトウェアは、2つの異なるタイプの`HeaderFooters`を提供します:`TextHeaderFooter`と`HtmlHeaderFooter`です。 `TextHeaderFooter`は、テキストのみを含み、「{page} of {total-pages}」のようなマージフィールドを組み込む必要があるかもしれないヘッダーとフッターに最適です。 一方、`HtmlHeaderFooter`は、より高度なオプションで、あらゆるHTMLコンテンツを扱い、きれいにフォーマットすることができ、より複雑なヘッダーやフッターに適しています。 #### HTML ヘッダーとフッター IronPDF for Pythonを使うと、`HtmlHeaderFooter`機能を使ってHTMLからPDFドキュメントのHTMLヘッダーやフッターを作成することができます。 つまり、HTMLを使ってヘッダーやフッターをデザインすることができ、IronPDF for PythonはそれをPDFに合わせて完璧に変換し、細部まで正確に仕上げます。 ヘッダーやフッターのHTMLデザインがあれば、IronPDF for Pythonはそれを正確にPDFドキュメントに適用することができます。 ```python from ironpdf import * import os # Instantiate Renderer renderer = ChromePdfRenderer() # Build a footer using HTML to style the text renderer.RenderingOptions.HtmlFooter = HtmlHeaderFooter() renderer.RenderingOptions.HtmlFooter.MaxHeight = 15 # millimeters renderer.RenderingOptions.HtmlFooter.HtmlFragment = "{page} of {total-pages}" renderer.RenderingOptions.HtmlFooter.DrawDividerLine = True # Ensure sufficient bottom margin renderer.RenderingOptions.MarginBottom = 25 # mm # Build a header using an image asset renderer.RenderingOptions.HtmlHeader = HtmlHeaderFooter() renderer.RenderingOptions.HtmlHeader.MaxHeight = 20 # millimeters renderer.RenderingOptions.HtmlHeader.HtmlFragment = "" renderer.RenderingOptions.HtmlHeader.BaseUrl = os.path.abspath("C:/Users/lyty1/OneDrive/Documents/IronPdfPythonNew") # Ensure sufficient top margin renderer.RenderingOptions.MarginTop = 25 # mm ``` #### テキストヘッダーとフッター ```python from ironpdf import * # Initiate PDF Renderer renderer = ChromePdfRenderer() # Add a text header to every page renderer.RenderingOptions.FirstPageNumber = 1 # use 2 if a cover page will be appended renderer.RenderingOptions.TextHeader.DrawDividerLine = True renderer.RenderingOptions.TextHeader.CenterText = "{url}" renderer.RenderingOptions.TextHeader.Font = FontTypes.Helvetica renderer.RenderingOptions.TextHeader.FontSize = 12 renderer.RenderingOptions.MarginTop = 25 # create 25mm space for header # Add a text footer to every page renderer.RenderingOptions.TextFooter.DrawDividerLine = True renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial renderer.RenderingOptions.TextFooter.FontSize = 10 renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}" renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}" renderer.RenderingOptions.MarginBottom = 25 # create 25mm space for footer ``` ### 概要とブックマーク ブックマーク」とも呼ばれるアウトラインは、PDF文書内の重要なページにすばやく移動するためのツールです。 Adobe Acrobat Readerを使用している場合、アプリの左サイドバーでこれらのブックマーク(階層構造で整理可能)を見ることができます。 IronPDF for Python ライブラリにより、ブックマークの操作がさらに簡単になりました。 PDF文書から既存のしおりを自動的に取り込むことができます。 さらに、IronPDFを使ってしおりを追加したり、編集したり、グループで並べたりすることができます。 ```python from ironpdf import * # Load an existing PDF document. pdf = PdfDocument.FromFile("existing.pdf") # Add bookmarks to the PDF pdf.Bookmarks.AddBookMarkAtEnd("Author's Note", 2) pdf.Bookmarks.AddBookMarkAtEnd("Table of Contents", 3) # Create a new bookmark and add nested bookmarks summaryBookmark = pdf.Bookmarks.AddBookMarkAtEnd("Summary", 17) summaryBookmark.Children.AddBookMarkAtStart("Conclusion", 18) # Add additional bookmarks pdf.Bookmarks.AddBookMarkAtEnd("References", 20) # Save the PDF with new bookmarks pdf.SaveAs("existing.pdf") ``` ### 注釈の追加と編集 IronPDF for Pythonを使ってPDFドキュメントに注釈を追加、編集することができます。 注釈は、テキストのハイライト、コメントの追加、リンクの作成に使用できます。 既存の注釈を編集することもできます。 ```python from ironpdf import * # Load an existing PDF pdf = PdfDocument("existing.pdf") # Create a TextAnnotation object annotation = TextAnnotation() annotation.Title = "This is the title" annotation.Subject = "This is a subject" annotation.Contents = "This is the comment content..." annotation.Icon = TextAnnotation.AnnotationIcon.Help annotation.Opacity = 0.9 annotation.Printable = False annotation.Hidden = False annotation.OpenByDefault = True annotation.ReadOnly = False annotation.Rotateable = True # Add the annotation to a specific page and location within the PDF pdf.AddTextAnnotation(annotation, 1, 150, 250) # Save the PDF with annotations pdf.SaveAs("existing.pdf") ``` ### 背景と前景を追加するスタンプをPDFに適用する") stamper.HorizontalAlignment = HorizontalAlignment.Center stamper.VerticalAlignment = VerticalAlignment.Bottom stamper.IsStampBehindContent = False stamper.Opacity = 30 # Load existing PDF and apply the stamp pdf = PdfDocument.FromFile("Sample.pdf") pdf.ApplyStamp(stamper).SaveAs("stampedimage.pdf") ``` ### PDFに透かしを追加するSAMPLE", 30, VerticalAlignment.Middle, HorizontalAlignment.Center) # Save the watermarked PDF pdf.SaveAs("Watermarked.pdf") ``` ## PDFでフォームを使う Editable PDF Form First name: Last name: Please specify your gender: Female Male Non-Binary / Other Please select all medical conditions that apply: Hypertension Heart Disease Stroke Diabetes Kidney Disease