PDF内のアノテーションを管理できます。
PDFにはコメント、提案、マークアップのような図形やハイライトをアノテーション(注釈)として追加できます。
アノテーションはページとは別のレイヤに適用されます。そのため、アノテーションはページコンテンツの一部ではありません。
通常のページコンテンツとは異なり、多くのアノテーションタイプはPDFビューアでインタラクティブに動作します。
Toolbox Add-onは以下のタイプのアノテーションに対応しています。
Highlight
Popup
StickyNote
StrikeThrough
Underline
FreeText
Ellipse
Ink
Line
Polygon
PolyLine
Rectangle
Squiggly
InternalLink
WebLink
TextStamp
CustomStamp
FileAttachment
Toolbox Add-onのAPIリファレンスはこちらです。(すべて英文)
C#のサンプルプロジェクトではPdftools SDK(Toolbox Add-on)ライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDK(Toolbox Add-on)ライブラリ(DLL)が含まれています。
ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。
License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。
入力PDFの内容を出力PDFにコピーして、コピーされた出力PDFにアノテーションを追加します。
// Open input document using (Stream inStream = new FileStream(inPath, FileMode.Open, FileAccess.Read)) using (Document inDoc = Document.Open(inStream, null)) { // Create output document using Stream outStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite); using Document outDoc = Document.Create(outStream, inDoc.Conformance, null); // Copy document-wide data CopyDocumentData(inDoc, outDoc); // Define page copy options PageCopyOptions copyOptions = new PageCopyOptions(); // Copy first page and add annotations Page outPage = CopyAndAddAnnotations(outDoc, inDoc.Pages[0], copyOptions); // Add the page to the output document's page list outDoc.Pages.Add(outPage); // Copy the remaining pages and add to the output document's page list PageList inPages = inDoc.Pages.GetRange(1, inDoc.Pages.Count - 1); PageList outPages = PageList.Copy(outDoc, inPages, copyOptions); outDoc.Pages.AddRange(outPages);
private static void CopyDocumentData(Document inDoc, Document outDoc) { // Copy document-wide data // Output intent if (inDoc.OutputIntent != null) outDoc.OutputIntent = IccBasedColorSpace.Copy(outDoc, inDoc.OutputIntent); // Metadata outDoc.Metadata = Metadata.Copy(outDoc, inDoc.Metadata); // Viewer settings outDoc.ViewerSettings = ViewerSettings.Copy(outDoc, inDoc.ViewerSettings); // Associated files (for PDF/A-3 and PDF 2.0 only) FileReferenceList outAssociatedFiles = outDoc.AssociatedFiles; foreach (FileReference inFileRef in inDoc.AssociatedFiles) outAssociatedFiles.Add(FileReference.Copy(outDoc, inFileRef)); // Plain embedded files FileReferenceList outEmbeddedFiles = outDoc.PlainEmbeddedFiles; foreach (FileReference inFileRef in inDoc.PlainEmbeddedFiles) outEmbeddedFiles.Add(FileReference.Copy(outDoc, inFileRef)); }
private static Page CopyAndAddAnnotations(Document outDoc, Page inPage, PageCopyOptions copyOptions) { // Copy page to output document Page outPage = Page.Copy(outDoc, inPage, copyOptions); // Make a RGB color space ColorSpace rgb = ColorSpace.CreateProcessColorSpace(outDoc, ProcessColorSpaceType.Rgb); // Get the page size for positioning annotations Size pageSize = outPage.Size; // Get the output page's list of annotations for adding annotations AnnotationList annotations = outPage.Annotations; // Create a sticky note and add to output page's annotations Paint green = Paint.Create(outDoc, rgb, new double[] { 0, 1, 0 }, null); Point stickyNoteTopLeft = new Point() { X = 10, Y = pageSize.Height - 10 }; StickyNote stickyNote = StickyNote.Create(outDoc, stickyNoteTopLeft, "Hello world!", green); annotations.Add(stickyNote); // Create an ellipse and add to output page's annotations Paint blue = Paint.Create(outDoc, rgb, new double[] { 0, 0, 1 }, null); Paint yellow = Paint.Create(outDoc, rgb, new double[] { 1, 1, 0 }, null); Rectangle ellipseBox = new Rectangle() { Left = 10, Bottom = pageSize.Height - 60, Right = 70, Top = pageSize.Height - 20 }; EllipseAnnotation ellipse = EllipseAnnotation.Create(outDoc, ellipseBox, new Stroke(blue, 1.5), yellow); annotations.Add(ellipse); // Create a free text and add to output page's annotations Paint yellowTransp = Paint.Create(outDoc, rgb, new double[] { 1, 1, 0 }, new Transparency(0.5)); Rectangle freeTextBox = new Rectangle() { Left = 10, Bottom = pageSize.Height - 170, Right = 120, Top = pageSize.Height - 70 }; FreeText freeText = FreeText.Create(outDoc, freeTextBox, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", yellowTransp); annotations.Add(freeText); // A highlight and a web-link to be fitted on existing page content elements Highlight highlight = null; WebLink webLink = null; // Extract content elements from the input page ContentExtractor extractor = new ContentExtractor(inPage.Content); foreach (ContentElement element in extractor) { // Take the first text element if (highlight == null && element is TextElement textElement) { // Get the quadrilaterals of this text element QuadrilateralList quadrilaterals = new QuadrilateralList(); foreach (TextFragment fragment in textElement.Text) quadrilaterals.Add(fragment.Transform.TransformRectangle(fragment.BoundingBox)); // Create a highlight and add to output page's annotations highlight = Highlight.CreateFromQuadrilaterals(outDoc, quadrilaterals, yellow); annotations.Add(highlight); } // Take the first image element if (webLink == null && element is ImageElement) { // Get the quadrilateral of this image QuadrilateralList quadrilaterals = new QuadrilateralList(); quadrilaterals.Add(element.Transform.TransformRectangle(element.BoundingBox)); // Create a web-link and add to the output page's links webLink = WebLink.CreateFromQuadrilaterals(outDoc, quadrilaterals, "https://www.pdf-tools.com"); Paint red = Paint.Create(outDoc, rgb, new double[] { 1, 0, 0 }, null); webLink.BorderStyle = new Stroke(red, 1.5); outPage.Links.Add(webLink); } // Exit loop if highlight and webLink have been created if (highlight != null && webLink != null) break; } // return the finished page return outPage; }
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。