我試圖合併很多PDF文件,併爲每個PDF我想添加一個書籤(PDF的名稱)合併的PDF文件,我發現合併的PDF文件的difrent技術,但沒有人可以只添加例如前面的書籤。 itextsharp添加了一章,然後是章節的書籤,我不想改變pdf的。使用書籤
Q
使用書籤
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");
}
}
樣品結合不同的文件到一個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;
}
我用foreach循環走了過來PDF和using語句優化馬里蘭州Kamruzzaman Sarker的答案。像這樣,它看起來更清潔,但所有的積分都歸他所有。
相關問題
- 1. 使用書籤
- 2. Gmail之星使用jQuery書籤書籤
- 3. 使用多個證書籤署證書
- 4. 使用jQuery點擊書籤
- 5. 使用自簽名證書
- 6. 使用PYPDF2摺疊書籤
- 7. 使用document.body.search()的小書籤
- 8. 如何使用書籤
- 9. 顯示:無使用書籤
- 10. 使用自簽名證書
- 11. 使用xml刪除書籤
- 12. 使用JavaScript的Facebook書籤
- 13. 如何使用localStorage將書籤添加到書籤Ionic
- 14. android書籤ContentProvider使書籤,但在瀏覽器的書籤不可見
- 15. 使用書籤參數與/用戶/
- 16. 書籤
- 17. IdentityServer4簽名證書:使用公共可用的證書+密鑰?
- 18. 書籤+列表書籤崩潰Emacs
- 19. 使用Ext JS書籤製表符?
- 20. 使用VBA動態創建書籤
- 21. 如何使用PHP讀取PDF書籤?
- 22. 在jQuery中使用書籤地址
- 23. 使用書籤小甜餅閱讀
- 24. 修改使用JavaScript書籤的網址
- 25. 使用OpenSSL.net簽署證書的問題
- 26. 在JS書籤中使用Tinybox
- 27. 使用已過濾查詢的書籤
- 28. 如何使用evernote api檢索書籤?
- 29. 使用證書籤署Soap請求
- 30. 使用Java 8的自簽名證書
也許你需要提取的個人頁面和他們重新組合成一個單一的文件。這樣你可以用書籤標記每個pdf的第一頁 – gyurisc 2012-04-17 08:15:03
我不知道如何添加一個簡單的標記 – XandrUu 2012-04-17 08:16:35