I could not find the answer to this question. First of all, I want to make a pdf viewer application. At the same time, it is necessary to take notes on the pdf like a pen, and this must be recorded in the pdf, of course, the live drawn area must be visible. I will share my codes and screenshots below.
body: Stack(
children:[
SizedBox(
height: document.pageSettings.size.height,
width: document.pageSettings.size.height,
child: GestureDetector(
onPanUpdate: (details) {
setState(() {
RenderBox renderBox = context.findRenderObject() as RenderBox;
points.add(renderBox.globalToLocal(details.globalPosition));
});
},
onPanEnd: (details) async {
points.add(null);
File(widget.path!).writeAsBytesSync(await document.save());
setState(() {
points.clear();
});
},
child:RepaintBoundary(
child: CustomPaint(
size: document.pageSettings.size,
painter: PDFCanvasPainter2(points, document,currentPage, widget.path!),
foregroundPainter: PDFCanvasPainter2(points, document,currentPage, widget.path!),
child: SfPdfViewer.file(
File(widget.path!),
enableTextSelection: false,
onPageChanged: (details) {
currentPage = details.newPageNumber;
print(currentPage);
},
),
),
),
),
),
Positioned(
top: 20,
child: ElevatedButton(
onPressed: () {
setState(() {
isSwipe = !isSwipe;
});
},
child: isSwipe ? Text("Okuma Modu") : Text("Çizim Modu"),
),
),
]
),
);
the black pen shape is my drawing and other red shapw is output.
my painter :
class PDFCanvasPainter2 extends CustomPainter {
final List<Offset?> points;
final PdfDocument document;
final int currentPage;
final String path;
const PDFCanvasPainter2( this.points, this.document, this.currentPage, this.path);
Offset adjustOffset(Offset offset, Size pageSize) {
return Offset(offset.dx, offset.dy);
}
@override
void paint(Canvas canvas, Size size) async {
PdfPage page = document.pages[currentPage != 0 ? currentPage - 1 : currentPage];
PdfPageLayer layer = page.layers.add(name: "layer1");
PdfGraphics graphics = layer.graphics;
Paint paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 2.0;
try{
for (int i = 0; i < points.length - 1; i++) {
if (points[i] != null && points[i + 1] != null) {
Offset adjustedStart = adjustOffset(points[i]!, page.size);
Offset adjustedEnd = adjustOffset(points[i + 1]!, page.size);
canvas.drawLine(points[i]!, points[i + 1]!, paint);
graphics.drawLine(
PdfPen(PdfColor(250, 42, 42), width: 2),
adjustedStart,
adjustedEnd
);
} else if (points[i] != null && points[i + 1] == null) {
// Kullanıcının kalemi kaldırdığını gösterir.
//document.pages.add().graphics.draw
}
}
}catch(e){
print(e);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
My first problem is that the shape I drew does not instantly save to the pdf. I can only see it when I open and close the pdf. My second problem is that the shape I draw does not appear where I draw it. The pdf is shaped according to the page size.
thank you so much.
I used many methods and packages and I was not successful in any of them. I could not even find a ready-made code separately.



