私は多くのpdfをマージしようとしていますが、ブックマーク(pdfの名前)を追加したいのですが、pdfをマージする方法は異なりますが、ブックマークの先頭など。 itextsharpは章を追加してから章のしおりを、私はpdfを変更したくありません。ブックマークでPDFファイルをマージする
5
A
答えて
13
itextsharpを使用するとできます。 私は法に従うことによってそれを行う...
MergePdfFiles(string outputPdf, string[] sourcePdfs)
{
PdfReader reader = null;
Document document = new Document();
PdfImportedPage page = null;
PdfCopy pdfCpy = null;
int n = 0;
int totalPages = 0;
int page_offset = 0;
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
IList<Dictionary<string, object>> tempBookmarks;
for (int i = 0; i <= sourcePdfs.GetUpperBound(0); i++)
{
reader = new PdfReader(sourcePdfs[i]);
reader.ConsolidateNamedDestinations();
n = reader.NumberOfPages;
tempBookmarks = SimpleBookmark.GetBookmark(reader);
if (i == 0)
{
document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
pdfCpy = new PdfCopy(document, new FileStream(outputPdf, FileMode.Create));
document.Open();
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
page_offset += n;
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
// MessageBox.Show(n.ToString());
totalPages = n;
}
else
{
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
page_offset += n;
totalPages += n;
}
for (int j = 1; j <= n; j++)
{
page = pdfCpy.GetImportedPage(reader, j);
pdfCpy.AddPage(page);
}
reader.Close();
}
pdfCpy.Outlines = bookmarks;
document.Close();
}
0
タスクのためにDocotic.Pdf libraryを試してください。ここで
はあなたが説明したものを行い、サンプルコードです:
public static void combineDocumentsWithBookmarks()
{
string[] names = new string[] { "first.pdf", "second.pdf", "third.pdf" };
using (PdfDocument pdf = new PdfDocument())
{
int targetPageIndex = 0;
for (int i = 0; i < names.Length; i++)
{
string currentName = names[i];
if (i == 0)
pdf.Open(currentName);
else
pdf.Append(currentName);
pdf.OutlineRoot.AddChild(currentName, targetPageIndex);
targetPageIndex = pdf.PageCount;
}
// setting PageMode will cause PDF viewer to display
// bookmarks pane when document is open
pdf.PageMode = PdfPageMode.UseOutlines;
pdf.Save("output.pdf");
}
}
サンプルは、1つのPDFに別の文書を結合してブックマークを作成します。各ブックマークは、元の文書の最初のページを指しています。
免責事項:私はDocotic.Pdfライブラリを開発している会社で働いています。
0
public string MergeFiles(string outputPath)
{
if (string.IsNullOrEmpty(outputPath))
throw new NullReferenceException("Path for output document is null or empty.");
using (Document outputDocument = new Document())
{
using (PdfCopy pdf = new PdfCopy(outputDocument, new FileStream(outputPath, FileMode.Create)))
{
outputDocument.Open();
// All bookmarks for output document
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
// Bookmarks of the current document
IList<Dictionary<string, object>> tempBookmarks;
int pageOffset = 0;
// Merge documents and add bookmarks
foreach (string file in Files)
{
using (PdfReader reader = new PdfReader(file))
{
reader.ConsolidateNamedDestinations();
// Get bookmarks of current document
tempBookmarks = SimpleBookmark.GetBookmark(reader);
SimpleBookmark.ShiftPageNumbers(tempBookmarks, pageOffset, null);
pageOffset += reader.NumberOfPages;
if(tempBookmarks != null)
// Add bookmarks of current document to all bookmarks
bookmarks.AddRange(tempBookmarks);
// Add every page of document to output document
for (int i = 1; i <= reader.NumberOfPages; i++)
pdf.AddPage(pdf.GetImportedPage(reader, i));
}
}
// Add all bookmarks to output document
pdf.Outlines = bookmarks;
}
}
return outputPath;
}
私はpdfファイルの上に行くためにforeachループを使用してステートメントを使用してメリーKamruzzaman Sarkerの答えを最適化。このように私はより清潔に見えるが、すべてのクレジットは彼に行く。
関連する問題
- 1. ブックマークでPDFファイルのコマンドラインをマージする
- 2. PDF分割、マージ、ブックマークなど
- 3. Itext7でマージするときにPDFにブックマークを追加する方法
- 4. PHPでPDFファイルをマージする
- 5. PDFファイルでネストされたブックマークを取得する
- 6. pdfファイルとpypdfをマージする
- 7. レンダラ(XSL)でPDFブックマークを作る方法
- 8. 複数のPDFファイルを1つのPDFにマージする方法
- 9. PyPDF2でPDFにブックマークを追加
- 10. Asciidoctor-pdfブックマークの深さ
- 11. スウィフト3.0で1つのpdfを別のpdfファイルにマージするには?
- 12. ファイルのタイムスタンプ別にpdfファイルをマージする
- 13. c#を使用してpdfファイルのブックマークを取得する方法
- 14. iText PDFをクロップボックスでマージする
- 15. 2つのファイルpdfを1つのファイルにマージしますか?
- 16. iTextSharpを使用してPDFファイルを出力ストリームにマージする
- 17. のColdFusion/DDX PDF単一ページのブックマーク
- 18. フィールド内の値に基づいてPDFファイルをマージする
- 19. pngイメージを1つのpdfファイルにマージする
- 20. Wireshark - PDFファイルの部分コンテンツデータをマージする方法
- 21. すべてのPdfのブックマークを取得する
- 22. C#を使用してPDFブックマークのURLを編集する
- 23. itextsharpを使用してpdfにブックマークを追加する
- 24. PDF IText 5のテキストをブックマークするには?
- 25. PHPを使ってPDFをマージする
- 26. Chromeブックマークを解析するJsonファイル:Java
- 27. pdfファイル名をデータベースから取り除くと、PDFファイルをPHPにマージするには?
- 28. ノード内のPDFをマージする
- 29. 多くのpdfをマージする方法
- 30. reportlibにpython3でPDFファイルをマージする方法はありますか?
個々のページを抽出し、それらを1つのファイルに再構成する必要があるかもしれません。この方法で、各PDFの最初のページにブックマーク – gyurisc
を付けることができます。simplebookmarkを追加する方法がわかりません – XandrUu