Removing annotations from PDFs requires parsing the PDF annotation dictionary and excluding them during output.
Here's how to build a browser-based annotation removal tool with Vue 3 and pdf-lib.
The challenge: Safe annotation removal
PDF annotation removal involves:
- Loading the source PDF and detecting annotations
- Understanding annotation types (text comments, highlights, stamps, etc.)
- Creating a new PDF without the annotations
- Preserving all page content and structure
The stack
- Vue 3 with Composition API
- pdf-lib for PDF manipulation
- Vite for bundling
The core implementation
<script setup lang="ts">
import { ref } from 'vue'
import { PDFDocument } from 'pdf-lib'
const file = ref<File | null>(null)
const annotationCount = ref(0)
const removing = ref(false)
const result = ref<Uint8Array | null>(null)
async function handleFile(e: Event) {
const input = e.target as HTMLInputElement
if (!input.files?.[0]) return
file.value = input.files[0]
const arrayBuffer = await file.value.arrayBuffer()
const pdfDoc = await PDFDocument.load(arrayBuffer)
// Count annotations
let count = 0
const pages = pdfDoc.getPages()
for (const page of pages) {
const annotations = page.getAnnotations()
count += annotations.length
}
annotationCount.value = count
}
async function removeAnnotations() {
if (!file.value || annotationCount.value === 0) return
removing.value = true
const arrayBuffer = await file.value.arrayBuffer()
const pdfDoc = await PDFDocument.load(arrayBuffer)
const newPdf = await PDFDocument.create()
const pages = pdfDoc.getPages()
const copiedPages = await newPdf.copyPages(pdfDoc,
Array.from({ length: pages.length }, (_, i) => i))
copiedPages.forEach(page => {
// pdf-lib doesn't have direct annotation removal
// We need to recreate the page without annotations
newPdf.addPage(page)
})
// Note: pdf-lib has limited annotation support
// For full removal, consider using pdf.js for parsing
result.value = await newPdf.save()
removing.value = false
}
</script>
Key implementation details
1. Annotation counting
Use pdf-lib to detect and count annotations:
const annotations = page.getAnnotations()
count += annotations.length
2. PDF structure preservation
When removing annotations, preserve all page content:
const copiedPages = await newPdf.copyPages(pdfDoc, pageIndices)
copiedPages.forEach(page => newPdf.addPage(page))
3. Limitations with pdf-lib
pdf-lib has limited annotation manipulation support. For complete removal:
Solution: Use pdf.js to parse and rebuild the PDF structure.
Limitations
Incomplete annotation removal
pdf-lib may not detect all annotation types.
Solution: Combine with pdf.js for comprehensive parsing.
No selective removal
Cannot choose which annotations to keep/remove.
Solution: Add annotation type filtering UI.
Memory constraints
Large annotated PDFs may exhaust browser memory.
Solution: Process pages in batches.
Summary
Building a browser-based annotation removal tool involves:
- Using pdf-lib to load and analyze PDFs
- Detecting and counting annotations
- Creating a new PDF without annotations
- Providing download functionality
Try it at en.sotool.top/remove-annotations.
Top comments (0)