私は多くのpdfをマージしようとしています.pdfごとにブックマーク(pdfの名前)を追加したいのですが、pdfをマージするさまざまな手法を見つけましたが、ブックマークのみを追加することはできません. itextsharp は章を追加し、次にその章のブックマークを追加します。pdf を変更したくありません。
質問する
5788 次
3 に答える
14
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();
}
于 2012-06-15T04:59:49.137 に答える
0
タスクにはDocotic.Pdfライブラリを試してください。
これがあなたが説明したことをするサンプルコードです:
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ライブラリを開発している会社で働いています。
于 2012-04-25T17:18:24.243 に答える
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;
}
foreachループを使用してpdfを調べ、ステートメントを使用して、Md Kamruzzaman Sarkerの回答を最適化しました。このように、私にはきれいに見えますが、すべてのクレジットは彼にあります.
于 2017-05-24T08:07:39.933 に答える