PDF ドキュメントの添付ファイルは、PDF ファイル自体に埋め込まれたファイルや追加データを指します。 これは、PDF を表示するときに表示されるテキスト、画像、および書式設定を含む通常の PDF コンテンツとは異なります。 これらの添付ファイルは、画像、ドキュメント、スプレッドシート、その他の形式を含むさまざまなファイル形式を取ることができます。 通常、添付ファイルは、PDF を開いたときにユーザーがアクセスできる追加の参考資料や補足データを提供するために使用されます。 この機能は、包括的なPDFレポートを作成する場合や、複数のPDFをサポートドキュメントとマージする必要がある場合に特に役立ちます。
クイックスタート: PDF への添付ファイルの追加
IronPDFを使用してPDFドキュメントに添付ファイルを追加します。 この簡単な例では、ファイルを添付ファイルとして PDF に埋め込む方法を示します。 既存のPDFを読み込み、AddAttachmentメソッドを使用して、更新した文書を保存します。 このプロセスにより、補足資料がPDFに含まれ、どのPDFビューアからも直接アクセスできるようになります。
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
Install-Package IronPdf
2このコード スニペットをコピーして実行します。
var pdf = IronPdf.PdfDocument.FromFile("example.pdf");pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt"));pdf.SaveAs("updated.pdf");
var pdf = IronPdf.PdfDocument.FromFile("example.pdf");
pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt"));
pdf.SaveAs("updated.pdf");
using IronPdf;using System.IO;// Import attachment filebyte[] fileData = File.ReadAllBytes(@"path/to/file");// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("sample.pdf");// Add attachment to the PDFpdf.Attachments.AddAttachment("Example", fileData);pdf.SaveAs("addAttachment.pdf");
using IronPdf;
using System.IO;
// Import attachment file
byte[] fileData = File.ReadAllBytes(@"path/to/file");
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData);
pdf.SaveAs("addAttachment.pdf");
ImportsIronPdfImportsSystem.IO' Import attachment filePrivate fileData() AsByte = File.ReadAllBytes("path/to/file")' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("sample.pdf")' Add attachment to the PDFpdf.Attachments.AddAttachment("Example", fileData)pdf.SaveAs("addAttachment.pdf")
Imports IronPdf
Imports System.IO
' Import attachment file
Private fileData() As Byte = File.ReadAllBytes("path/to/file")
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData)
pdf.SaveAs("addAttachment.pdf")
PDFの添付ファイルは、Attachmentsプロパティにアクセスすることでバイナリデータとして取得できます。 バイナリデータを使用して、添付ファイルをそれぞれのファイル形式で PDF からエクスポートできます。
using IronPdf;using System.IO;// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");// Iterate through all attachmentsforeach (var attachment in pdf.Attachments){ if (attachment.Name.Contains("Example")) { // Save byte to fileFile.WriteAllBytes($"{attachment.Name}.doc", attachment.Data); }}
using IronPdf;
using System.IO;
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");
// Iterate through all attachments
foreach (var attachment in pdf.Attachments)
{
if (attachment.Name.Contains("Example"))
{
// Save byte to file
File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data);
}
}
ImportsIronPdfImportsSystem.IO' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("addAttachment.pdf")' Iterate through all attachmentsFor Each attachment In pdf.Attachments If attachment.Name.Contains("Example") Then ' Save byte to fileFile.WriteAllBytes($"{attachment.Name}.doc", attachment.Data) End IfNext attachment
Imports IronPdf
Imports System.IO
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")
' Iterate through all attachments
For Each attachment In pdf.Attachments
If attachment.Name.Contains("Example") Then
' Save byte to file
File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data)
End If
Next attachment
// Get all attachments as a listvar allAttachments = pdf.Attachments.ToList();// Filter attachments by size (e.g., files larger than 1MB)var largeAttachments = pdf.Attachments .Where(a => a.Data.Length > 1024 * 1024) .ToList();// Find specific attachment by exact namevar specificAttachment = pdf.Attachments .FirstOrDefault(a => a.Name == "report.xlsx");
// Get all attachments as a list
var allAttachments = pdf.Attachments.ToList();
// Filter attachments by size (e.g., files larger than 1MB)
var largeAttachments = pdf.Attachments
.Where(a => a.Data.Length > 1024 * 1024)
.ToList();
// Find specific attachment by exact name
var specificAttachment = pdf.Attachments
.FirstOrDefault(a => a.Name == "report.xlsx");
' Get all attachments as a listDim allAttachments = pdf.Attachments.ToList()' Filter attachments by size (e.g., files larger than 1MB)Dim largeAttachments = pdf.Attachments _ .Where(Function(a) a.Data.Length > 1024 * 1024) _ .ToList()' Find specific attachment by exact nameDim specificAttachment = pdf.Attachments _ .FirstOrDefault(Function(a) a.Name = "report.xlsx")
' Get all attachments as a list
Dim allAttachments = pdf.Attachments.ToList()
' Filter attachments by size (e.g., files larger than 1MB)
Dim largeAttachments = pdf.Attachments _
.Where(Function(a) a.Data.Length > 1024 * 1024) _
.ToList()
' Find specific attachment by exact name
Dim specificAttachment = pdf.Attachments _
.FirstOrDefault(Function(a) a.Name = "report.xlsx")
using IronPdf;using System.Linq;// Open existing PDFPdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");// Add attachment to the PDFPdfAttachmentCollection retrieveAttachments = pdf.Attachments;// Remove attachment from PDFpdf.Attachments.RemoveAttachment(retrieveAttachments.First());pdf.SaveAs("removeAttachment.pdf");
using IronPdf;
using System.Linq;
// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");
// Add attachment to the PDF
PdfAttachmentCollection retrieveAttachments = pdf.Attachments;
// Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First());
pdf.SaveAs("removeAttachment.pdf");
ImportsIronPdfImportsSystem.Linq' Open existing PDFPrivate pdf AsPdfDocument = PdfDocument.FromFile("addAttachment.pdf")' Add attachment to the PDFPrivate retrieveAttachments AsPdfAttachmentCollection = pdf.Attachments' Remove attachment from PDFpdf.Attachments.RemoveAttachment(retrieveAttachments.First())pdf.SaveAs("removeAttachment.pdf")
Imports IronPdf
Imports System.Linq
' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")
' Add attachment to the PDF
Private retrieveAttachments As PdfAttachmentCollection = pdf.Attachments
' Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First())
pdf.SaveAs("removeAttachment.pdf")
添付ファイルを削除して PDF ビューアで結果のファイルを開いた後、添付ファイルが表示されなくなります:
// Remove all attachments that match a patternvar attachmentsToRemove = pdf.Attachments .Where(a => a.Name.StartsWith("temp_")) .ToList();foreach (var attachment in attachmentsToRemove){ pdf.Attachments.RemoveAttachment(attachment);}// Or remove all attachments at oncewhile (pdf.Attachments.Count > 0){ pdf.Attachments.RemoveAttachment(pdf.Attachments.First());}
// Remove all attachments that match a pattern
var attachmentsToRemove = pdf.Attachments
.Where(a => a.Name.StartsWith("temp_"))
.ToList();
foreach (var attachment in attachmentsToRemove)
{
pdf.Attachments.RemoveAttachment(attachment);
}
// Or remove all attachments at once
while (pdf.Attachments.Count > 0)
{
pdf.Attachments.RemoveAttachment(pdf.Attachments.First());
}
ImportsSystem.Linq' Remove all attachments that match a patternDim attachmentsToRemove = pdf.Attachments _ .Where(Function(a) a.Name.StartsWith("temp_")) _ .ToList()For Each attachment In attachmentsToRemove pdf.Attachments.RemoveAttachment(attachment)Next' Or remove all attachments at onceWhile pdf.Attachments.Count > 0 pdf.Attachments.RemoveAttachment(pdf.Attachments.First())EndWhile
Imports System.Linq
' Remove all attachments that match a pattern
Dim attachmentsToRemove = pdf.Attachments _
.Where(Function(a) a.Name.StartsWith("temp_")) _
.ToList()
For Each attachment In attachmentsToRemove
pdf.Attachments.RemoveAttachment(attachment)
Next
' Or remove all attachments at once
While pdf.Attachments.Count > 0
pdf.Attachments.RemoveAttachment(pdf.Attachments.First())
End While
添付ファイルが正常に削除されたことを確認するにはどうすればよいですか?
添付ファイルの削除は、いくつかの方法で確認できます:
// Check the attachment countint attachmentCountBefore = pdf.Attachments.Count;pdf.Attachments.RemoveAttachment(targetAttachment);int attachmentCountAfter = pdf.Attachments.Count;// Verify the count decreasedif (attachmentCountAfter < attachmentCountBefore){Console.WriteLine("Attachment successfully removed");}// Check if specific attachment existsbool attachmentExists = pdf.Attachments .Any(a => a.Name == "specificFile.txt");
// Check the attachment count
int attachmentCountBefore = pdf.Attachments.Count;
pdf.Attachments.RemoveAttachment(targetAttachment);
int attachmentCountAfter = pdf.Attachments.Count;
// Verify the count decreased
if (attachmentCountAfter < attachmentCountBefore)
{
Console.WriteLine("Attachment successfully removed");
}
// Check if specific attachment exists
bool attachmentExists = pdf.Attachments
.Any(a => a.Name == "specificFile.txt");
' Check the attachment countDim attachmentCountBefore AsInteger = pdf.Attachments.Countpdf.Attachments.RemoveAttachment(targetAttachment)Dim attachmentCountAfter AsInteger = pdf.Attachments.Count' Verify the count decreasedIf attachmentCountAfter < attachmentCountBefore ThenConsole.WriteLine("Attachment successfully removed")End If' Check if specific attachment existsDim attachmentExists AsBoolean = pdf.Attachments _ .Any(Function(a) a.Name = "specificFile.txt")
' Check the attachment count
Dim attachmentCountBefore As Integer = pdf.Attachments.Count
pdf.Attachments.RemoveAttachment(targetAttachment)
Dim attachmentCountAfter As Integer = pdf.Attachments.Count
' Verify the count decreased
If attachmentCountAfter < attachmentCountBefore Then
Console.WriteLine("Attachment successfully removed")
End If
' Check if specific attachment exists
Dim attachmentExists As Boolean = pdf.Attachments _
.Any(Function(a) a.Name = "specificFile.txt")
Is it possible to filter PDF attachments by type or name in IronPDF?
Yes, you can filter attachments using the `Attachments` collection based on file extensions or specific name patterns.
What are the best practices for managing PDF attachments in IronPDF?
Some best practices include managing file sizes, using descriptive naming conventions, securing sensitive content with passwords, and leveraging asynchronous operations for better performance.
How can I verify if an attachment was successfully removed?
You can verify attachment removal by checking the number of attachments before and after removal or confirming the absence of the specific attachment by name in the `Attachments` collection.
What happens to the PDF file size when attachments are removed using IronPDF?
Removing attachments with IronPDF reduces the file size roughly by the size of the attachment, as the attachment data is completely removed from the PDF file.